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


当前回答

还需要一点sudo。然后使用easy_install安装pip。的工作原理。

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

其他回答

这种事也发生在我身上。我认为如果requirements.txt包含一个“分发”条目,而virtualenv使用setuptools,问题就会发生。Pip将尝试修补setuptools以为分发腾出空间,但不幸的是它会中途失败。

简单的解决方法是删除当前的virtualenv,然后使用——distribute参数创建一个新的virtualenv。

使用virtualenvwrapper的示例:

$ deactivate
$ rmvirtualenv yourenv
$ mkvirtualenv yourenv --distribute
$ workon yourenv
$ pip install -r requirements.txt

如果您使用的是Python 3,则应该使用pip3而不是pip。该命令类似于$ pip3 install requirements.txt

我今天也遇到了这个问题。我只在虚拟环境中遇到了问题。

我的解决方案是停用虚拟env,删除并使用pip卸载virtualenv,然后重新安装它。之后,我为我的项目创建了一个新的虚拟环境,然后pip在虚拟环境中工作得很好,就像在正常环境中一样。

您可以使用该命令 Sudo apt-get install—重新安装python3-pkg-resources 如果您正在使用python3,那么我就是这种情况。

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.