Python virtualenv问题 [英] Python virtualenv questions

查看:94
本文介绍了Python virtualenv问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows XP上使用VirtualEnv.我想知道我的大脑是否被正确包裹:

我运行了virtualenv ENV,它创建了C:\WINDOWS\system32\ENV.然后,我将PATH变量更改为包括C:\WINDOWS\system32\ENV\Scripts而不是C:\Python27\Scripts.然后,我将Django检出到C:\WINDOWS\system32\ENV\Lib\site-packages\django-trunk中,更新了PYTHON_PATH变量以指向新的Django目录,然后继续执行easy_install其他操作(当然,这些操作都进入了我的新C:\WINDOWS\system32\ENV\Lib\site-packages目录中).

我了解为什么我应该使用VirtualEnv,以便可以在同一台计算机上运行Django的多个版本以及其他库,但这是否意味着要在环境之间进行切换,我基本上必须更改我的PATHPYTHON_PATH变量?因此,我从开发一个在ENV环境中使用Django 1.2的Django项目开始,然后更改我的PATH,这样我就可以使用一个名为ENV2的环境,该环境具有Django的开发版本? >

基本上就是这样,还是有更好的方法来自动完成所有这些操作(我可以在Python代码中更新路径,但这需要我在应用程序中编写机器特定的代码)?

此外,与在Linux上使用VirtualEnv(我是Linux的初学者)相比,此过程如何.

解决方案

通常virtualenv在当前目录中创建环境.除非出于某种原因打算在C:\Windows\system32中创建虚拟环境,否则我将对环境使用其他目录.

您不需要弄乱路径:使用activate脚本(在<env>\Scripts中)确保Python可执行文件和路径是特定于环境的.完成此操作后,命令提示符将更改以指示环境.然后,您可以仅调用easy_install,并将通过这种方式安装的任何内容安装到此环境中.使用deactivate将所有内容恢复为激活前的状态.

示例:

c:\Temp>virtualenv myenv
New python executable in myenv\Scripts\python.exe
Installing setuptools..................done.
c:\Temp>myenv\Scripts\activate
(myenv) C:\Temp>deactivate
C:\Temp>

请注意,我不需要为deactivate指定路径-activate会为您执行该操作,因此,激活后,"Python"将在virtualenv中运行Python,而不是系统Python. (尝试一下-进行import sys; sys.prefix,它将显示您环境的根.)

您只需激活一个新环境即可在环境/项目之间切换,但是您需要为activate指定整个路径,以便它知道要激活哪个环境.您永远不需要显式地混淆PATH或PYTHONPATH.

如果使用Windows Powershell,则可以利用包装器.在Linux上,virtualenvwrapper(指向该端口的链接指向Powershell)使virtualenv的使用变得更加轻松.

更新:完全没有错,但可能并不完全符合virtualenv的精神.您可以采取不同的方法:例如,如果您在virtualenv中安装了Django以及您站点所需的其他任何东西,则可以在激活virtualenv的情况下在项目目录中(正在开发站点的地方)工作.因为它已被激活,所以您的Python会找到Django以及您可以轻松安装到虚拟环境中的任何其他内容:并且由于您在项目目录中工作,因此项目文件也将对Python可见.

进一步的更新:您应该能够使用pipdistribute而不是setuptools,并且仅将python setup.py installvirtualenv一起使用.只需确保已激活环境,然后再向其中安装内容即可.

I'm using VirtualEnv on Windows XP. I'm wondering if I have my brain wrapped around it correctly:

I ran virtualenv ENV and it created C:\WINDOWS\system32\ENV. I then changed my PATH variable to include C:\WINDOWS\system32\ENV\Scripts instead of C:\Python27\Scripts. Then, I checked out Django into C:\WINDOWS\system32\ENV\Lib\site-packages\django-trunk, updated my PYTHON_PATH variable to point the new Django directory, and continued to easy_install other things (which of course go into my new C:\WINDOWS\system32\ENV\Lib\site-packages directory).

I understand why I should use VirtualEnv so I can run multiple versions of Django, and other libraries on the same machine, but does this mean that to switch between environments I have to basically change my PATH and PYTHON_PATH variable? So, I go from developing one Django project which uses Django 1.2 in an environment called ENV and then change my PATH and such so that I can use an environment called ENV2 which has the dev version of Django?

Is that basically it, or is there some better way to automatically do all this (I could update my path in Python code, but that would require me to write machine-specific code in my application)?

Also, how does this process compare to using VirtualEnv on Linux (I'm quite the beginner at Linux).

解决方案

Normally virtualenv creates environments in the current directory. Unless you're intending to create virtual environments in C:\Windows\system32 for some reason, I would use a different directory for environments.

You shouldn't need to mess with paths: use the activate script (in <env>\Scripts) to ensure that the Python executable and path are environment-specific. Once you've done this, the command prompt changes to indicate the environment. You can then just invoke easy_install and whatever you install this way will be installed into this environment. Use deactivate to set everything back to how it was before activation.

Example:

c:\Temp>virtualenv myenv
New python executable in myenv\Scripts\python.exe
Installing setuptools..................done.
c:\Temp>myenv\Scripts\activate
(myenv) C:\Temp>deactivate
C:\Temp>

Notice how I didn't need to specify a path for deactivate - activate does that for you, so that when activated "Python" will run the Python in the virtualenv, not your system Python. (Try it - do an import sys; sys.prefix and it should print the root of your environment.)

You can just activate a new environment to switch between environments/projects, but you'll need to specify the whole path for activate so it knows which environment to activate. You shouldn't ever need to mess with PATH or PYTHONPATH explicitly.

If you use Windows Powershell then you can take advantage of a wrapper. On Linux, the virtualenvwrapper (the link points to a port of this to Powershell) makes life with virtualenv even easier.

Update: Not incorrect, exactly, but perhaps not quite in the spirit of virtualenv. You could take a different tack: for example, if you install Django and anything else you need for your site in your virtualenv, then you could work in your project directory (where you're developing your site) with the virtualenv activated. Because it was activated, your Python would find Django and anything else you'd easy_installed into the virtual environment: and because you're working in your project directory, your project files would be visible to Python, too.

Further update: You should be able to use pip, distribute instead of setuptools, and just plain python setup.py install with virtualenv. Just ensure you've activated an environment before installing something into it.

这篇关于Python virtualenv问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