487

I'm deploying a Django app to a dev server and am hitting this error when I run 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 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?

daaawx
  • 2,268
  • 2
  • 12
  • 13
igniteflow
  • 7,356
  • 9
  • 35
  • 44

35 Answers35

685

July 2018 Update

Most people should now use pip install setuptools (possibly with sudo).

Some may need to (re)install the python-setuptools package via their package manager (apt-get install, yum install, etc.).

This issue can be highly dependent on your OS and dev environment. See the legacy/other answers below if the above isn't working for you.

Explanation

This error message is caused by a missing/broken Python setuptools package. Per Matt M.'s comment and setuptools issue #581, the bootstrap script referred to below is no longer the recommended installation method.

The bootstrap script instructions will remain below, in case it's still helpful to anyone.

Legacy Answer

I encountered the same ImportError today while trying to use pip. Somehow the setuptools package had been deleted in my Python environment.

To fix the issue, run the setup script for setuptools:

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

(or if you don't have wget installed (e.g. OS X), try

curl https://bootstrap.pypa.io/ez_setup.py | python

possibly with sudo prepended.)

If you have any version of distribute, or any setuptools below 0.6, you will have to uninstall it first.*

See Installation Instructions for further details.


* If you already have a working distribute, upgrading it to the "compatibility wrapper" that switches you over to setuptools is easier. But if things are already broken, don't try that.

