问题:在virtualenv中使用不同的Python版本
我有一个目前使用python 2.5.4运行的Debian系统。我正确安装了virtualenv,一切正常。我是否可以将virtualenv与其他版本的Python一起使用?
我编译了Python 2.6.2,并希望将其与一些virtualenv一起使用。覆盖二进制文件是否足够?还是我必须更改有关库的某些内容?
I have a Debian system currently running with python 2.5.4. I got virtualenv properly installed, everything is working fine. Is there a possibility that I can use a virtualenv with a different version of Python?
I compiled Python 2.6.2 and would like to use it with some virtualenv. Is it enough to overwrite the binary file? Or do I have to change something in respect to the libraries?
回答 0
在创建virtualenv实例时,只需使用--python
(或short -p
)选项来指定要使用的Python可执行文件,例如:
virtualenv --python=/usr/bin/python2.6 <path/to/new/virtualenv/>
注意:对于Python 3.3或更高版本,请参阅下面的Aelfinn 答案。
Just use the --python
(or short -p
) option when creating your virtualenv instance to specify the Python executable you want to use, e.g.:
virtualenv --python=/usr/bin/python2.6 <path/to/new/virtualenv/>
N.B. For Python 3.3 or later, refer to The Aelfinn’s answer below.
回答 1
从Python 3开始,Python Docs建议使用以下命令创建虚拟环境:
python3 -m venv <myenvname>
请注意,venv
不允许使用其他版本的Python创建虚拟环境。为此,请安装并使用virtualenv
软件包。
过时的信息
该pyvenv
脚本可用于创建虚拟环境
pyvenv /path/to/new/virtual/environment
但自Python 3.6起已弃用。
Since Python 3, the Python Docs suggest creating the virtual environment with the following command:
python3 -m venv <myenvname>
Please note that venv
does not permit creating virtual environments with other versions of Python. For that, install and use the virtualenv
package.
Obsolete information
The pyvenv
script can be used to create a virtual environment
pyvenv /path/to/new/virtual/environment
but it has been deprecated since Python 3.6.
回答 2
这些是您在共享托管环境上时需要执行的步骤,需要从源代码安装和编译Python,然后venv
从您的Python版本创建。对于Python 2.7.9。您将按照以下方式进行操作:
mkdir ~/src
wget http://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar -zxvf Python-2.7.9.tgz
cd Python-2.7.9
mkdir ~/.localpython
./configure --prefix=$HOME/.localpython
make
make install
虚拟环境
cd ~/src
wget https://pypi.python.org/packages/5c/79/5dae7494b9f5ed061cff9a8ab8d6e1f02db352f3facf907d9eb614fb80e9/virtualenv-15.0.2.tar.gz#md5=0ed59863994daf1292827ffdbba80a63
tar -zxvf virtualenv-15.0.2.tar.gz
cd virtualenv-15.0.2/
~/.localpython/bin/python setup.py install
virtualenv ve -p $HOME/.localpython/bin/python2.7
source ve/bin/activate
自然,这可以适用于您想要复制工作和部署的确切环境的任何情况。
These are the steps you can follow when you are on a shared hosting environment and need to install & compile Python from source and then create venv
from your Python version. For Python 2.7.9. you would do something along these lines:
mkdir ~/src
wget http://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar -zxvf Python-2.7.9.tgz
cd Python-2.7.9
mkdir ~/.localpython
./configure --prefix=$HOME/.localpython
make
make install
virtual env
cd ~/src
wget https://pypi.python.org/packages/5c/79/5dae7494b9f5ed061cff9a8ab8d6e1f02db352f3facf907d9eb614fb80e9/virtualenv-15.0.2.tar.gz#md5=0ed59863994daf1292827ffdbba80a63
tar -zxvf virtualenv-15.0.2.tar.gz
cd virtualenv-15.0.2/
~/.localpython/bin/python setup.py install
virtualenv ve -p $HOME/.localpython/bin/python2.7
source ve/bin/activate
Naturally, this can be applicable to any situation where you want to replicate the exact environment you work and deploy on.
回答 3
virtualenv --python=/usr/bin/python2.6 <path/to/myvirtualenv>
virtualenv --python=/usr/bin/python2.6 <path/to/myvirtualenv>
回答 4
有一个更简单的方法,
virtualenv venv --python=python2.7
感谢您的评论,这仅在系统级别安装了python2.7的情况下才有效(例如/usr/bin/python2.7)。
否则,如果您使用自制软件,则可以使用该路径提供所需的内容。
virtualenv venv --python=/usr/local/bin/python
您可以使用以下命令找到python安装的路径
which python
这也适用于python 3。
which python3
>> /usr/local/bin/python3
virtualenv venv --python=/usr/local/bin/python3
最终凝结为:
virtualenv venv -p `which python`
virtualenv venv -p `which python3`
There is an easier way,
virtualenv venv --python=python2.7
Thanks to a comment, this only works if you have python2.7 installed at the system level (e.g. /usr/bin/python2.7).
Otherwise, if you are using homebrew you can use the path to give you what you want.
virtualenv venv --python=/usr/local/bin/python
You can find the path to your python installation with
which python
This will also work with python 3.
which python3
>> /usr/local/bin/python3
virtualenv venv --python=/usr/local/bin/python3
Ultimately condensing to:
virtualenv venv -p `which python`
virtualenv venv -p `which python3`
回答 5
在Windows下对我来说这有效:
virtualenv --python=c:\Python25\python.exe envname
没有python.exe
我WindowsError: [Error 5] Access is denied
我有安装virtualenv 1.6.1的Python2.7.1,我想要python 2.5.2。
Under Windows for me this works:
virtualenv --python=c:\Python25\python.exe envname
without the python.exe
I got WindowsError: [Error 5] Access is denied
I have Python2.7.1 installed with virtualenv 1.6.1, and I wanted python 2.5.2.
回答 6
Mac OSX 10.6.8(Snow Leopard):
1)当您这样做时pip install virtualenv
,pip命令与您的一个python版本相关联,virtualenv
并被安装到该python版本中。你可以做
$ which pip
看看是什么版本的python。如果您看到类似以下内容:
$ which pip
/usr/local/bin/pip
然后做:
$ ls -al /usr/local/bin/pip
lrwxrwxr-x 1 root admin 65 Apr 10 2015 /usr/local/bin/pip ->
../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pip
您可以在输出中看到python版本。
默认情况下,它将是用于您创建的任何新环境的python版本。但是,您可以使用以下命令指定计算机上安装的任何版本的python,以便在新环境中使用-p flag
:
$ virtualenv -p python3.2 my_env
Running virtualenv with interpreter /usr/local/bin/python3.2
New python executable in my_env/bin/python
Installing setuptools, pip...done.
virtualenv my_env
将在当前目录中创建一个文件夹,其中将包含Python可执行文件以及pip [command]的副本,可用于安装其他软件包。
http://docs.python-guide.org/en/latest/dev/virtualenvs/
virtualenv
只需将python从您计算机上的某个位置复制到新创建的my_env / bin /目录中。
2)系统python在中/usr/bin
,而我安装的各种python版本默认情况下安装在:
/usr/local/bin
3)我安装的各种python的名称都类似于python2.7
或python3.2
,我可以使用这些名称而不是完整路径。
========虚拟环境=========
1)我在使virtualenvwrapper正常工作时遇到一些问题。这就是我最后输入的内容~/.bash_profile
:
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/django_projects #Not very important -- mkproject command uses this
#Added the following based on:
#http://stackoverflow.com/questions/19665327/virtualenvwrapper-installation-snow-leopard-python
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.7
#source /usr/local/bin/virtualenvwrapper.sh
source /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh
2)-p option
与virtualenvwrapper 的工作原理不同:我必须指定要在新环境中使用的python解释器的完整路径(当我不想使用默认的python版本时):
$ mkvirtualenv -p /usr/local/bin/python3.2 my_env
Running virtualenv with interpreter /usr/local/bin/python3
New python executable in my_env/bin/python
Installing setuptools, pip...done.
Usage: source deactivate
removes the 'bin' directory of the environment activated with 'source
activate' from PATH.
与virtualenv不同,virtualenvwrapper将在$ WORKON_HOME环境变量指定的位置创建环境。这样可以将您所有的环境都放在一个地方。
Mac OSX 10.6.8 (Snow Leopard):
1) When you do pip install virtualenv
, the pip command is associated with one of your python versions, and virtualenv
gets installed into that version of python. You can do
$ which pip
to see what version of python that is. If you see something like:
$ which pip
/usr/local/bin/pip
then do:
$ ls -al /usr/local/bin/pip
lrwxrwxr-x 1 root admin 65 Apr 10 2015 /usr/local/bin/pip ->
../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pip
You can see the python version in the output.
By default, that will be the version of python that is used for any new environment you create. However, you can specify any version of python installed on your computer to use inside a new environment with the -p flag
:
$ virtualenv -p python3.2 my_env
Running virtualenv with interpreter /usr/local/bin/python3.2
New python executable in my_env/bin/python
Installing setuptools, pip...done.
virtualenv my_env
will create a folder in the current directory which
will contain the Python executable files, and a copy of the pip
[command] which you can use to install other packages.
http://docs.python-guide.org/en/latest/dev/virtualenvs/
virtualenv
just copies python from a location on your computer into the newly created my_env/bin/ directory.
2) The system python is in /usr/bin
, while the various python versions I installed were, by default, installed into:
/usr/local/bin
3) The various pythons I installed have names like python2.7
or python3.2
, and I can use those names rather than full paths.
========VIRTUALENVWRAPPER=========
1) I had some problems getting virtualenvwrapper to work. This is what I ended up putting in ~/.bash_profile
:
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/django_projects #Not very important -- mkproject command uses this
#Added the following based on:
#http://stackoverflow.com/questions/19665327/virtualenvwrapper-installation-snow-leopard-python
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.7
#source /usr/local/bin/virtualenvwrapper.sh
source /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh
2) The -p option
works differently with virtualenvwrapper: I have to specify the full path to the python interpreter to be used in the new environment(when I do not want to use the default python version):
$ mkvirtualenv -p /usr/local/bin/python3.2 my_env
Running virtualenv with interpreter /usr/local/bin/python3
New python executable in my_env/bin/python
Installing setuptools, pip...done.
Usage: source deactivate
removes the 'bin' directory of the environment activated with 'source
activate' from PATH.
Unlike virtualenv, virtualenvwrapper will create the environment at the location specified by the $WORKON_HOME environment variable. That keeps all your environments in one place.
回答 7
这两个命令应该可以正常工作。
virtualenv -p python2 myenv
(对于python2)
virtualenv -p python3 myenv
(对于python3)
These two commands should work fine.
virtualenv -p python2 myenv
(For python2)
virtualenv -p python3 myenv
(For python3)
回答 8
假设您当前在virtualenv中安装了python 2.7。但是要使用python3.2
,您必须使用以下命令进行更新:
$ virtualenv --python=/usr/bin/python3.2 name_of_your_virtualenv
然后通过以下方式激活您的virtualenv:
$ source activate name_of_your_virtualenv
然后python --version
在shell中执行:检查您的版本是否已更新。
Suppose you currently have python 2.7 installed in your virtualenv. But want to make use of python3.2
, You would have to update this with:
$ virtualenv --python=/usr/bin/python3.2 name_of_your_virtualenv
Then activate your virtualenv by:
$ source activate name_of_your_virtualenv
and then do: python --version
in shell to check whether your version is now updated.
回答 9
您可以virtualenv
使用所需的python版本进行调用。例如:
python3 -m virtualenv venv
或者,直接指向您的virtualenv路径。例如,对于Windows:
c:\Python34\Scripts\virtualenv.exe venv
并通过运行:
venv/bin/python
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
您可以看到虚拟环境中安装的python版本
You can call virtualenv
with python version you want. For example:
python3 -m virtualenv venv
Or alternatively directly point to your virtualenv path. e.g. for windows:
c:\Python34\Scripts\virtualenv.exe venv
And by running:
venv/bin/python
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
you can see the python version installed in virtual environment
回答 10
该-p
方法效果很好,但是您必须记住每次都使用它。如果您的目标是通常切换到较新版本的Python,那很痛苦,而且还可能导致错误。
您的另一种选择是设置一个环境变量,其作用与相同-p
。通过~/.bashrc
文件或在登录会话中管理环境变量的任何位置进行设置:
export VIRTUALENV_PYTHON=/path/to/desired/version
然后virtualenv
,只要您未-p
在命令行上指定,就可以使用它。
The -p
approach works well, but you do have to remember to use it every time. If your goal is to switch to a newer version of Python generally, that’s a pain and can also lead to mistakes.
Your other option is to set an environment variable that does the same thing as -p
. Set this via your ~/.bashrc
file or wherever you manage environment variables for your login sessions:
export VIRTUALENV_PYTHON=/path/to/desired/version
Then virtualenv
will use that any time you don’t specify -p
on the command line.
回答 11
通过使用命令替换为您找到python2甚至更加容易:
virtualenv -p $(which python2) <path/to/new/virtualenv/>
或者在使用virtualenvwrapper时:
mkvirtualenv -p $(which python2) <env_name>
Even easier, by using command substitution to find python2 for you:
virtualenv -p $(which python2) <path/to/new/virtualenv/>
Or when using virtualenvwrapper :
mkvirtualenv -p $(which python2) <env_name>
回答 12
对于Mac(High Sierra),在python3上安装virtualenv并为python2创建virtualenv:
$ python3 -m pip install virtualenv
$ python3 -m virtualenv --python=python2 vp27
$ source vp27/bin/activate
(vp27)$ python --version
Python 2.7.14
For Mac(High Sierra), install the virtualenv on python3 and create a virtualenv for python2:
$ python3 -m pip install virtualenv
$ python3 -m virtualenv --python=python2 vp27
$ source vp27/bin/activate
(vp27)$ python --version
Python 2.7.14
回答 13
在Mac上,我使用pyenv和virtualenvwrapper。我必须创建一个新的virtualenv。您需要自制软件,如果您使用的是Mac,我假设您已经安装了自制软件,但这只是为了好玩:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install pyenv
pyenv install 2.7.10
pyenv global 2.7.10
export PATH=/Users/{USERNAME}/.pyenv/versions/2.7.10/bin:$PATH
mkvirtualenv -p ~/.pyenv/versions/2.7.10/bin/python {virtual_env_name}
我也首先冻结了我的需求,因此我可以使用以下命令重新安装在新的virtualenv中:
pip install -r requirements.txt
On the mac I use pyenv and virtualenvwrapper. I had to create a new virtualenv. You need homebrew which I’ll assume you’ve installed if you’re on a mac, but just for fun:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install pyenv
pyenv install 2.7.10
pyenv global 2.7.10
export PATH=/Users/{USERNAME}/.pyenv/versions/2.7.10/bin:$PATH
mkvirtualenv -p ~/.pyenv/versions/2.7.10/bin/python {virtual_env_name}
I also froze my requirements first so i could simply reinstall in the new virtualenv with:
pip install -r requirements.txt
回答 14
[2019年11月]我需要在基于Python 3.8的Arch Linux系统上安装Python 3.7环境(env)。Python 3.7不再存在于系统上,因此我无法降级Python以安装所需的软件包。
此外,我想在虚拟环境(venv)中使用该软件包/ Python 3.7。这就是我做的。
下载Python版本的源文件:
我从下载了Python 3.7.4源文件
https://www.python.org/downloads/source/
至
/mnt/Vancouver/apps/python_versions/src/Python-3.7.4.tgz
然后,我将该存档(源文件)提取到
/mnt/Vancouver/apps/python_versions/src/Python-3.7.4/
安装:
[注意:在我的系统中是env,不是venv。]
cd /mnt/Vancouver/apps/python_versions/src/Python-3.7.4/
time ./configure ## 17 sec
time make ## 1 min 51 sec
time sudo make install ## 18 sec
time make clean ## 0.3 sec
检查已安装的Python版本:
$ which python
/usr/bin/python
$ python --version
Python 3.8.0
$ which python3.7
/usr/local/bin/python3.7
$ python ## Python 3.8 [system / env]
Python 3.8.0 (default, Oct 23 2019, 18:51:26)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ python3.7 ## newly-installed Python 3.7 package
Python 3.7.4 (default, Nov 20 2019, 11:36:53)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.7.4 (default, Nov 20 2019, 11:36:53)
[GCC 9.2.0]
>>>
$ python3.7 --version
Python 3.7.4
如何为特定的Python版本创建venv:
https://docs.python.org/3/tutorial/venv.html
12.2。创建虚拟环境
用于创建和管理虚拟环境的模块称为venv
。venv
通常会安装可用的最新版本的Python。如果您的系统上有多个Python版本,则可以通过运行python3或所需的任意版本来选择特定的Python版本。
要创建虚拟环境,请确定要放置目录的目录,然后使用带有目录路径的脚本运行venv模块:
python3 -m venv tutorial-env
tutorial-env
如果该目录不存在,它将创建一个目录,并在其中创建包含Python解释器,标准库和各种支持文件的副本的目录。…
在[Python 3.8作业环境/系统上]建立Python 3.7 venv:
python3.7 -m venv ~/venv/py3.7 ## create Python 3.7-based venv
source ~/venv/py3.7/bin/activate ## activate that venv
deactivate ## deactivate that venv (when done, there)
添加到~/.bashrc
:
alias p37='echo " [Python 3.7 venv (source ~/venv/py3.7/bin/activate)]" && source ~/venv/py3.7/bin/activate'
测试Python 3.7 venv:
$ p37
[Python 3.7 venv (source ~/venv/py3.7/bin/activate)]
(py3.7)$ python --version
Python 3.7.4
(py3.7)$ python
Python 3.7.4 (default, Nov 20 2019, 11:36:53)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.7.4 (default, Nov 20 2019, 11:36:53)
[GCC 9.2.0]
>>>
[November 2019] I needed to install a Python 3.7 environment (env) on my Python 3.8-based Arch Linux system. Python 3.7 was no longer on the system, so I could not downgrade Python, to install a package that I needed.
Furthermore, I wanted to use that package / Python 3.7 inside a virtual environment (venv). This is how I did it.
Download Python version source files:
I downloaded the Python 3.7.4 source files from
https://www.python.org/downloads/source/
to
/mnt/Vancouver/apps/python_versions/src/Python-3.7.4.tgz
I then extracted that archive (source files) to
/mnt/Vancouver/apps/python_versions/src/Python-3.7.4/
Installation:
[Note: in my system env, not a venv.]
cd /mnt/Vancouver/apps/python_versions/src/Python-3.7.4/
time ./configure ## 17 sec
time make ## 1 min 51 sec
time sudo make install ## 18 sec
time make clean ## 0.3 sec
Examine installed Python versions:
$ which python
/usr/bin/python
$ python --version
Python 3.8.0
$ which python3.7
/usr/local/bin/python3.7
$ python ## Python 3.8 [system / env]
Python 3.8.0 (default, Oct 23 2019, 18:51:26)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ python3.7 ## newly-installed Python 3.7 package
Python 3.7.4 (default, Nov 20 2019, 11:36:53)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.7.4 (default, Nov 20 2019, 11:36:53)
[GCC 9.2.0]
>>>
$ python3.7 --version
Python 3.7.4
How to create a venv for a specific Python version:
https://docs.python.org/3/tutorial/venv.html
12.2. CREATING VIRTUAL ENVIRONMENTS
The module used to create and manage virtual environments is called venv
. venv
will usually install the most recent version of Python that you have available. If you have multiple versions of Python on your system, you can select a specific Python version by running python3 or whichever version you want.
To create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path:
python3 -m venv tutorial-env
This will create the tutorial-env
directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files.
…
Create Python 3.7 venv [on a Python 3.8 operating env / system]:
python3.7 -m venv ~/venv/py3.7 ## create Python 3.7-based venv
source ~/venv/py3.7/bin/activate ## activate that venv
deactivate ## deactivate that venv (when done, there)
Added to ~/.bashrc
:
alias p37='echo " [Python 3.7 venv (source ~/venv/py3.7/bin/activate)]" && source ~/venv/py3.7/bin/activate'
Test Python 3.7 venv:
$ p37
[Python 3.7 venv (source ~/venv/py3.7/bin/activate)]
(py3.7)$ python --version
Python 3.7.4
(py3.7)$ python
Python 3.7.4 (default, Nov 20 2019, 11:36:53)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.7.4 (default, Nov 20 2019, 11:36:53)
[GCC 9.2.0]
>>>
回答 15
在Linux的Windows子系统中:
为python3创建环境:
virtualenv --python=/usr/bin/python3 env
激活它:
source env/bin/activate
In windows subsystem for linux:
Create environment for python3:
virtualenv --python=/usr/bin/python3 env
Activate it:
source env/bin/activate
回答 16
在Windows上:
py -3.4x32 -m venv venv34
要么
py -2.6.2 -m venv venv26
这将使用py
启动器,它将为您找到正确的python可执行文件(假设您已安装了它)。
On windows:
py -3.4x32 -m venv venv34
or
py -2.6.2 -m venv venv26
This uses the py
launcher which will find the right python executable for you (assuming you have it installed).
回答 17
是的,以上答案是正确的,并且在基于Linux和MAC OS X的基于Unix的系统上可以正常工作。
我试图为Python2和Python3创建virtualenv使用以下命令。
在这里,我已经使用venv2&venv3作为其名称Python2与Python3分别。
Python2»
MacBook-Pro-2:~ admin$ virtualenv venv2 --python=`which python2`
Running virtualenv with interpreter /usr/local/bin/python2
New python executable in /Users/admin/venv2/bin/python
Installing setuptools, pip, wheel...done.
MacBook-Pro-2:~ admin$
MacBook-Pro-2:~ admin$ ls venv2/bin/
activate easy_install pip2.7 python2.7
activate.csh easy_install-2.7 python wheel
activate.fish pip python-config
activate_this.py pip2 python2
MacBook-Pro-2:~ admin$
Python3»
MacBook-Pro-2:~ admin$ virtualenv venv3 --python=`which python3`
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/admin/venv3/bin/python3
Also creating executable in /Users/admin/venv3/bin/python
Installing setuptools, pip, wheel...done.
MacBook-Pro-2:~ admin$
MacBook-Pro-2:~ admin$ ls venv3/bin/
activate easy_install pip3.6 python3.6
activate.csh easy_install-3.6 python wheel
activate.fish pip python-config
activate_this.py pip3 python3
MacBook-Pro-2:~ admin$
检查Python安装位置
MacBook-Pro-2:~ admin$ which python2
/usr/local/bin/python2
MacBook-Pro-2:~ admin$
MacBook-Pro-2:~ admin$ which python3
/usr/local/bin/python3
MacBook-Pro-2:~ admin$
Yes, the above answers are correct and works fine on Unix based systems like Linux & MAC OS X.
I tried to create virtualenv for Python2 & Python3 with the following commands.
Here I have used venv2 & venv3 as their names for Python2 & Python3 respectively.
Python2 »
MacBook-Pro-2:~ admin$ virtualenv venv2 --python=`which python2`
Running virtualenv with interpreter /usr/local/bin/python2
New python executable in /Users/admin/venv2/bin/python
Installing setuptools, pip, wheel...done.
MacBook-Pro-2:~ admin$
MacBook-Pro-2:~ admin$ ls venv2/bin/
activate easy_install pip2.7 python2.7
activate.csh easy_install-2.7 python wheel
activate.fish pip python-config
activate_this.py pip2 python2
MacBook-Pro-2:~ admin$
Python3 »
MacBook-Pro-2:~ admin$ virtualenv venv3 --python=`which python3`
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/admin/venv3/bin/python3
Also creating executable in /Users/admin/venv3/bin/python
Installing setuptools, pip, wheel...done.
MacBook-Pro-2:~ admin$
MacBook-Pro-2:~ admin$ ls venv3/bin/
activate easy_install pip3.6 python3.6
activate.csh easy_install-3.6 python wheel
activate.fish pip python-config
activate_this.py pip3 python3
MacBook-Pro-2:~ admin$
Checking Python installation locations
MacBook-Pro-2:~ admin$ which python2
/usr/local/bin/python2
MacBook-Pro-2:~ admin$
MacBook-Pro-2:~ admin$ which python3
/usr/local/bin/python3
MacBook-Pro-2:~ admin$
回答 18
对我有用
sudo apt-get install python3-minimal
virtualenv --no-site-packages --distribute -p /usr/bin/python3 ~/.virtualenvs/py3
It worked for me
sudo apt-get install python3-minimal
virtualenv --no-site-packages --distribute -p /usr/bin/python3 ~/.virtualenvs/py3
回答 19
我使用pyenv来管理我的python版本。
pyenv install 3.7.3
pyenv local 3.7.3
检查您的python版本:
$ python --version
Python 3.7.3
使用venv创建虚拟环境:
python -m venv .
然后激活虚拟环境:
source bin/activate
检查您的python版本:
$ python --version
Python 3.7.3
您可能需要删除以前的虚拟环境
rm -rf bin
I use pyenv to manage my python version.
pyenv install 3.7.3
pyenv local 3.7.3
Check your python version:
$ python --version
Python 3.7.3
Create the virtual environment with venv:
python -m venv .
Then activate the Virtual Environment:
source bin/activate
Check your python version:
$ python --version
Python 3.7.3
You may need to remove the previous virtual environment
rm -rf bin
回答 20
对于Windows,这些似乎有些复杂。如果您使用的是运行python 3.3或更高版本的Windows,则可以使用python启动器py
更加轻松地执行此操作。只需安装不同的python版本,然后运行:
py -[my version] -m venv env
这将env
使用python在当前目录中创建一个虚拟环境[my version]
。举个例子:
py -3.7 -m venv env
./env/Scripts/activate
这将创建一个env
使用python3.7 的虚拟环境并将其激活。不需要路径或其他复杂的东西。
These seem a little overcomplicated for Windows. If you’re on Windows running python 3.3 or later, you can use the python launcher py
to do this much more easily. Simply install the different python version, then run:
py -[my version] -m venv env
This will create a virtual environment called env
in your current directory, using python [my version]
. As an example:
py -3.7 -m venv env
./env/Scripts/activate
This creates a virtual environment called env
using python3.7 and activates it. No paths or other complex stuff required.
回答 21
回答 22
对于2019年的Debian(debian 9)系统,我发现了一个简单的解决方案,可以从虚拟环境中解决问题。
假设虚拟环境是通过以下方式创建的:
python3.7 -m venv myenv
但只有python2
和的版本python2.7
,因此您需要python3.7的最新功能。
然后,只需运行以下命令:
(myvenv) $ python3.7 -m venv --upgrade /home/username/path/to/myvenv/
如果系统上已经提供python3.7软件包,则会添加这些软件包。
For Debian (debian 9) Systems in 2019, I discovered a simple solution that may solve the problem from within the virtual environment.
Suppose the virtual environment were created via:
python3.7 -m venv myenv
but only has versions of python2
and python2.7
, and you need the recent features of python3.7.
Then, simply running the command:
(myvenv) $ python3.7 -m venv --upgrade /home/username/path/to/myvenv/
will add python3.7 packages if they are already available on your system.
回答 23
这是virtualenv的错误。只需升级您的点子即可解决。
pip install --upgrade virtualenv
This was a bug with virtualenv.
Just upgrading your pip should be the fix.
pip install --upgrade virtualenv
回答 24
正如在多个答案中已经提到的那样,使用virtualenv是一个干净的解决方案。但是,每个人都应该注意的一个小陷阱是,如果在bash_aliases中设置了python的别名,例如:
python=python3.6
该别名也将在虚拟环境中使用。因此,在这种情况下,无论使用什么解释器创建环境,python -V
在虚拟环境中运行的始终将输出3.6
:
virtualenv venv --python=pythonX.X
As already mentioned in multiple answers, using virtualenv is a clean solution. However a small pitfall that everyone should be aware of is that if an alias for python is set in bash_aliases like:
python=python3.6
this alias will also be used inside the virtual environment. So in this scenario running python -V
inside the virtual env will always output 3.6
regardless of what interpreter is used to create the environment:
virtualenv venv --python=pythonX.X
回答 25
它在Windows上使用python 2安装对我有用:
- 第1步:安装python 3版本。
- 步骤2:为虚拟环境创建一个env文件夹。
- 第3步:c:\ Python37 \ python -m venv c:\ path \ to \ env。
这就是我在现有python 2安装上创建Python 3虚拟环境的方式。
It worked for me on windows with python 2 installation :
- Step 1: Install python 3 version .
- Step 2: create a env folder for
the virtual environment.
- Step 3 : c:\Python37\python -m venv
c:\path\to\env.
This is how i created Python 3 virtual environment on my existing python 2 installation.
回答 26
您可以这样做:
virtualenv -p python3 .
You can do it by doing this:
virtualenv -p python3 .
回答 27
是的,您只需要安装其他版本的python,并在命令中定义其他版本的python的位置,例如:
virtualenv / home / payroll / Documents / env -p / usr / bin / python3
Yes you just need to install the other version of python, and define the location of your other version of python in your command like :
virtualenv /home/payroll/Documents/env -p /usr/bin/python3
回答 28
这是如何在Visual Studio Code文件夹中创建虚拟环境
的分步说明:我使用了Powershell(管理员模式):1.我在要创建虚拟环境的地方创建了VSCode文件夹-“ D:\ Code_Python_VE”。
2.接下来,我键入命令-“ pip3 install virtualenv”。(D:\ Code_Python_VE> pip3安装virtualenv)3. D:\ Code_Python_VE> python3 -m venv project_env
4. D:\ Code_Python_VE> project_env \ Scripts \ activate.bat
5. D:\ Code_Python_VE> ls-这将列出一个新的目录“ project_env”。
6. D:\ Code_Python_VE>代码。这将启动Visual Studio代码。确保命令是(代码。)。
7.创建具有以下内容的launch.jason:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "python",
"request": "launch",
"name": "Python: Current File (Integrated Terminal 1)",
"program": "${file}"
},
{
"name": "Python: Current File (Integrated Terminal 2)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
(请搜索如何转到“调试”窗口并在VS Code中添加新配置)。
- 在Visual Studio代码中按F1键,命令托盘将打开-选择Python Interpreter,然后选择虚拟环境project_env。
- 使用一条语句print(“ Hello World”)添加test.py文件。
- 运行该程序。
- 在Visual Studio Code终端-
(project_env)d:\ Code_Python_VE> python -m pip install –upgrade
希望对您有所帮助。
Here is the stepbystep how to create the Virtual environment in Visual Studio Code folder:
I used Powershell (Administrator mode):
1. I create a VSCode folder – “D:\Code_Python_VE” where I want to create Virtual environment.
2. Next I type the command – “pip3 install virtualenv”. (D:\Code_Python_VE> pip3 install virtualenv)
3. D:\Code_Python_VE> python3 -m venv project_env
4. D:\Code_Python_VE>project_env\Scripts\activate.bat
5. D:\Code_Python_VE> ls – This will list a new directory “project_env”.
6. D:\Code_Python_VE> code . This will start Visual Studio Code. Make sure the command is (code .).
7. Create launch.jason with following content:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "python",
"request": "launch",
"name": "Python: Current File (Integrated Terminal 1)",
"program": "${file}"
},
{
"name": "Python: Current File (Integrated Terminal 2)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
(Please search how to go to Debug window and Add new Configuration in VS Code).
- Press F1 in Visual studio code and the command pallet will open – Select Python Interpreter and select the virtual environment project_env.
- Add test.py file with one statement print(“Hello World”).
- Run this program.
- In Visual studio Code terminal –
(project_env) d:\Code_Python_VE>python -m pip install –upgrade
I hope this helps.
回答 29
UBUNTU 19.04 /全球Python 3.7。
这对我有用,使用推荐的venv进行python 3开发,从而启用了python 3.8环境。
安装3.8和3.8 venv模块
$ sudo apt install python3.8 python3.8-venv ##以及您需要的任何其他模块
使用您要在该环境中创建的python版本创建虚拟环境
$ /usr/bin/python3.8 -m venv python38-env
切换到您的虚拟环境
$源python38-env / bin / activate
python -V = python 3.8
UBUNTU 19.04 / Global Python 3.7.
This worked for me, enabling a Python 3.8 environment using the recommended venv for python 3 development.
Install 3.8 and 3.8 venv module
$ sudo apt install python3.8 python3.8-venv ## plus any other modules you need
Create your Virtual Env using the python version you want in that env
$ /usr/bin/python3.8 -m venv python38-env
switch into your virtual env
$ source python38-env/bin/activate
python -V = python 3.8