简体   繁体   English

为什么我的virtualenv的pip列表包在我的lib / python2.7目录中?

[英]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. 在那里,我安装了诸如virtualenvvirtualenvwrapper类的东西,因为作为共享主机的客户,我无权访问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.

相关问题 当使用python2.7创建一个干净的virtualenv时,为什么pip使用全局包? - Why pip uses global packages when created a clean virtualenv using python2.7? 为什么要在我当前的virtualenv之外安装python包呢? - Why would pip install python packages outside of my current virtualenv? 当我的 virtualenv 有 python3.5 时,为什么 django 1.9 继续使用 python2.7? - Why does django 1.9 keeps using python2.7 when my virtualenv have python3.5? virtualenv中的python3仍使用/usr/lib/python2.7/dist-packages中的库 - python3 in a virtualenv still uses library in /usr/lib/python2.7/dist-packages 如何在Linux下的/usr/local/lib/python2.7/site-packages下安装Pip - how to Install Pip under /usr/local/lib/python2.7/site-packages in Linux “/lib/python2.7/site-packages/不支持.pth文件”在MacOS上安装pip - “/lib/python2.7/site-packages/ does NOT support .pth files” installing pip on MacOS 为什么Python 3在我的Python 2.7包目录中查找包? - Why is Python 3 looking in my Python 2.7 package directory for packages? 如果我使用pip3安装了virtualenv,为什么为什么要用Python2.7创建环境? - Why does virtualenv create the environments with Python2.7 if I installed it using pip3? virtualenv:无法创建“ /lib/python2.7”:权限被拒绝 - virtualenv: could not create '/lib/python2.7': Permission denied 没有VirtualEnv的Python2.7中的站点包 - Site-Packages in Python2.7 without VirtualEnv
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM