No module named pkg_resources

2018-12-31 14:14发布

I'm deploying a Django app to a dev server and am hitting this error when i run pip install 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 appears to be distributed with setuptools. Initially I thought this might not be installed to the python in the virtualenv so I installed setuptools (2.6, same version as Python) to the Python site-packages in the virtualenv with the following command

sh setuptools-0.6c11-py2.6.egg --install-dir /var/www/mydir/virtualenvs/dev/lib/python2.6/site-packages

EDIT This only happens inside the virtualenv. If I open a console outside the virtualenv then pkg_resources is present

but I am still getting the same error. Any ideas as to why pkg_resources is not on the path?

30条回答
伤终究还是伤i
2楼-- · 2018-12-31 14:53

I had this error earlier and the highest rated answer gave me an error trying to download the ez_setup.py file. I found another source so you can run the command:

curl http://peak.telecommunity.com/dist/ez_setup.py | python

I found that I also had to use sudo to get it working, so you may need to run:

sudo curl http://peak.telecommunity.com/dist/ez_setup.py | sudo python

I've also created another location that the script can be downloaded from:

https://gist.github.com/ajtrichards/42e73562a89edb1039f3

查看更多
君临天下
3楼-- · 2018-12-31 14:54

After trying several of these answers, then reaching out to a colleague, what worked for me on Ubuntu 16.04 was:

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

In my case, it was only an old version of pillow 3.1.1 that was having trouble (pillow 4.x worked fine), and that's now resolved!

查看更多
素衣白纱
4楼-- · 2018-12-31 14:55

I fixed the error with virtualenv by doing this:

Copied pkg_resources.py from

/Library/Python/2.7/site-packages/setuptools

to

/Library/Python/2.7/site-packages/

This may be a cheap workaround, but it worked for me.

.

If setup tools doesn't exist, you can try installing system-site-packages by typing virtualenv --system-site-packages /DESTINATION DIRECTORY, changing the last part to be the directory you want to install to. pkg_rousources.py will be under that directory in lib/python2.7/site-packages

查看更多
皆成旧梦
5楼-- · 2018-12-31 14:56

For me, it turned out to be a permissions problem on site-packages. Since it's only my dev environment, I raised the permissions and everything is working again:

sudo chmod -R a+rwx /path/to/my/venv/lib/python2.7/site-packages/
查看更多
路过你的时光
6楼-- · 2018-12-31 14:56

Apparently you're missing setuptools. Some virtualenv versions use distribute instead of setuptools by default. Use the --setuptools option when creating the virtualenv or set the VIRTUALENV_SETUPTOOLS=1 in your environment.

查看更多
妖精总统
7楼-- · 2018-12-31 14:58

On Opensuse 42.1 the following fixed this issue:

zypper in python-Pygments
查看更多
登录 后发表回答