使用特定版本的 Python 创建 Windows Python virtualenv
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44165220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create Windows Python virtualenv with a specific version of Python
提问by Hack-R
I have multiple versions of Python. I need to create a virtualenv for my project and make sure that it's using Python 2.7.
我有多个版本的 Python。我需要为我的项目创建一个 virtualenv,并确保它使用 Python 2.7。
I've tried to accomplish this with the combination of this guidefor virtualenv
on Windows and this SO poston virtualenv
with a specific kind of Python.
我已经试过的组合来完成此本指南为virtualenv
Windows和此SO岗位上virtualenv
具有特定类型的Python。
Unfortunately it's not working, which is probably because the latter resource was written by someone using Linux.
不幸的是它不起作用,这可能是因为后一种资源是由使用 Linux 的人编写的。
Here's what I did:
这是我所做的:
C:\Python27\Scripts>pip install virtualenv You are using pip version
6.0.6, however version 9.0.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command. Collecting virtualenv Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
100% |################################| 1.8MB 3.7MB/s eta 0:00:01 Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0
C:\Python27\Scripts>pip install virtualenvwrapper-win You are using pip version 6.0.6, however version 9.0.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command. Collecting virtualenvwrapper-win Downloading virtualenvwrapper-win-1.2.1.zip Requirement already satisfied (use
--upgrade to upgrade): virtualenv in c:\python27\lib\site-packages (from virtualenvwrapper
-win) Installing collected packages: virtualenvwrapper-win Running setup.py install for virtualenvwrapper-win Successfully installed virtualenvwrapper-win-1.2.1
C:\Python27\Scripts>mkvirtualenv c:\users\hackr\Desktop\P27_D19 --python=C:\Python27\python.exe
'python.exe' is not recognized as an internal or external command, operable program or batch file.
'virtualenv.exe' is not recognized as an internal or external command, operable program or batch file.
'python.exe' is not recognized as an internal or external command, operable program or batch file.
'virtualenv.exe' 不是内部或外部命令,也不是可运行的程序或批处理文件。
Update:I just got this to work using plain virtualenv
instead of the recommended mkvirtualenv
command, which I assume is part of the wrapper they had me install. If someone would like to help me understand what happened and if there are any downfalls of not using the wrapper, that would be a good way to write up the answer.
更新:我只是使用普通命令virtualenv
而不是推荐的mkvirtualenv
命令让它工作,我认为这是他们让我安装的包装器的一部分。如果有人想帮助我了解发生了什么,以及如果不使用包装器有任何缺点,那将是写出答案的好方法。
采纳答案by CristiFati
If having multiple Pythonversions installed, it's best to always specify full paths (to be sure) and not rely on environment variables(at least it's how I do it):
如果安装了多个Python版本,最好始终指定完整路径(可以肯定)并且不依赖于环境变量(至少我是这样做的):
pip install virtualenv
: I do it like :"C:\Install\x64\Python\3.5.3\python.exe" -m pip install virtualenv
(don't mind the paths) to control the Pythoninstallation (same thing for virtualenvwrapper).
pip install virtualenv
:我这样做:("C:\Install\x64\Python\3.5.3\python.exe" -m pip install virtualenv
不介意路径)来控制Python安装(virtualenvwrapper 也是如此)。
I don't have a mkvirtualenvin my "%PYTHON_INSTALLATION_DIR%\Scripts"(e.g. "C:\Install\x64\Python\3.5.3\Scripts") folder, but (this is an example that I "crafted" now):
我没有mkvirtualenv在我的“%PYTHON_INSTALLATION_DIR%\脚本”(如“C:\安装\ 64 \ Python的\ 3.5.3 \脚本”)文件夹,但(这是一个例子,我“制作”了) :
"c:\Install\x64\Python\3.5.3\Scripts\virtualenv.exe" -p "c:\Install\x64\Python\2.7.13\python.exe" "c:\venvs\py2713"
"c:\Install\x64\Python\3.5.3\Scripts\virtualenv.exe" -p "c:\Install\x64\Python\2.7.13\python.exe" "c:\venvs\py2713"
From then on:
从那时起:
"c:\venvs\py2713\Scripts\activate.bat"
python
......
"c:\venvs\py2713\Scripts\activate.bat"
python
......
I know, it's kind of annoying to specify all those funky paths (on Lnxis soooo much easier), but at least it's safe. Anyway, after setting up the virtual environments, you can adjust your environment (%PATH%), so you don't have to specify full paths.
我知道,指定所有这些时髦的路径有点烦人(在Lnx上要容易得多),但至少它是安全的。无论如何,在设置虚拟环境后,您可以调整您的环境(%PATH%),因此您不必指定完整路径。
I noticed your comment while writing, and I must say that PyCharm(Professional Edition) is an excellent tool to work with Django(as a matter of fact with Pythonin general). If on the other hand, you go for Community Editionit's not quite so great (as expected). [SO]: Run/Debug a Django application's UnitTests from the mouse right click context menu in PyCharm Community Edition?is one of the painful issues that I had to deal with.
我在写作时注意到了你的评论,我必须说PyCharm(专业版)是一个与Django一起工作的优秀工具(实际上与Python一起工作)。另一方面,如果你选择社区版,它就不是那么好(如预期的那样)。[SO]:从 PyCharm 社区版中的鼠标右键单击上下文菜单运行/调试 Django 应用程序的单元测试?是我不得不处理的痛苦问题之一。
@EDIT0:
@编辑0:
Just now I installed virtualenvwrapper-win: "C:\Install\x64\Python\3.5.3\python.exe" -m pip install virtualenvwrapper-win
. Running mkvirtualenv, either by its full path or by adding its parent folder in %PATH%(mkvirtualenv a
), yielded the same error:
刚才我安装了virtualenvwrapper- win: "C:\Install\x64\Python\3.5.3\python.exe" -m pip install virtualenvwrapper-win
。通过完整路径或通过在%PATH%( ) 中添加其父文件夹运行mkvirtualenv,产生了相同的错误:mkvirtualenv a
'python.exe' is not recognized as an internal or external command, operable program or batch file.
'python.exe' 不是内部或外部命令,也不是可运行的程序或批处理文件。
but it created the VEnv(under %USERPROFILE%\Envs). So the error is benign. Anyway it can be fixed by either:
但它创建了VEnv(在%USERPROFILE%\ Envs下)。所以这个错误是良性的。无论如何,它可以通过以下任一方式修复:
- Setting %PYTHONHOME%
- Adding the path to python.exein %PATH%
- 设置%PYTHONHOME%
- 在%PATH% 中添加python.exe的路径
An additional step that I did, was setting %WORKON_HOME%to the folder where I want my VEnvsto be located. Note that the environment variables must be persisted (since I only did this for testing purposes I only set them in the cmdconsole that I used for this task), otherwise they'll have to be set every time you need to use these tools.
我所做的另一个步骤是将%WORKON_HOME%设置为我希望我的VEnvs所在的文件夹。请注意,必须保留环境变量(因为我这样做只是为了测试目的,所以我只在用于此任务的cmd控制台中设置它们),否则每次需要使用这些工具时都必须设置它们。
After that workon, lsvirtualenvworked like a charm (well, not as great as in Lnx, but close enough).
在那之后workon,lsvirtualenv像魅力一样工作(嗯,不像Lnx那样好,但足够接近)。
Note(about mkvirtualenvv1.2.1): It will use the default python.exe(see above), as a base for the new VEnv, it's not as flexible as virtualenv.exe(which accepts the -p/--pythonargument).
注意(关于mkvirtualenv v1.2.1):它将使用默认的python.exe(见上文),作为新VEnv 的基础,它不像virtualenv.exe(它接受-p/ --python参数)那么灵活.