我正在将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不在路径上有任何想法吗?


当前回答

2018年7月更新

大多数人现在应该使用pip install setuptools(可能与sudo一起使用)。

有些人可能需要通过他们的包管理器(apt-get install, yum install等)(重新)安装python-setuptools包。

此问题可能高度依赖于您的操作系统和开发环境。如果上面的方法对你不起作用,请参阅下面的遗产/其他答案。

解释

此错误消息是由Python setuptools包丢失/损坏引起的。马特·M。的注释和setuptools issue #581,下面提到的引导脚本不再是推荐的安装方法。

引导脚本说明将保留在下面,以防它对任何人都有帮助。

遗留的答案

我今天在尝试使用pip时遇到了同样的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。)

如果你有任何版本的分发,或任何setuptools低于0.6,你必须先卸载它

有关详细信息,请参阅安装说明。


*如果你已经有一个工作的发行版,升级到“兼容性包装”,切换到setuptools更容易。但如果事情已经坏了,就不要再尝试了。

其他回答

看起来他们已经离开bitbucket,现在在github (https://github.com/pypa/setuptools)

执行的命令为:

wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python

I have had the same problem when I used easy-install to install pip for python 2.7.14. For me the solution was (might not be the best, but worked for me, and this is probably the simplest) that the folder that contained the easy-install.py also contained a folder pkg_resources, and i have copy-pasted this folder into the same folder where my pip-script.py script was (python27\Scripts). Since then, I found it in the python27\Lib\site-packages\pip-9.0.1-py2.7.egg\pip\_vendor folder as well, it might be a better solution to modify the pip-script.py file to import this.

在Opensuse 42.1中,以下修复了此问题:

zypper in python-Pygments

2018年7月更新

大多数人现在应该使用pip install setuptools(可能与sudo一起使用)。

有些人可能需要通过他们的包管理器(apt-get install, yum install等)(重新)安装python-setuptools包。

此问题可能高度依赖于您的操作系统和开发环境。如果上面的方法对你不起作用,请参阅下面的遗产/其他答案。

解释

此错误消息是由Python setuptools包丢失/损坏引起的。马特·M。的注释和setuptools issue #581,下面提到的引导脚本不再是推荐的安装方法。

引导脚本说明将保留在下面,以防它对任何人都有帮助。

遗留的答案

我今天在尝试使用pip时遇到了同样的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。)

如果你有任何版本的分发,或任何setuptools低于0.6,你必须先卸载它

有关详细信息,请参阅安装说明。


*如果你已经有一个工作的发行版,升级到“兼容性包装”,切换到setuptools更容易。但如果事情已经坏了,就不要再尝试了。

简单的解决方法是使用conda来升级setuptools或整个环境。(windows用户专用)

conda upgrade -c anaconda setuptools

如果setuptools被移除,需要重新安装setuptools。

conda install -c anaconda setuptools

如果这些方法都不起作用,您可以升级conda环境。但我不建议你重新安装和卸载一些包,因为这将加剧情况。