Linux Python virtualenv 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4527958/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:03:04  来源:igfitidea点击:

Python virtualenv questions

pythonwindowslinuxvirtualenv

提问by orokusaki

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

我在 Windows XP 上使用 VirtualEnv。我想知道我的大脑是否正确地缠绕在它周围:

I ran virtualenv ENVand it created C:\WINDOWS\system32\ENV. I then changed my PATHvariable to include C:\WINDOWS\system32\ENV\Scriptsinstead of C:\Python27\Scripts. Then, I checked out Django into C:\WINDOWS\system32\ENV\Lib\site-packages\django-trunk, updated my PYTHON_PATHvariable to point the new Django directory, and continued to easy_installother things (which of course go into my new C:\WINDOWS\system32\ENV\Lib\site-packagesdirectory).

我跑了virtualenv ENV,它创建了C:\WINDOWS\system32\ENV. 然后我将PATH变量更改为 includeC:\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目录)。

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 PATHand PYTHON_PATHvariable? So, I go from developing one Django project which uses Django 1.2 in an environment called ENVand then change my PATHand such so that I can use an environment called ENV2which has the dev version of Django?

我明白为什么我应该使用 VirtualEnv 以便我可以在同一台机器上运行多个版本的 Django 和其他库,但这是否意味着要在环境之间切换,我必须基本上改变我的PATHPYTHON_PATH变量?所以,我从开发一个 Django 项目开始,该项目在一个名为的环境中使用 Django 1.2 ENV,然后更改我的PATH等等,以便我可以使用一个名为的环境ENV2,该环境具有 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)?

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

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

另外,这个过程与在 Linux 上使用 VirtualEnv 相比如何(我是 Linux 的初学者)。

采纳答案by Vinay Sajip

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

通常virtualenv在当前目录中创建环境。除非您C:\Windows\system32出于某种原因打算在其中创建虚拟环境,否则我会为环境使用不同的目录。

You shouldn't need to mess with paths: use the activatescript (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 deactivateto set everything back to how it was before activation.

您不需要弄乱路径:使用activate脚本 (in <env>\Scripts) 来确保 Python 可执行文件和路径是特定于环境的。完成此操作后,命令提示符会更改以指示环境。然后您可以调用easy_install,并且您以这种方式安装的任何内容都将安装到此环境中。使用deactivate到一套一切回到它是怎么激活之前。

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- activatedoes 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.prefixand it should print the root of your environment.)

请注意,我不需要为deactivate-指定路径-activate为您指定路径,以便在激活“Python”时在 virtualenv 中运行 Python,而不是您的系统 Python。(试试看 - 做一个import sys; sys.prefix,它应该打印你环境的根。)

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

您可以只激活一个新环境来在环境/项目之间切换,但您需要指定整个路径,activate以便它知道要激活哪个环境。您永远不需要明确地弄乱 PATH 或 PYTHONPATH 。

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 virtualenveven easier.

如果您使用 Windows Powershell,那么您可以利用包装器。在 Linux 上,virtualenvwrapper(链接指向 Powershell 的端口)使生活virtualenv变得更加轻松。

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.

更新:不正确,完全正确,但可能不完全符合virtualenv. 您可以采取不同的策略:例如,如果您在 virtualenv 中安装了 Django 和您的站点所需的任何其他内容,那么您可以在激活 virtualenv 的情况下在您的项目目录(您正在开发站点的位置)中工作。因为它被激活了,你的 Python 会找到 Django 和你在虚拟环境中轻松安装的任何其他东西:并且因为你在你的项目目录中工作,你的项目文件对 Python 也是可见的。

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

进一步更新:您应该能够使用pip,distribute而不是setuptools,并且只是简单地python setup.py install使用virtualenv。只需确保在将某些内容安装到环境中之前已激活环境。

回答by mouad

Yes basically this is what virtualenv do , and this is what the activatecommand is for, from the doc here:

是的,基本上这就是 virtualenv 所做的,这就是activate命令的用途,来自此处的文档:

