pyenv
管理不同版本的 Python 开发环境
安装
mac安装
brew install pyenv
linux安装
yum install pyenv
使用
➜ virtualenv pyenv -h
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
commands List all available pyenv commands
local Set or show the local application-specific Python version
global Set or show the global Python version
shell Set or show the shell-specific Python version
install Install a Python version using python-build
uninstall Uninstall a specific Python version
rehash Rehash pyenv shims (run this after installing executables)
version Show the current Python version and its origin
versions List all Python versions available to pyenv
which Display the full path to an executable
whence List all Python versions that contain the given executable
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
添加环境变量
需要将以下环境变量添加到对应的文件中
zsh是~/.zshrc,bash是~/.bashrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
查看可用版本
➜ virtualenv pyenv install --list | grep '3.6.[1-9]'
3.6.1
3.6.2
3.6.3
安装指定版本
➜ virtualenv pyenv install 3.6.3
Downloading Python-3.6.3.tar.xz...
-> https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz
Installing Python-3.6.3...
Installed Python-3.6.3 to /Users/zhujipeng/.pyenv/versions/3.6.3
➜ virtualenv pyenv versions
* system (set by /Users/zhujipeng/.pyenv/version)
3.6.3
切换指定版本
➜ virtualenv pyenv global 3.6.3
➜ virtualenv python -V
Python 3.6.3
➜ virtualenv pyenv global system
➜ virtualenv python -V
Python 2.7.10
virtualenv
创建独立系统的的 Python 开发环境
安装
在线安装
pip install virtualenv
离线安装
pip install virtualenv-15.1.0-py2.py3-none-any.whl
virtualenv-15.1.0-py2.py3-none-any.whl需要更换为实际的wheel安装包
使用
➜ virtualenv virtualenv -h
Usage: virtualenv [OPTIONS] DEST_DIR
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-v, --verbose Increase verbosity.
-q, --quiet Decrease verbosity.
-p PYTHON_EXE, --python=PYTHON_EXE
The Python interpreter to use, e.g.,
--python=python2.5 will use the python2.5 interpreter
to create the new environment. The default is the
interpreter that virtualenv was installed with
(/usr/local/opt/python/bin/python2.7)
--clear Clear out the non-root install and start from scratch.
--no-site-packages DEPRECATED. Retained only for backward compatibility.
Not having access to global site-packages is now the
default behavior.
--system-site-packages
Give the virtual environment access to the global
site-packages.
--always-copy Always copy files rather than symlinking.
--unzip-setuptools Unzip Setuptools when installing it.
--relocatable Make an EXISTING virtualenv environment relocatable.
This fixes up scripts and makes all .pth files
relative.
--no-setuptools Do not install setuptools in the new virtualenv.
--no-pip Do not install pip in the new virtualenv.
--no-wheel Do not install wheel in the new virtualenv.
--extra-search-dir=DIR
Directory to look for setuptools/pip distributions in.
This option can be used multiple times.
--download Download preinstalled packages from PyPI.
--no-download, --never-download
Do not download preinstalled packages from PyPI.
--prompt=PROMPT Provides an alternative prompt prefix for this
environment.
--setuptools DEPRECATED. Retained only for backward compatibility.
This option has no effect.
--distribute DEPRECATED. Retained only for backward compatibility.
This option has no effect.
| 选项 | 说明 |
|---|---|
| -p PYTHON_EXE, --python=PYTHON_EXE | 指使用的python解释器 |
| --system-site-packages | 可以使用系统的中的包 |
| --relocatable | 使得virtualenv的环境变量是可移植的 |
使用当前python
➜ virtualenv virtualenv python27
New python executable in /Users/zhujipeng/Downloads/test/virtualenv/python27/bin/python2.7
Also creating executable in /Users/zhujipeng/Downloads/test/virtualenv/python27/bin/python
Installing setuptools, pip, wheel...done.
➜ virtualenv ls python27
bin include lib pip-selfcheck.json
➜ virtualenv python27/bin/python -V
Python 2.7.13
➜ virtualenv python27/bin/pip list --format=columns
Package Version
---------- -------
pip 9.0.1
setuptools 37.0.0
wheel 0.30.0
指定python版本
➜ virtualenv virtualenv -p /usr/bin/python2.6 python26
Running virtualenv with interpreter /usr/bin/python2.6
New python executable in /Users/zhujipeng/Downloads/test/virtualenv/python26/bin/python2.6
Also creating executable in /Users/zhujipeng/Downloads/test/virtualenv/python26/bin/python
Installing setuptools, pip, wheel...done.
➜ virtualenv ls python26
bin include lib pip-selfcheck.json
➜ virtualenv python26/bin/python -V
Python 2.6.9
➜ virtualenv python26/bin/pip list --format=columns
DEPRECATION: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of pip will drop support for Python 2.6
Package Version
---------- -------
pip 9.0.1
setuptools 36.8.0
wheel 0.30.0
激活指定python
➜ virtualenv python -V
Python 2.7.11
➜ virtualenv source python27/bin/activate
(python27) ➜ virtualenv which python
/Users/zhujipeng/Downloads/test/virtualenv/python27/bin/python
(python27) ➜ virtualenv python -V
Python 2.7.13
(python27) ➜ virtualenv deactivate
➜ virtualenv source python26/bin/activate
(python26) ➜ virtualenv which python
/Users/zhujipeng/Downloads/test/virtualenv/python26/bin/python
(python26) ➜ virtualenv python -V
Python 2.6.9
(python26) ➜ virtualenv deactivate
deactivate退出激活状态
移植
要想生成的 Python 开发环境可以直接拷贝别的机器,需要变为可移植的
➜ virtualenv virtualenv --relocatable python26
Making script /Users/zhujipeng/Downloads/test/virtualenv/python26/bin/easy_install relative
Making script /Users/zhujipeng/Downloads/test/virtualenv/python26/bin/easy_install-2.6 relative
Making script /Users/zhujipeng/Downloads/test/virtualenv/python26/bin/pip relative
Making script /Users/zhujipeng/Downloads/test/virtualenv/python26/bin/pip2 relative
Making script /Users/zhujipeng/Downloads/test/virtualenv/python26/bin/pip2.6 relative
Making script /Users/zhujipeng/Downloads/test/virtualenv/python26/bin/python-config relative
Making script /Users/zhujipeng/Downloads/test/virtualenv/python26/bin/wheel relative
参考
Python 多版本共存之 pyenv
Python--Virtualenv简明教程
pyenv global system does not work