更新2018年
这些天,我建议使用Pipenv创建虚拟环境并在Python中处理项目依赖项.
Pipenv — the officially recommended Python packaging tool from Python.org, free (as in freedom).
Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. Windows is a first–class citizen, in our world.
要创建虚拟环境,请cd进入项目目录并运行:
pipenv --python 3.6
在您的环境中安装包:
pipenv install my_package
或者如果依赖项仅用于开发目的(测试等):
pipenv install my_packge --dev
在您的环境中运行:
pipenv run python my_file.py
老答案
在Linux上,您可以轻松地在主服务器旁边使用install multiple versions of Python,您可以使用标准库中的venv软件包从每个版本> = 3.3创建虚拟环境.
创建venv
$python3.3 -m venv myvenv_foo # Create a python3.4 venv named 'myvenv_foo'
$python3.4 -m venv myvenv_bar # Create a python3.4 venv named 'myvenv_bar'
$python3.5 -m venv myvenv_baz # Create a python3.5 venv named 'myvenv_baz'
# etc...
激活venv
source myvenv_foo/bin/activate # Activates venv 'myvenv_foo'
停用venv
deactivate
注意:python与pythonX.X
如果安装了多个Python版本,则可以通过向命令添加版本号来访问每个版本,例如python3.5,python3.6等.但请记住,当你激活一个venv时,你将它绑定到clean / versionless python命令,只要它被激活.例如:
$python -V # Use the *clean* 'python' command to show the main version of the OS.
Python 2.7.6
$python3.5 -m venv myvenv_foo # Create a new venv from 'python3.5'.
$source myvenv_foo/bin/activate # Activate venv.
$python -V # The *clean* 'python' command is now bound to your activated venv.
Python 3.5.2
$deactivate # Deactivate venv.
$python -V # Now the *clean* command is bound back to the main version.
Python 2.7.6