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


当前回答

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最后执行安装命令。

apt-get install python-pip

备注:必须为root用户。

其他回答

如果你在通过conda安装的应用程序中遇到这个问题,解决方案(如此错误报告中所述)只是简单地安装安装工具:

conda install setuptools

没有一个张贴的答案对我有用,所以我重新安装pip,它工作了!

sudo apt-get install python-setuptools python-dev build-essential 

sudo easy_install pip 

pip install --upgrade setuptools

(参考:http://www.saltycrane.com/blog/2010/02/how-install-pip-ubuntu/)

显然你缺少setuptools。一些virtualenv版本默认使用distribute而不是setuptools。在创建virtualenv时使用——setuptools选项,或者在您的环境中设置VIRTUALENV_SETUPTOOLS=1。

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.

对我来说,一个很好的解决方案是使用virtualenv的no-download选项(VIRTUALENV_NO_DOWNLOAD=1 tox For tox)。