cwc
  • 7,957
  • 2
  • 18
  • 18
  • After reading this comment, I realized that I was using pip while at the same time updating my system packages (including several Python distribution packages) in the background. Turned out to be a bad idea... – Danilo Bargen Oct 18 '12 at 20:51
  • 42
    Piping curl to a local command always scares me, but this works great. :) I had run `sudo python` though YMMV. – Nik Reiman Dec 05 '12 at 10:49
  • @NikReiman I ran bin/python so it would install in my virtualenv. Worked great! – hannson Dec 10 '12 at 00:36
  • 5
    Under Cygwin I had to install the **python-setuptools** package. This got **easy_install** working again, which had suffered the same problem. Then I used `easy_install pip` to get pip working again. My problem arose from Cygwin upgrading Python from 2.6 to 2.7 behind my back while installing something unrelated. – Steve Pitchers Mar 21 '13 at 09:27
  • +1 saved me a re-install. I'd followed the updated `brew doctor` advice to empty out `/usr/local/share/python/` and unthinkingly extended this to deleting `easy_install`. This curl one liner fixed it all. – ricardo Jul 11 '13 at 00:52
  • 5
    This was the best answer last year… but as of mid-2013, `distribute` is obsolete, and `setuptools` is its successor. I'll edit the answer to update it, but I'm not sure that's the best way to handle it, so someone may revert the edit, in which case: Go [here](https://pypi.python.org/pypi/setuptools/0.9.8#installation-instructions). – abarnert Aug 16 '13 at 23:27
  • 1
    This URL didn't work for me but this one did: http://peak.telecommunity.com/dist/ez_setup.py – ajtrichards Apr 30 '14 at 19:25
  • 1
    @Cerin: It would be more helpful for everybody if you say what happened when you tried. And did you use `sudo`, as @Nik noted above? – Michael Scheper Aug 31 '14 at 14:38
  • 1
    I was getting this error when running `xattr` on mac. That uses the system python (and `python` for me was one I installed). So for me the command was `curl https://bootstrap.pypa.io/ez_setup.py | sudo /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python` – GP89 Jan 14 '15 at 16:07
  • 1
    Thanks, @StevePitchers, `sudo yum reinstall python-setuptools` solved the issue for me (without needing to pipe curl). Hit the problem updating via pip in the first place. – jozxyqk Feb 16 '15 at 09:53
  • 1
    It looks like that bitbucket repo has been moved to github. The new command would be wget https://raw.githubusercontent.com/pypa/setuptools/bootstrap/ez_setup.py -O - | python – Chris Belyeu May 18 '16 at 19:05
  • Awesome thanks! PS: in my case, the problem is in `pip3`, so I had to run with `python3`. – Aziz Alto Jun 30 '16 at 05:26
  • 1
    even with `sudo` i get the following error: `error: [Errno 13] Permission denied: '/Users//anaconda/lib/python3.5/site-packages/easy-install.pth' ` – McLawrence Apr 28 '17 at 12:41
  • [This method is deprecated](https://github.com/pypa/setuptools/issues/581). – wim Oct 03 '17 at 19:10
  • Distribute link is broken http://pythonhosted.org/setuptools/merge-faq.html – Daniel Springer Oct 08 '17 at 16:40
  • ez_setup.py is deprecated – milanchandna Nov 20 '17 at 09:00
  • 1
    `ez_setup.py is deprecated and when using it setuptools will be pinned to 33.1.1 since it's the last version that supports setuptools self upgrade/installation, check https://github.com/pypa/setuptools/issues/581 for more info; use pip to install setuptools` - part of the output from the command in this answer – Mattwmaster58 Jul 24 '18 at 05:05
  • 1
    after installing setuptools, this problem has been resolved. – pengchy Sep 03 '18 at 10:20
  • 1
    `apt-get install --reinstall python-setuptools ` works for me, an ubuntu 14.04 server. – user2545464 Apr 11 '19 at 00:51
  • I had a similar issue expect that the import caused the following error `AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'`, I solved it using `pip install --upgrade --force-reinstall setuptools`. – cglacet Apr 24 '19 at 11:57
  • on MacOS 10.15 I had to run: `pip3 install --upgrade setuptools --user` – Filip Happy Sep 16 '19 at 11:35
132
sudo apt-get install --reinstall python-pkg-resources

fixed it for me in Debian. Seems like uninstalling some .deb packages (twisted set in my case) has broken the path python uses to find packages

Marek
  • 1,489
  • 1
  • 8
  • 6
62

I have seen this error while trying to install rhodecode to a virtualenv on ubuntu 13.10. For me the solution was to run

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

before I run easy_install rhodecode.

Stephen Fuhry
  • 11,090
  • 6
  • 49
  • 52
Ali Akdurak
  • 3,561
  • 1
  • 16
  • 15
  • 14
    `pip install --upgrade setuptools` alone did the trick for me. – ryantuck Sep 23 '16 at 19:16
  • i needed `pip install --upgrade setuptools --user` – dfrankow Oct 20 '16 at 00:00
  • 6
    I can't run `pip install --upgrade setuptools` due to the same error: `No module named pkg_resources` – McLawrence Apr 28 '17 at 12:38
  • 1
    `pip install --upgrade distribute` did the job when using Python 3's venv module. Python 2's virtualenv supported `--distribute` flag, but one has to install `distribute` on environments created by `venv`. – Nirmal Sep 19 '17 at 13:24
37

It also happened to me. I think the problem will happen if the requirements.txt contains a "distribute" entry while the virtualenv uses setuptools. Pip will try to patch setuptools to make room for distribute, but unfortunately it will fail half way.

The easy solution is delete your current virtualenv then make a new virtualenv with --distribute argument.

An example if using virtualenvwrapper:

$ deactivate
$ rmvirtualenv yourenv
$ mkvirtualenv yourenv --distribute
$ workon yourenv
$ pip install -r requirements.txt
pram
  • 1,418
  • 13
  • 16
  • 2
    this helped me when none of the other solutions did (using Ubuntu 16.04) – MIkee Feb 26 '17 at 02:08
  • For environments created by Python 3's venv module, running `pip install -U distribute` after activating the environment will do the job. – Nirmal Sep 19 '17 at 13:26
17

In CentOS 6 installing the package python-setuptools fixed it.

yum install python-setuptools
Paul
  • 171
  • 1
  • 2
14

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

Tomasz Jakub Rup
  • 9,464
  • 7
  • 44
  • 47
ajtrichards
  • 26,991
  • 13
  • 84
  • 93
  • The "curl" command would not need sudo, so it could be: `curl http://peak.telecommunity.com/dist/ez_setup.py | sudo python` – tombrown52 Apr 20 '16 at 16:01
  • 1
    this answer saved me with "| sudo python" addition, because "| python" alone did not work! Thanks – patti_jane Jun 14 '16 at 22:03
14

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!

ptim
  • 12,355
  • 8
  • 70
  • 89
8

Needed a little bit more sudo. Then used easy_install to install pip. Works.

sudo wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
sudo easy_install pip
Tomasz Jakub Rup
  • 9,464
  • 7
  • 44
  • 47
Tarion
  • 14,013
  • 11
  • 61
  • 98
6

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

mrgnw
  • 1,724
  • 2
  • 15
  • 16
4

the simple resoluition is that you can use conda to upgrade setuptools or entire enviroment. (Specially for windows user.)

conda upgrade -c anaconda setuptools

if the setuptools is removed, you need to install setuptools again.

conda install -c anaconda setuptools

if these all methodes doesn't work, you can upgrade conda environement. But I do not recommend that you need to reinstall and uninstall some packages because after that it will exacerbate the situation.

3

For me, this error was being caused because I had a subdirectory called "site"! I don't know if this is a pip bug or not, but I started with:

/some/dir/requirements.txt /some/dir/site/

pip install -r requirements.txt wouldn't work, giving me the above error!

renaming the subfolder from "site" to "src" fixed the problem! Maybe pip is looking for "site-packages"? Crazy.

jdg
  • 2,101
  • 2
  • 17
  • 17
2

I had this problem when I had activated my virtualenv as a different user than the one who created it. It seems to be a permission problem. I discovered this when I tried the answer by @cwc and saw this in the output:

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

Switching back to the user that created the virtualenv, then running the original pip install command went without problems. Hope this helps!

Martijn de Milliano
  • 3,572
  • 3
  • 31
  • 43
2

I had this problem today as well. I only got the problem inside the virtual env.

The solution for me was deactivating the virtual env, deleting and then uninstalling virtualenv with pip and reinstalling it. After that I created a new virtual env for my project, then pip worked fine both inside the virtual environment as in the normal environment.

arno_v
  • 11,564
  • 2
  • 25
  • 30
2

Looks like they have moved away from bitbucket and are now on github (https://github.com/pypa/setuptools)

Command to run is:

wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
shadyhill
  • 465
  • 4
  • 12
  • File "/python/Python-3.5.1/Lib/urllib/request.py", line 162, in urlopen return opener.open(url, data, timeout) File "/python/Python-3.5.1/Lib/urllib/request.py", line 465, in open File "/python/Python-3.5.1/Lib/urllib/request.py", line 488, in _open 'unknown_open', req) File "/python/Python-3.5.1/Lib/urllib/request.py", line 443, in _call_chain result = func(*args) File "/python/Python-3.5.1/Lib/urllib/request.py", line 1310, in unknown_open raise URLError('unknown url type: %s' % type) urllib.error.URLError: – JDOaktown Jun 08 '16 at 02:15
2

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/
kat
  • 517
  • 4
  • 7
2

If you are encountering this issue with an application installed via conda, the solution (as stated in this bug report) is simply to install setup-tools with:

conda install setuptools
Gabriel
  • 32,750
  • 58
  • 187
  • 337
2

On Windows, with python 3.7, this worked for me:

pip install --upgrade setuptools --user

--user installs packages in your home directory, which doesn't require admin privileges.

Samuel Liew
  • 68,352
  • 105
  • 140
  • 225
1

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.

Danilo Bargen
  • 15,862
  • 15
  • 79
  • 111
1

In my case, I had 2 python versions installed initially and later I had deleted the older one. So while creating the virtual environment

virtualenv venv

was referring to the uninstalled python

What worked for me

python3 -m virtualenv venv

Same is true when you are trying to use pip.

1

A lot of answers are recommending the following but if you read through the source of that script, you'll realise it's deprecated.

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

If your pip is also broken, this won't work either.

pip install setuptools

I found I had to run the command from Ensure pip, setuptools, and wheel are up to date, to get pip working again.

python -m pip install --upgrade pip setuptools wheel
Everett Toews
  • 7,889
  • 9
  • 40
  • 42
0

I came across this answer when I was trying to follow this guide for OSX. What worked for me was, after running python get-pip, I had to ALSO easy_install pip. That fixed the issue of not being able to run pip at all. I did have a bunch of old macport stuff installed. That may have conflicted.

KitsuneYMG
  • 12,247
  • 3
  • 35
  • 57
0

On windows, I installed pip downloaded from www.lfd.uci.edu/~gohlke/pythonlibs/ then encontered this problem.

So I should have installed setuptools(easy_install) first.

thinker3
  • 11,218
  • 5
  • 26
  • 35
0

just reinstall your setuptools by :

$ 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

then everything will be fine.

Scott Yang
  • 151
  • 1
  • 6
0

I use CentOS 6.7, and my python was just upgrade from 2.6.6 to 2.7.11, after tried so many different answer, finally the following one does the job:

sudo yum install python-devel

Hope help someone in the same situation.

shellbye
  • 3,834
  • 2
  • 27
  • 39
0

None of the posted answers worked for me, so I reinstalled pip and it worked!

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

sudo easy_install pip 

pip install --upgrade setuptools

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

user13107
  • 2,719
  • 4
  • 29
  • 48
  • Traceback (most recent call last): File "/usr/bin/easy_install", line 5, in from pkg_resources import load_entry_point ImportError: No module named 'pkg_resources' – JDOaktown Jun 08 '16 at 02:13
  • owner-Dimension-3000 owner # sudo easy_install pip Traceback (most recent call last): File "/usr/bin/easy_install", line 5, in from pkg_resources import load_entry_point ImportError: No module named 'pkg_resources' owner-Dimension-3000 owner # pip install --upgrade setuptools Traceback (most recent call last): File "/usr/bin/pip", line 5, in from pkg_resources import load_entry_point ImportError: No module named 'pkg_resources' owner-Dimension-3000 owner # – JDOaktown Jun 08 '16 at 02:14
  • did you first remove installed pip version? – user13107 Jun 16 '16 at 09:28
0

I ran into this problem after updating my Ubuntu build. It seems to have gone through and removed set up tools in all of my virtual environments.

To remedy this I reinstalled the virtual environment back into the target directory. This cleaned up missing setup tools and got things running again.

e.g.:

~/RepoDir/TestProject$ virtualenv TestEnvironmentDir
Steve D.
  • 181
  • 1
  • 4
0

For me a good fix was to use --no-download option to virtualenv (VIRTUALENV_NO_DOWNLOAD=1 tox for tox.)

Baczek
  • 979
  • 1
  • 11
  • 20
0

On Opensuse 42.1 the following fixed this issue:

zypper in python-Pygments
David Hamner
  • 521
  • 4
  • 6
0

ImportError: No module named pkg_resources: the solution is to reinstall python pip using the following Command are under.

Step: 1 Login in root user.

sudo su root

Step: 2 Uninstall python-pip package if existing.

apt-get purge -y python-pip

Step: 3 Download files using wget command(File download in pwd )

wget https://bootstrap.pypa.io/get-pip.py

Step: 4 Run python file.

python ./get-pip.py

Step: 5 Finaly exicute installation command.

apt-get install python-pip

Note: User must be root.

0

I experienced that error in my Google App Engine environment. And pip install -t lib setuptools fixed the issue.

Murat Çorlu
  • 7,029
  • 4
  • 47
  • 73
0

If you are using Python 3, you should use pip3 instead of pip. The command looks like $ pip3 install requirements.txt

0

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.

Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399
B Kasza
  • 3
  • 3
0

yum -y install python-setuptools

i configure the Ceph there is a problem execute command "$ ceph-deploy new node1", and I execute the command "$ yum -y install python-setuptools", then the problem is gone.Thanks

Prabhat G
  • 2,768
  • 1
  • 17
  • 30
Lucas
  • 11
  • 1
0

It was all running good in my system. The moment i used- pip install virtualenv problems started happening. Probably it broke the path to the setup files

0

You can use the command sudo apt-get install --reinstall python3-pkg-resources if you are using python3 , this was the case with me.