igniteflow
igniteflow

Reputation: 9802

No module named pkg_resources

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?

Upvotes: 701

Views: 1096459

Answers (30)

Julio Batista Silva
Julio Batista Silva

Reputation: 2099

For people finding this question in 2025 or after:

Installing setuptools as proposed by most answers does work, but you should really replace pkg_resources with importlib.

For example, use

from importlib.metadata import version
version("pyjwt")

instead of

import pkg_resources
pkg_resources.get_distribution("pyjwt").version

Read more at https://setuptools.pypa.io/en/latest/pkg_resources.html

Upvotes: 1

Zihao Zheng
Zihao Zheng

Reputation: 1

I use conda virtual environment as well. And I use ROS to run the code. I've tried different versions setuptools (reinstall and install many times), but it did not work for me.

ImportError: cannot import name 'packaging' from 'pkg_resources' (/opt/ros/noetic/lib/python3/dist-packages/pkg_resources/init.py)

If I run which python, it shows the python in conda virtual environment.

/home/<use_name>/miniconda3/envs/ros_env/bin/python

So the problem is probably the python from the virtual environment try to find the package from the ros environment, which causes the problem. This can happen due to the way Python paths are set up.

Make sure that your Conda environment's Python paths are prioritized. You can modify the PYTHONPATH environment variable to prioritize your Conda environment's paths.

export PYTHONPATH="/home/<user_name>/miniconda3/envs/ros_env/lib/python3.8/site-packages:$PYTHONPATH"

Problem solved for me.

Upvotes: 0

joedragons
joedragons

Reputation: 2645

I caused this error (or very similar) by removing the "/Library/Python/2.7/site-packages/" directory from my machine thinking it was a local cache. Only easy_install stopped working (pip was fine).

None of the items listed here helped me but I was able to recover by downloading the latest from https://www.python.org/downloads/ and installing it. Luckily I was not on latest, as I believe otherwise I would have had to reinstall.

I suspect (but not 100% sure) I also needed to open a new terminal (or new tab in terminal) to have it work.

Upvotes: 1

Eugene Brown
Eugene Brown

Reputation: 4352

I encountered this error in a poetry environment.

Steps I took to resolve it:

  1. Add setuptools as a dev dependency with
poetry add --dev setuptools
  1. Install setuptools in the poetry env using
poetry run pip install setuptools

Upvotes: 12

Lu&#237;s Soares
Lu&#237;s Soares

Reputation: 6222

In my case, I did:

pip install -U poetry

Upvotes: -1

dineshsundaram
dineshsundaram

Reputation: 51

I ran into this problem after installing the latest Python version 3.10.4. Somehow, the setuptools package and pip were deleted.

I used the following command to resolve the issue : in [Windows]

py -m ensurepip --default-pip

Upvotes: 5

Meghraj pardesi
Meghraj pardesi

Reputation: 86

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

Upvotes: 6

Everett Toews
Everett Toews

Reputation: 10974

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

Upvotes: 17

weibin Wang
weibin Wang

Reputation: 41

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.

Upvotes: 4

David Jablonski
David Jablonski

Reputation: 151

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.

Upvotes: 5

Afsan Abdulali Gujarati
Afsan Abdulali Gujarati

Reputation: 1624

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.

Upvotes: 1

cwc
cwc

Reputation: 10475

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.

Upvotes: 936

Gabriel
Gabriel

Reputation: 42439

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

Upvotes: 2

Lucas
Lucas

Reputation: 11

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

Upvotes: 0

B Kasza
B Kasza

Reputation: 3

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.

Upvotes: 0

than7
than7

Reputation: 17

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

Upvotes: -2

ptim
ptim

Reputation: 15607

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!

Upvotes: 24

Murat &#199;orlu
Murat &#199;orlu

Reputation: 8555

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

Upvotes: 0

Tukadiya Ramesh
Tukadiya Ramesh

Reputation: 40

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.

Upvotes: 0

ajtrichards
ajtrichards

Reputation: 30565

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

Upvotes: 14

Tarion
Tarion

Reputation: 17164

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

Upvotes: 8

David Hamner
David Hamner

Reputation: 701

On Opensuse 42.1 the following fixed this issue:

zypper in python-Pygments

Upvotes: 0

Baczek
Baczek

Reputation: 1247

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

Upvotes: 0

Steve D.
Steve D.

Reputation: 271

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

Upvotes: 0

kat
kat

Reputation: 615

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/

Upvotes: 3

user13107
user13107

Reputation: 3489

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/)

Upvotes: 1

shadyhill
shadyhill

Reputation: 525

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

Upvotes: 2

shellbye
shellbye

Reputation: 4858

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.

Upvotes: 0

Scott Yang
Scott Yang

Reputation: 389

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.

Upvotes: 0

Paul
Paul

Reputation: 191

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

yum install python-setuptools

Upvotes: 19

Related Questions