[英]Why is my virtualenv's pip listing packages in my lib/python2.7 directory?
In my home, I have a directory named lib/python2.7
(there are actually five directories like that, for different python versions). 在我的家中,我有一个名为
lib/python2.7
的目录(对于不同的python版本,实际上有五个目录)。 Since this is a shared hosting ( Webfaction ), that directory is fundamental to me. 由于这是一个共享托管( Webfaction ),因此该目录对我而言至关重要。 There, I have stuff like
virtualenv
and virtualenvwrapper
installed, since as customer of a shared hosting, I have no access to sudo
and installing global packages. 在那里,我安装了诸如
virtualenv
和virtualenvwrapper
类的东西,因为作为共享主机的客户,我无权访问sudo
和安装全局软件包。
However, when I create a virtualenv: 但是,当我创建一个virtualenv时:
$ mkvirtualenv myenvironment
$ workon myenvironment
$ which pip
# outputs the myenvironment's path to pip
$ pip freeze
The command shows the whole packages list under my lib/python2.7
(this includes the same virtualenv packages, and conflicting packages I have due to... legacy... reasons). 该命令在我的
lib/python2.7
下显示了整个软件包列表(这包括相同的virtualenv软件包,以及由于……传统……原因而导致的冲突软件包)。 This also annoys me if I want to install a package which is the name of a package in lib/python2.7
since it does not allow me to update it. 如果我要安装一个软件包(它是
lib/python2.7
中的软件包的名称),这也让我lib/python2.7
因为它不允许我对其进行更新。
Right inside the workon
environment, I try to check whether the PYTHONPATH has weird stuff, but it is empty: 在
workon
环境中,我尝试检查PYTHONPATH是否具有奇怪的内容,但是它是空的:
$ echo $PYTHONPATH
# shows a blank line
It is also empty if I try that command out of any virtual environment. 如果我在任何虚拟环境中尝试执行该命令,则它也是空的。
It seems that --no-site-packages
is default but solves just part of the problem. 似乎
--no-site-packages
是默认的,但只能解决部分问题。 This means: pip freeze | wc -l
这意味着:
pip freeze | wc -l
pip freeze | wc -l
displays a lesser value when in an environment than when executing globally, out of any environment, which tells me that there are certain already-provided packages that are being excluded and are from the hosting itself (and not installed by me since, again, the hosting is shared and I don't have access to global space). 在任何环境中,
pip freeze | wc -l
在环境中显示的值要小于全局执行时的值,这告诉我,某些已提供的软件包已被排除在主机之外(而我却未安装,因为,再次,托管是共享的 ,我无法访问全局空间)。
My question is: How can I solve this? 我的问题是:我该如何解决? I want my virtualenv not list the packages in
$HOME/lib/python2.7
我希望我的virtualenv不在
$HOME/lib/python2.7
列出软件包
Please avoid dupe-linking to this question , nothing was useful there and still does not have an accepted answer. 请避免对此问题进行重复链接,那里没有任何用处,但仍然没有可接受的答案。 I wrote this question after reading and trying each solution in that question
在阅读并尝试了该问题的每个解决方案后,我写了这个问题
I think you need to specify python version. 我认为您需要指定python版本。 You can specify python version with which you want to create virtual environment using command like
您可以使用以下命令指定要用于创建虚拟环境的python版本
virtualenv -p /usr/bin/python3.4 virt/virtname --no-site-packages
Because when you not specify a python version, virtualenv creates a environment with pythonv2.7 and hence all packages end up in the folder you mentioned. 因为当您不指定python版本时,virtualenv会使用pythonv2.7创建一个环境,因此所有软件包最终都位于您提到的文件夹中。
Found the solution after deeply digging. 经过深入挖掘,找到了解决方案。 This is a Webfaction custom but this could apply to any installation like this if the problem occurs.
这是一个Webfaction定制,但是如果出现问题,则可以将其应用于任何此类安装。
The core of the problem is that Webfaction configured a sitecustomize.py file for us . 问题的核心是Webfaction为我们配置了一个sitecustomize.py文件 。 The file is located at
/usr/local/lib/pythonX.Y/sitecustomize.py
and adds by itself the contents of ~/lib/pythonX.Y
(and conditionally the contents of any python app under ~/webapps
if you are working under a directory of it to run a python script). 该文件位于
/usr/local/lib/pythonX.Y/sitecustomize.py
并自行添加~/lib/pythonX.Y
的内容(如果有工作,则有条件地添加~/webapps
下任何python应用程序的内容在其目录下运行python脚本)。
Even when the virtualenv's python executable is a different one, it will load the said sitecustomize.py
file each time it runs as the base python executable does. 即使virtualenv的python可执行文件是一个不同的文件,它也会每次像基本python可执行文件一样运行时加载所述
sitecustomize.py
文件。
The workaround here? 解决方法在这里? Create an empty sitecustomize.py in your virtualenv to override the other:
在virtualenv中创建一个空的sitecustomize.py以覆盖另一个:
touch ~/.virtualenvs/yourvenv/lib/pythonX.Y/sitecustomize.py
And it will work. 它将起作用。 Take this as reference if you are stuck here like I was
如果您像我一样被困在这里,可以以此为参考
Notes : Replace XY on each case with the corresponding version you are working. 注意 :将每种情况下的XY替换为您正在使用的相应版本。 Additionally remember: You cannot remove or edit the base sitecustomize.py since you are in a shared hosting, in this case.
另外请记住: 在这种情况下,您不能删除或编辑基本sitecustomize.py,因为您位于共享主机中。 However, overriding will work for each case as long as you do this for every virtualenv you want.
但是,只要您对所需的每个virtualenv都执行覆盖,则覆盖将适用于每种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.