activate script

In a newly created virtualenv there will be a bin/activate shell script, or a Scripts/activate.bat batch file on Windows.

This will change your $PATH to point to the virtualenv bin/ directory. Unlike workingenv, this is all it does; it's a convenience. But if you use the complete path like /path/to/env/bin/python script.py you do not need to activate the environment first. You have to use source because it changes the environment in-place. After activating an environment you can use the function deactivate to undo the changes.

The activate script will also modify your shell prompt to indicate which environment is currently active.

激活脚本

在新创建的 virtualenv 中,将有一个 bin/activate shell 脚本,或 Windows 上的 Scripts/activate.bat 批处理文件。

这将更改您的 $PATH 以指向 virtualenv bin/ 目录。与workingenv 不同,这就是它所做的一切;这是一个方便。但是如果你使用像 /path/to/env/bin/python script.py 这样的完整路径,你不需要先激活环境。您必须使用源代码,因为它会就地更改环境。激活环境后,您可以使用功能停用来撤消更改。

activate 脚本还将修改您的 shell 提示以指示当前处于活动状态的环境。

so you should just use activatecommand which will do all that for you:

所以你应该只使用activate命令来为你做所有的事情:

> \path\to\env\bin\activate.bat

回答by Dimitris Kougioumtzis

in my project wsgi.py file i have this code (it works with virtualenv,django,apache2 in windows and python 3.4)

在我的项目 wsgi.py 文件中,我有这个代码(它适用于 windows 和 python 3.4 中的 virtualenv、django、apache2)

import os
import sys
DJANGO_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)),'..')
sys.path.append(DJANGO_PATH)
sys.path.append('c:/myproject/env/Scripts')
sys.path.append('c:/myproject/env/Lib/site-packages')
activate_this = 'c:/myproject/env/scripts/activate_this.py'
exec(open(activate_this).read())
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
application = get_wsgi_application()

in virtualhost file conf i have

在虚拟主机文件 conf 我有

<VirtualHost *:80>
    ServerName mysite
    WSGIScriptAlias / c:/myproject/myproject/myproject/wsgi.py
    DocumentRoot c:/myproject/myproject/
    <Directory  "c:/myproject/myproject/myproject/">
       Options +Indexes +FollowSymLinks +MultiViews
       AllowOverride All
      Require local
   </Directory>
</VirtualHost>

回答by Presh Onyee

on Windows I have python 3.7 installed and I still couldn't activate virtualenvfrom Gitbash with ./Scripts/activatealthough it worked from Powershell after running Set-ExecutionPolicy Unrestrictedin Powershell and changing the setting to "Yes To All".

在 Windows 上,我安装了 python 3.7,但我仍然无法virtualenv从 Gitbash激活,./Scripts/activate尽管它在 Powershell 中运行Set-ExecutionPolicy Unrestricted并将设置更改为“全部是”后从 Powershell 工作。

I don't like Powershell and I like to use Gitbash, so to activate virtualenvin Gitbash first navigate to your project folder, use lsto list the contents of the folder and be sure you see "Scripts". Change directory to "Scripts" using cd Scripts, once you're in the "Scripts" path use . activateto activate virtualenv. Don't forget the space after the dot.

我不喜欢 Powershell,我喜欢使用 Gitbash,所以要virtualenv在 Gitbash 中激活,首先导航到您的项目文件夹,使用ls列出文件夹的内容,并确保您看到“脚本”。使用 将目录更改为“脚本” cd Scripts,一旦您进入“脚本”路径,请使用. activate激活virtualenv. 不要忘记点后面的空格。

回答by Nitesh Kumar

After creating virtual environment copy the activate.bat file from Script folder of python and paste to it your environment and open cmd from your virtual environment and run activate.bat file.enter image description here

创建虚拟环境后,从 python 的 Script 文件夹中复制 activate.bat 文件并将其粘贴到您的环境中,然后从您的虚拟环境中打开 cmd 并运行 activate.bat 文件。在此处输入图片说明