问题:没有名为pkg_resources的模块
我正在将Django应用程序部署到开发服务器,并且在运行时遇到此错误pip install -r requirements.txt
:
Traceback (most recent call last):
File "/var/www/mydir/virtualenvs/dev/bin/pip", line 5, in <module>
from pkg_resources import load_entry_point
ImportError: No module named pkg_resources
pkg_resources
似乎与一起分发setuptools
。最初,我认为可能不会将它安装到virtualenv中的Python,所以我setuptools 2.6
使用以下命令将了(与Python相同的版本)安装到virtualenv 中的Python站点软件包中:
sh setuptools-0.6c11-py2.6.egg --install-dir /var/www/mydir/virtualenvs/dev/lib/python2.6/site-packages
编辑:这只发生在virtualenv内部。如果我在virtualenv之外打开控制台,则pkg_resources
存在,但仍然出现相同的错误。
关于为什么pkg_resources
不在路上的任何想法?
I’m deploying a Django app to a dev server and am hitting this error when I run pip install -r requirements.txt
:
Traceback (most recent call last):
File "/var/www/mydir/virtualenvs/dev/bin/pip", line 5, in <module>
from pkg_resources import load_entry_point
ImportError: No module named pkg_resources
pkg_resources
appears to be distributed with setuptools
. Initially I thought this might not be installed to the Python in the virtualenv, so I installed setuptools 2.6
(same version as Python) to the Python site-packages in the virtualenv with the following command:
sh setuptools-0.6c11-py2.6.egg --install-dir /var/www/mydir/virtualenvs/dev/lib/python2.6/site-packages
EDIT: This only happens inside the virtualenv. If I open a console outside the virtualenv then pkg_resources
is present, but I am still getting the same error.
Any ideas as to why pkg_resources
is not on the path?
回答 0
2018年7月更新
现在大多数人都应该使用pip install setuptools
(可能与一起使用sudo
)。
有些人可能需要(重新)安装python-setuptools
通过他们的软件包管理的软件包(apt-get install
,yum install
,等)。
此问题可能高度取决于您的操作系统和开发环境。如果上述方法不适用于您,请参见下面的旧式/其他答案。
说明
此错误消息是由缺少/损坏的Python setuptools
软件包引起的。根据Matt M.的注释和setuptools问题#581,以下引用的引导脚本不再是推荐的安装方法。
如果仍然对任何人有帮助,引导脚本说明将保留在下面。
旧版答案
我ImportError
今天在尝试使用点子时遇到了同样的问题。不知何故,该setuptools
软件包已在我的Python环境中删除。
要解决此问题,请运行以下安装脚本setuptools
:
wget https://bootstrap.pypa.io/ez_setup.py -O - | python
(或者,如果您尚未wget
安装(例如OS X),请尝试
curl https://bootstrap.pypa.io/ez_setup.py | python
可能带有sudo
前缀。)
如果您使用的任何版本distribute
,或setuptools
0.6以下的版本,则必须先将其卸载。*
有关更多详细信息,请参见安装说明。
*如果您已经可以使用distribute
,则将其升级到“兼容性包装器” setuptools
可以更轻松地进行切换。但是,如果事情已经坏了,请不要尝试。
July 2018 Update
Most people should now use pip install setuptools
(possibly with sudo
).
Some may need to (re)install the python-setuptools
package via their package manager (apt-get install
, yum install
, etc.).
This issue can be highly dependent on your OS and dev environment. See the legacy/other answers below if the above isn’t working for you.
Explanation
This error message is caused by a missing/broken Python setuptools
package. Per Matt M.’s comment and setuptools issue #581, the bootstrap script referred to below is no longer the recommended installation method.
The bootstrap script instructions will remain below, in case it’s still helpful to anyone.
Legacy Answer
I encountered the same ImportError
today while trying to use pip. Somehow the setuptools
package had been deleted in my Python environment.
To fix the issue, run the setup script for setuptools
:
wget https://bootstrap.pypa.io/ez_setup.py -O - | python
(or if you don’t have wget
installed (e.g. OS X), try
curl https://bootstrap.pypa.io/ez_setup.py | python
possibly with sudo
prepended.)
If you have any version of distribute
, or any setuptools
below 0.6, you will have to uninstall it first.*
See Installation Instructions for further details.
* If you already have a working distribute
, upgrading it to the “compatibility wrapper” that switches you over to setuptools
is easier. But if things are already broken, don’t try that.
回答 1
sudo apt-get install --reinstall python-pkg-resources
在Debian中为我修复了该问题。似乎卸载某些.deb软件包(在我的情况下为扭曲集)已破坏python用于查找软件包的路径
sudo apt-get install --reinstall python-pkg-resources
fixed it for me in Debian. Seems like uninstalling some .deb packages (twisted set in my case) has broken the path python uses to find packages
回答 2
尝试在Ubuntu 13.10上将rhodecode安装到virtualenv时,我已经看到此错误。对我来说,解决方案是运行
pip install --upgrade setuptools
pip install --upgrade distribute
在运行easy_install rhodecode之前。
I have seen this error while trying to install rhodecode to a virtualenv on ubuntu 13.10. For me the solution was to run
pip install --upgrade setuptools
pip install --upgrade distribute
before I run easy_install rhodecode.
回答 3
这也发生在我身上。我认为,在virtualenv使用setuptools的情况下,如果requirements.txt包含“ distribute”条目,则会出现此问题。Pip将尝试修补setuptools以便为分发腾出空间,但不幸的是,它将失败一半。
一种简单的解决方案是删除当前的virtualenv,然后使用–distribute参数创建一个新的virtualenv。
如果使用virtualenvwrapper的示例:
$ deactivate
$ rmvirtualenv yourenv
$ mkvirtualenv yourenv --distribute
$ workon yourenv
$ pip install -r requirements.txt
It also happened to me. I think the problem will happen if the requirements.txt contains a “distribute” entry while the virtualenv uses setuptools. Pip will try to patch setuptools to make room for distribute, but unfortunately it will fail half way.
The easy solution is delete your current virtualenv then make a new virtualenv with –distribute argument.
An example if using virtualenvwrapper:
$ deactivate
$ rmvirtualenv yourenv
$ mkvirtualenv yourenv --distribute
$ workon yourenv
$ pip install -r requirements.txt
回答 4
在CentOS 6中,安装软件包python-setuptools对其进行了修复。
yum install python-setuptools
In CentOS 6 installing the package python-setuptools fixed it.
yum install python-setuptools
回答 5
我之前有这个错误,评分最高的答案给我一个错误,试图下载ez_setup.py
文件。我找到了另一个来源,因此您可以运行以下命令:
curl http://peak.telecommunity.com/dist/ez_setup.py | python
我发现还必须使用sudo
它才能使其正常工作,因此您可能需要运行:
sudo curl http://peak.telecommunity.com/dist/ez_setup.py | sudo python
我还创建了另一个位置,可以从以下位置下载脚本:
https://gist.github.com/ajtrichards/42e73562a89edb1039f3
I had this error earlier and the highest rated answer gave me an error trying to download the ez_setup.py
file. I found another source so you can run the command:
curl http://peak.telecommunity.com/dist/ez_setup.py | python
I found that I also had to use sudo
to get it working, so you may need to run:
sudo curl http://peak.telecommunity.com/dist/ez_setup.py | sudo python
I’ve also created another location that the script can be downloaded from:
https://gist.github.com/ajtrichards/42e73562a89edb1039f3
回答 6
在尝试了以下几个答案之后,与一位同事联系,在Ubuntu 16.04上为我工作的是:
pip install --force-reinstall -U setuptools
pip install --force-reinstall -U pip
就我而言,只有枕头3.1.1的旧版本有问题(枕头4.x正常工作),现在已解决!
After trying several of these answers, then reaching out to a colleague, what worked for me on Ubuntu 16.04 was:
pip install --force-reinstall -U setuptools
pip install --force-reinstall -U pip
In my case, it was only an old version of pillow 3.1.1 that was having trouble (pillow 4.x worked fine), and that’s now resolved!
回答 7
需要更多的须藤。然后使用easy_install安装pip。作品。
sudo wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
sudo easy_install pip
Needed a little bit more sudo. Then used easy_install to install pip. Works.
sudo wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
sudo easy_install pip
回答 8
我通过执行以下操作修复了virtualenv的错误:
从复制了pkg_resources.py
/Library/Python/2.7/site-packages/setuptools
至
/Library/Python/2.7/site-packages/
这可能是一个便宜的解决方法,但对我有用。
。
如果不存在安装工具,则可以通过键入尝试安装系统站点软件包virtualenv --system-site-packages /DESTINATION DIRECTORY
,将最后一部分更改为要安装到的目录。pkg_rousources.py
将在lib / python2.7 / site-packages中的该目录下
I fixed the error with virtualenv by doing this:
Copied pkg_resources.py from
/Library/Python/2.7/site-packages/setuptools
to
/Library/Python/2.7/site-packages/
This may be a cheap workaround, but it worked for me.
.
If setup tools doesn’t exist, you can try installing system-site-packages by typing virtualenv --system-site-packages /DESTINATION DIRECTORY
, changing the last part to be the directory you want to install to. pkg_rousources.py
will be under that directory in lib/python2.7/site-packages
回答 9
对我来说,导致此错误是因为我有一个名为“ site”的子目录!我不知道这是否是pip错误,但我从以下内容开始:
/some/dir/requirements.txt / some / dir / site /
pip install -r requirements.txt无法正常工作,出现上述错误!
将子文件夹从“ site”重命名为“ src”解决了该问题!也许pip正在寻找“网站包装”?疯。
For me, this error was being caused because I had a subdirectory called “site”! I don’t know if this is a pip bug or not, but I started with:
/some/dir/requirements.txt
/some/dir/site/
pip install -r requirements.txt wouldn’t work, giving me the above error!
renaming the subfolder from “site” to “src” fixed the problem! Maybe pip is looking for “site-packages”? Crazy.
回答 10
当我将我的virtualenv激活为不同于创建它的用户时,我遇到了这个问题。看来是权限问题。我在尝试@cwc的答案时发现了这一点,并在输出中看到了这一点:
Installing easy_install script to /path/env/bin
error: /path/env/bin/easy_install: Permission denied
切换回创建virtualenv的用户,然后运行原始pip install
命令没有任何问题。希望这可以帮助!
I had this problem when I had activated my virtualenv as a different user than the one who created it. It seems to be a permission problem. I discovered this when I tried the answer by @cwc and saw this in the output:
Installing easy_install script to /path/env/bin
error: /path/env/bin/easy_install: Permission denied
Switching back to the user that created the virtualenv, then running the original pip install
command went without problems. Hope this helps!
回答 11
我今天也有这个问题。我只在虚拟环境中遇到问题。
对我来说,解决方案是停用虚拟环境,删除后再使用pip卸载virtualenv并重新安装。之后,我为我的项目创建了一个新的虚拟环境,然后pip在虚拟环境中都能正常工作,就像在正常环境中一样。
I had this problem today as well. I only got the problem inside the virtual env.
The solution for me was deactivating the virtual env, deleting and then uninstalling virtualenv with pip and reinstalling it. After that I created a new virtual env for my project, then pip worked fine both inside the virtual environment as in the normal environment.
回答 12
Looks like they have moved away from bitbucket and are now on github (https://github.com/pypa/setuptools)
Command to run is:
wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
回答 13
对我来说,原来是上的权限问题site-packages
。由于这只是我的开发环境,因此我提出了权限,然后一切又重新开始了:
sudo chmod -R a+rwx /path/to/my/venv/lib/python2.7/site-packages/
For me, it turned out to be a permissions problem on site-packages
. Since it’s only my dev environment, I raised the permissions and everything is working again:
sudo chmod -R a+rwx /path/to/my/venv/lib/python2.7/site-packages/
回答 14
如果通过conda安装的应用程序遇到此问题,则解决方案(如此错误报告中所述)仅是使用以下命令安装安装工具:
conda install setuptools
If you are encountering this issue with an application installed via conda, the solution (as stated in this bug report) is simply to install setup-tools with:
conda install setuptools
回答 15
在Windows上,使用python 3.7,这对我有用:
pip install --upgrade setuptools --user
--user
将软件包安装在您的主目录中,该目录不需要管理员权限。
On Windows, with python 3.7, this worked for me:
pip install --upgrade setuptools --user
--user
installs packages in your home directory, which doesn’t require admin privileges.
回答 16
简单的解决方法是您可以使用conda升级setuptools或整个环境。(特别适用于Windows用户。)
conda upgrade -c anaconda setuptools
如果删除了setuptools,则需要再次安装setuptools。
conda install -c anaconda setuptools
如果所有方法均无效,则可以升级conda环境。但是我不建议您需要重新安装和卸载某些软件包,因为这样做会加剧这种情况。
the simple resoluition is that you can use conda to upgrade setuptools or entire enviroment. (Specially for windows user.)
conda upgrade -c anaconda setuptools
if the setuptools is removed, you need to install setuptools again.
conda install -c anaconda setuptools
if these all methodes doesn’t work, you can upgrade conda environement. But I do not recommend that you need to reinstall and uninstall some packages because after that it will exacerbate the situation.
回答 17
显然您缺少setuptools。某些virtualenv版本默认情况下使用分发而不是setuptools。--setuptools
在创建virtualenv时使用该选项,或者VIRTUALENV_SETUPTOOLS=1
在您的环境中设置。
Apparently you’re missing setuptools. Some virtualenv versions use distribute instead of setuptools by default. Use the --setuptools
option when creating the virtualenv or set the VIRTUALENV_SETUPTOOLS=1
in your environment.
回答 18
就我而言,我最初安装了2个python版本,后来又删除了较旧的版本。因此,在创建虚拟环境时
virtualenv venv
指的是卸载的python
什么对我有用
python3 -m virtualenv venv
当您尝试使用点子时也是如此。
In my case, I had 2 python versions installed initially and later I had deleted the older one. So while creating the virtual environment
virtualenv venv
was referring to the uninstalled python
What worked for me
python3 -m virtualenv venv
Same is true when you are trying to use pip.
回答 19
当我尝试遵循本OSX指南时,遇到了这个答案。对我python get-pip
有用的是,跑步后,我还必须easy_install pip
。这解决了根本无法运行点子的问题。我确实安装了一堆旧的Macport东西。那可能有冲突。
I came across this answer when I was trying to follow this guide for OSX. What worked for me was, after running python get-pip
, I had to ALSO easy_install pip
. That fixed the issue of not being able to run pip at all. I did have a bunch of old macport stuff installed. That may have conflicted.
回答 20
回答 21
只需setuptools
通过以下方式重新安装您的:
$ sudo wget https://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefefe74e
$ tar -zxvf setuptools-0.6c11.tar.gz
$ cd setuptools-0.6c11/
$ sudo python setup.py build
$ sudo python setup.py install
$ sudo pip install --upgrade setuptools
那么一切都会好起来的。
just reinstall your setuptools
by :
$ sudo wget https://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefefe74e
$ tar -zxvf setuptools-0.6c11.tar.gz
$ cd setuptools-0.6c11/
$ sudo python setup.py build
$ sudo python setup.py install
$ sudo pip install --upgrade setuptools
then everything will be fine.
回答 22
我使用CentOS 6.7,而我的python刚刚从2.6.6升级到2.7.11,在尝试了许多不同的答案之后,终于有以下一个工作了:
sudo yum install python-devel
希望能帮助同样情况的人。
I use CentOS 6.7, and my python was just upgrade from 2.6.6 to 2.7.11, after tried so many different answer, finally the following one does the job:
sudo yum install python-devel
Hope help someone in the same situation.
回答 23
回答 24
更新我的Ubuntu版本后,我遇到了这个问题。它似乎已经遍历并删除了我所有虚拟环境中的设置工具。
为了解决这个问题,我将虚拟环境重新安装回了目标目录。这清理了缺少的设置工具,并使一切重新运行。
例如:
~/RepoDir/TestProject$ virtualenv TestEnvironmentDir
I ran into this problem after updating my Ubuntu build. It seems to have gone through and removed set up tools in all of my virtual environments.
To remedy this I reinstalled the virtual environment back into the target directory. This cleaned up missing setup tools and got things running again.
e.g.:
~/RepoDir/TestProject$ virtualenv TestEnvironmentDir
回答 25
对我来说,一个很好的解决方法是使用--no-download
选项virtualenv(VIRTUALENV_NO_DOWNLOAD=1 tox
用于tox。)
For me a good fix was to use --no-download
option to virtualenv (VIRTUALENV_NO_DOWNLOAD=1 tox
for tox.)
回答 26
在Opensuse 42.1上,以下内容解决了此问题:
zypper in python-Pygments
On Opensuse 42.1 the following fixed this issue:
zypper in python-Pygments
回答 27
ImportError:没有名为pkg_resources的模块:解决方法是使用下面的命令重新安装python pip。
步骤:1登录到root用户。
sudo su root
步骤:2卸载python-pip软件包(如果存在)。
apt-get purge -y python-pip
步骤:3使用wget命令下载文件(在中下载文件pwd
)
wget https://bootstrap.pypa.io/get-pip.py
步骤:4运行python文件。
python ./get-pip.py
步骤:5 Finalic exicute安装命令。
apt-get install python-pip
注意:用户必须是root用户。
ImportError: No module named pkg_resources: the solution is to reinstall python pip using the following Command are under.
Step: 1 Login in root user.
sudo su root
Step: 2 Uninstall python-pip package if existing.
apt-get purge -y python-pip
Step: 3 Download files using wget command(File download in pwd
)
wget https://bootstrap.pypa.io/get-pip.py
Step: 4 Run python file.
python ./get-pip.py
Step: 5 Finaly exicute installation command.
apt-get install python-pip
Note: User must be root.
回答 28
我在Google App Engine环境中遇到了该错误。并pip install -t lib setuptools
解决了问题。
I experienced that error in my Google App Engine environment. And pip install -t lib setuptools
fixed the issue.
回答 29
如果您使用的是Python 3,则应使用pip3而不是pip。该命令看起来像$ pip3 install requirements.txt
If you are using Python 3, you should use pip3 instead of pip. The command looks like $ pip3 install requirements.txt