[toc]
环境准备
pyenv
- 安装Python解释器
- 管理Python版本
- 管理Python虚拟环境
相当于一个独立的版本,是bash写的一个程序,所以没有依赖;
- pyenv github地址
https://github.com/yyuu/pyenv-installer
- 苹果系统安装pyenv
# curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
# sudo vim /etc/bashrc
export PATH="/Users/ssjinyao/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
# source /etc/bashrc
- linux系统安装pyenv
# curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
# vim /etc/profile.d/pyenv.sh
export PATH="/Users/ssjinyao/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
- 安装一个python的依赖(CentOS)
# yum -y install. gcc make patch gdbm-devel openssl-devel sqlite-devel zlib-devel bzip2-devel readline-devel
# 注:在不同的linux系统上安装包的名字不一样
- pyenv安装一个python版本
# pyenv update #用来更新pyenv
# pyenv install 3.5.2
- 切换python 3.5.3
bogon:~ ssjinyao$ pyenv local 3.5.2
bogon:~ ssjinyao$ pyenv version
3.5.2 (set by /Users/ssjinyao/.python-version)
bogon:~ ssjinyao$ cat .python-version
3.5.2
- 切换python到系统版本
bogon:~ ssjinyao$ pyenv local system
bogon:~ ssjinyao$ pyenv version
system (set by /Users/ssjinyao/.python-version)
- 切换全局python变量(希望永远不要执行)
# pyenv global x.x.x
使用pyenv
local 命令
local 命令切换当前目录及其子目录的Python版本
如何恢复,可以通过删除 ‘.python-version’恢复默认的Python版本global 命令
global命令切换全局默认python版本
bogon:~ ssjinyao$ pyenv commands
--version
activate
commands
completions
deactivate
doctor
exec
global
help
hooks
init
install
installer
local
offline-installer
prefix
rehash
root
shell
shims
uninstall
update
version
version-file
version-file-read
version-file-write
version-name
version-origin
versions
virtualenv
virtualenv-delete
virtualenv-init
virtualenv-prefix
virtualenvs
whence
which
bogon:~ ssjinyao$ pyenv virtualenv 3.5.2 magedu
Ignoring indexes: https://pypi.python.org/simple
Requirement already satisfied (use --upgrade to upgrade): setuptools in /Users/ssjinyao/.pyenv/versions/3.5.2/envs/magedu/lib/python3.5/site-packages
Requirement already satisfied (use --upgrade to upgrade): pip in /Users/ssjinyao/.pyenv/versions/3.5.2/envs/magedu/lib/python3.5/site-packages
bogon:~ ssjinyao$ pyenv local 3.5.2/envs/magedu
(3.5.2/envs/magedu) bogon:~ ssjinyao$
(3.5.2/envs/magedu) bogon:~ ssjinyao$ pyenv local 3.5.2
bogon:~ ssjinyao$
bogon:~ ssjinyao$ ls -l ~/.pyenv/versions/
total 8
drwxr-xr-x 7 ssjinyao staff 238 11 19 10:30 3.5.2
lrwxr-xr-x 1 ssjinyao staff 49 11 19 10:35 magedu -> /Users/ssjinyao/.pyenv/versions/3.5.2/envs/magedu
bogon:~ ssjinyao$ pyenv versions
system
* 3.5.2 (set by /Users/ssjinyao/.python-version)
3.5.2/envs/magedu
magedu
bogon:~ ssjinyao$ pyenv uninstall magedu
pyenv-virtualenv: remove /Users/ssjinyao/.pyenv/versions/3.5.2/envs/magedu? y
bogon:~ ssjinyao$ pyenv versions
system
* 3.5.2 (set by /Users/ssjinyao/.python-version)
virtualenv 命令
创建虚拟环境pyenv virtualenv $bash_version $name
uninstall 命令
卸载某个版本,包括虚拟环境;
选择python版本时,可以这样选择
bogon:~ ssjinyao$ ls ~/.pyenv/versions/3.5.2/envs/magedu/bin/python
/Users/ssjinyao/.pyenv/versions/3.5.2/envs/magedu/bin/python