Skip to content

Instantly share code, notes, and snippets.

@JeOam
Last active June 6, 2018 03:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JeOam/121529345d2be64584ee to your computer and use it in GitHub Desktop.
Save JeOam/121529345d2be64584ee to your computer and use it in GitHub Desktop.
Python 环境配置

Python 环境配置: 安装 pip: A tool for installing and managing Python packages.

To install or upgrade pip, securely download get-pip.py. Then run the following (which may require administrator access):

$ sudo python get-pip.py
#Upgrade pip
$ pip install -U pip 

注:也可以 brew install python 来安装 pip

安装 VirtualEnvWrapper:

Install virtualenv
$ sudo pip install virtualenv 
$ sudo pip install virtualenvwrapper
$ subl ~/.zshrc

添加:source /usr/local/bin/virtualenvwrapper.sh

创建新的虚拟环境:$ mkvirtualenv venv,环境存放的目录是~/.virtualenv/venv; $ workon #列出当前环境 $ workon venv #切换环境 $ deactivate #退出环境

Ref: Install Python 2.7 (homebrew + pip + virtualenv) on Mac OS X Lion


Sublime Text 3 Setting

@JeOam
Copy link
Author

JeOam commented Sep 17, 2014

pip 安装 Python 库总是下载超时,修改 pip 源:建个文件 ~/.pip/pip.conf, 内容如下:

[global]
timeout = 6000
index-url = http://pypi.douban.com/simple

Ref:http://segmentfault.com/q/1010000000162410

@JeOam
Copy link
Author

JeOam commented Jun 12, 2015

配置 Vagrant 上开发环境:

  1. 下载安装 Vagrant
  2. 下载安装 VirtualBox
  3. 选择 Vagrant 的 OS 环境:https://atlas.hashicorp.com/boxes/search?provider=virtualbox ,这里选 ubuntu/trusty64
  4. 初始化 Vagrant:
$ subl ~/Vagrantfile  #设置 `config.vm.box = "ubuntu/trusty64"`
$ vagrant box add ubuntu/trusty64  [本地box文件路径] #box 下载慢,额外下载
$ vagrant init ubuntu/trusty64
$ vagrant up

这里获取的 vagrant box 镜像放在: ~/.vagrant.d/boxes/ 下。

参考: 1.2 Vgrant安装配置, 3 Vgrant使用入门, 使用 Vagrant 打造跨平台开发环境

打包 vagrant 环境:

$ vagrant halt
$ vagrant package
# create a package.box file (default name) in the same folder with
$ vagrant box list
$ vagrant box remove my-box
$ vagrant box add my-box file:///d:/path/to/file.box

解决设置共享目录时的 Can not install Guest Additions in VirtualBox for CentOS 7 问题:

  1. /Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso 文件放进虚拟机内。
  2. Centos 7 内:sudo yum install gcc kernel-devel
  3. sudo mkdir -p /media/VirtualBoxGuestAdditions
  4. sudo mount -t iso9660 -o loop VBoxGuestAdditions.iso /media/VirtualBoxGuestAdditions/
  5. sudo /media/VirtualBoxGuestAdditions/VBoxLinuxAdditions.run

参考:

@JeOam
Copy link
Author

JeOam commented Jun 12, 2015

Ubuntu 14.04 环境配置

0.采用国内的软件源:https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/

1.安装软件

2.下载安装 deb 软件:

  1. wget [file url]
  2. sudo dpkg -i [filename.deb]

3.tsocks: 让 apt-get 走 socks 代理的流量

  • sudo apt-get install tsocks
  • 编辑 /etc/tsocks.conf 里面的 serverserver_port
  • sudo -s
  • tsocks apt-get update # 在 apt-get 前加 tsocks

@JeOam
Copy link
Author

JeOam commented Jul 22, 2015

连接 Vagrant 虚拟环境:

$ vagrant ssh

查看连接 Vagrant 时的 ssh config 细节:

$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /path/to/private_key
  IdentitiesOnly yes
  LogLevel FATAL

所以上面的连接命令等价于:

$ ssh vagrant@127.0.0.1 -p 2222 -i /path/to/private_key

@JeOam
Copy link
Author

JeOam commented Oct 22, 2015

创建虚拟环境时,指定 Python 版本

cd ~/.virtualenvs
virtualenv [name] --python=python3.5
#or
python3.6 -m venv [name]

然后就可以 workon name 进入创建的虚拟环境了。

@JeOam
Copy link
Author

JeOam commented Nov 5, 2015

安装时,指定 pip 源:

pip install Scrapy -i http://pypi.mirrors.ustc.edu.cn/simple # 中国科学技术大学

@JeOam
Copy link
Author

JeOam commented Feb 29, 2016

一些安装完 Python 后,可选的软件包:

  • python-dev 在编译 C 模块时,所需的 Python 头文件
  • python-profiler 它包含了一些完全针对 GPL 协议的 Linux 发行版(诸如 Debian 或 Ubuntu)的非 GPL 协议模块(Hotshot 优化器)
  • gcc 在编译包含 C 代码扩展时所需的软件包

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment