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


当前回答

只需重新安装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

那么一切都会好起来的。

其他回答

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.

在windows上,我安装了从www.lfd.uci.edu/~gohlke/pythonlibs/下载的pip,然后遇到了这个问题。

所以我应该先安装setuptools(easy_install)。

我在诗歌环境中遇到了这个错误。

我采取的解决方法是:

添加setuptools作为开发依赖项

poetry add --dev setuptools

在诗词环境中安装setuptools

poetry run pip install setuptools

当我以不同于创建它的用户的身份激活virtualenv时,我遇到了这个问题。似乎是权限问题。当我尝试@cwc的答案并在输出中看到这一点时,我发现了这一点:

Installing easy_install script to /path/env/bin
error: /path/env/bin/easy_install: Permission denied

切换回创建virtualenv的用户,然后运行原来的pip install命令,没有任何问题。希望这能有所帮助!

在尝试了几个这样的答案后,然后联系了一位同事,在Ubuntu 16.04上对我有效的是:

pip install --force-reinstall -U setuptools
pip install --force-reinstall -U pip

在我的情况下,只有旧版本的枕头3.1.1有问题(枕头4。X工作正常),现在解决了!