在 Windows 上使用具有多个 Python 版本的 VirtualEnv [英] Using VirtualEnv with multiple Python versions on windows
问题描述
我的机器上有 python 2.7.6 和 3.4.0.2.7 版本在我的路上.我想使用 3.4 设置一个 virtualenv.在 SO 和其他地方有很多帖子建议我从命令提示符执行以下操作:
I have python 2.7.6 and 3.4.0 on my machine. The 2.7 version is on my path. I would like to set up a virtualenv using 3.4. There are many postings on SO and elsewhere that suggest I do the following from a command prompt:
virtualenv -p c:\python34 myvirtualenv
但这对我不起作用.控制台会话具有管理员权限并且 UAC 已关闭,但是我遇到了权限问题:
but this does not work for me. The console session has administrator privilege and UAC is off, however I get a permissions problem:
F:\virtualenv>virtualenv -p c:\python34 myenv
Running virtualenv with interpreter c:\python34
Traceback (most recent call last):
File "c:\python27\scripts\virtualenv-script.py", line 9, in <module>
load_entry_point('virtualenv==1.11', 'console_scripts', 'virtualenv')()
File "C:\Python27\lib\site-packages\virtualenv.py", line 779, in main
popen = subprocess.Popen([interpreter, file] + sys.argv[1:], env=env)
File "C:\Python27\lib\subprocess.py", line 709, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 957, in _execute_child
startupinfo)
WindowsError: [Error 5] Access is denied
我也尝试过专门指向 3.4 版本的 virtualenv,但在不改变路径的情况下,它最终执行了 2.7 和 3.4 python 文件的混合包.
I have also tried it specifically pointing to the 3.4 version of virtualenv, but without changing the path it ends up executing a mixed bag of 2.7 and 3.4 python files.
我能找到的设置虚拟环境的唯一方法是将我的路径更改为 3.4,运行 virtualenv,然后将我的路径重置为 2.7,这会破坏 virtualenv 上的 python 开关点.
The only way I could find to set up my virtual environment is to change my path to 3.4, run virtualenv then reset my path to 2.7, which defeats the point of the python switch on virtualenv.
谢谢
推荐答案
更好:
py -3.4 -m venv c:\path\to\wherever\you\want\it
如果您没有 py.exe
启动器(但应该安装它),您可以将 py -3.4
替换为 c:\Python34\python.exe
(假设默认位置)
If you don't have the py.exe
launcher (but it should be installed) you can replace py -3.4
with c:\Python34\python.exe
(assuming the default location)
这是因为有方便的、Windows 版本的、超级好的运行时选择器 py.exe
This works because of the handy-dandy, Windows-versioningest, super nice runtime picker py.exe
默认情况下,py.exe
将出现在 Windows 安装中(我认为它带有 2.7,我知道它带有 3+).当您运行 py
时,它会搜索一些环境变量,或者您可以使用特定的版本号覆盖它(在您的情况下为 -2.7
或 -3.4
) 您可以省略 .4
,它会选择最大"的次要版本号.
By default, py.exe
will be present on a Windows install (I think it comes with 2.7, I know it does with 3+). When you run py
then it will search for some environment variables or you can override that with a specific verison number (in your case -2.7
or -3.4
) You can leave off the .4
and it will choose the "biggest" minor version number.
您可以也使用它来运行 Python 脚本.如果你在你的脚本 #!python3
的顶部放置一个 hash-bang 行并将其命名为 py myscript.py
那么它会选择正确的 Python 版本开始, 通过搜索脚本的第一行并搜索版本号.
You can also use it to run Python scripts. If you put a hash-bang line at the top of your script #!python3
and call it py myscript.py
then it will pick the correct version of Python to start with, by searching the first line of the script and searching for a version number.
这很酷,因为您可以在脚本顶部放置类似 #!/usr/bin/env python3.4
的内容,然后使用 py
,或者在 linux 上做
This is cool, because you can put something like #!/usr/bin/env python3.4
in the top of your script and run it on Windows with py
, or on linux by doing
$ chmod +x myscript.py
$ ./myscript.py
很有用.
这篇关于在 Windows 上使用具有多个 Python 版本的 VirtualEnv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!