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


当前回答

很多答案都推荐以下方法,但是如果你通读了脚本的源代码,你就会发现它已经被弃用了。

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

如果你的pip也坏了,这也没用。

pip install setuptools

我发现我必须运行确保pip、setuptools和wheel是最新的命令,以使pip重新工作。

python -m pip install --upgrade pip setuptools wheel

其他回答

当我试图遵循OSX指南时,我偶然发现了这个答案。对我有用的是,在运行python get-pip之后,我还必须easy_install pip。这解决了根本无法运行pip的问题。我确实安装了一堆旧的macport软件。这可能有些矛盾。

在我的例子中,我最初安装了2个python版本,后来我删除了旧的版本。在创建虚拟环境时

virtualenv venv

是指已卸载的python

什么对我有用

python3 -m virtualenv venv

当你试图使用pip时也是如此。

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.

我使用CentOS 6.7,我的python刚刚从2.6.6升级到2.7.11,在尝试了这么多不同的答案后,最后下面的一个完成了工作:

sudo yum install python-devel

希望能帮助到有同样处境的人。

我在ubuntu 13.10上试图将rhodecode安装到virtualenv时看到过这个错误。对我来说,解决办法就是逃跑

pip install --upgrade setuptools
pip install --upgrade distribute 

在我运行easy_install rhodecode之前。