100

I installed pip using get-pip.py, but the following error happened after pip freeze gets executed:

    Exception:
    Traceback (most recent call last):
      File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 122, in main
        status = self.run(options, args)
      File "/usr/lib/python2.7/dist-packages/pip/commands/freeze.py", line 74, in run
        req = pip.FrozenRequirement.from_dist(dist, dependency_links, find_tags=find_tags)
      File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 299, in from_dist
        assert len(specs) == 1 and specs[0][0] == '=='
    AssertionError

    Storing debug log for failure in /home/simon/.pip/pip.log

I've checked that pip version 1.7 is the latest one. However, updating pip via pip install -U pip did not update my pip. How can I fix this problem?

    simon@simon-OptiPlex-780:~/Nightybuild$ pip --version
    pip 1.5.6 from /usr/lib/python2.7/dist-packages (python 2.7)
    simon@simon-OptiPlex-780:~/Nightybuild$ pip --version
    pip 1.5.6 from /usr/lib/python2.7/dist-packages (python 2.7)
    simon@simon-OptiPlex-780:~/Nightybuild$ pip install -U pip
    Downloading/unpacking pip
      Downloading pip-7.1.2-py2.py3-none-any.whl (1.1MB): 1.1MB downloaded
    Installing collected packages: pip
    Successfully installed pip
    Cleaning up...
    simon@simon-OptiPlex-780:~/Nightybuild$ pip --version
    pip 1.5.6 from /usr/lib/python2.7/dist-packages (python 2.7)
1
  • 2
    sudo -H pip3 install --upgrade pip
    – moldovean
    May 8, 2016 at 10:09

10 Answers 10

101

I usually just run the following commands to upgrade both pip2 (=pip by default) and pip3:

sudo -H pip3 install --upgrade pip
sudo -H pip2 install --upgrade pip

You must make sure that you upgrade the version (for Python 2 or 3), which you want to react on the command pip without number, last.

Also please note that this keeps the old packaged versions installed through apt-get or any other package manager, but adds new versions which have nothing to do with the system packages. The pip-installed packages will be preferred, but you should not remove the apt-get-installed ones either, because the package manager can't know that any pip version is installed otherwise.

4
  • 5
    Why is it important to set home in sudo -H? May 28, 2016 at 20:42
  • 13
    @Masi Because pip wants to write its downloaded files somewhere to cache them. It's implemented to place them in a subfolder of the current user's home directory. If you're running it without -H it will complain, because it detects that the currently set home directory (normal user) does not match the user it runs as (root). If it wrote its files in your home directory, they would be owned by root and not accessible to you as normal user any more. Therefore it does not cache the files in this case. It's not terrible to omit the -H as pip detects it and warns you.
    – Byte Commander
    May 28, 2016 at 20:47
  • 1
    Why do both the commands just overwrite the pip command? When I upgrade pip2 it doesn't upgrade the pip2 command, just pip. Oct 18, 2017 at 8:30
  • @Yep_It's_Me this is because pip is relative to which version of python you are calling. and is why his commands are pip2 & pip3 respectively call the appropriate version of python to update pip. Does that make sense?
    – JayRizzo
    Jan 20, 2019 at 4:30
66

I think the

pip install --upgrade pip

command does not work properly anymore. The correct command should be:

  • for Python 3:

    python3 -m pip install --upgrade pip
    
  • for Python 2:

    python2 -m pip install --upgrade pip
    

P.S. If you want to make sure your other Python packages are also up to date, follow the instructions here.

5
  • Gg @david-foerster
    – JayRizzo
    Jan 20, 2019 at 15:40
  • On Linux, just use pip install -U pip (see Upgrading pip)
    – Eido95
    Mar 24, 2019 at 9:53
  • 2
    I had problems when updating pip from root (in a Docker image) and this worked like a charm. Jun 6, 2019 at 15:29
  • 1
    Worked like a charm on Linux mint
    – HelloWorld
    Jun 13, 2019 at 13:01
  • No module named pip
    – B. Shea
    Feb 8, 2022 at 15:54
7

Go to the website https://pypi.python.org/pypi/pip.

Copy (or download) the source link (ends in .tar.gz).

For 9.0.1, the link is https://pypi.python.org/pypi?:action=show_md5&digest=35f01da33009719497f01a4ba69d63c9.

Installation procedure:

wget Link goes here
tar -xzvf pip-9.0.1.tar.gz
cd pip-9.0.1
sudo python3 setup.py install

The version should be changed to the latest version and the link can be updated with the latest version's link.

This should work.

2
  • this is the only thing that worked for me since everything else needs a connection to the server from the shell, which again fails with the same error. Aug 12, 2019 at 11:13
  • I have trouble with the last line. I get the error ImportError: No module named 'setuptools', but when I run pip install setuptools I get Requirement already satisfied: setuptools in... Aug 27, 2020 at 9:59
5

I think it's worth mentioning that what I'm explaining below is if you expect pip to point to Python 2 and pip3 to point to Python 3. The reason I mention this is because when you upgrade pip3, it also takes over the pip command as well. This is a somewhat strange convention because by default python points to 2.x and python3 points to 3.x. That being said...

If you want to have the latest versions of python 2.x pip and python 3.x pip3 coexist on the same machine (using pip for 2.x and pip3 for 3.x), you need to do the following:

sudo apt-get install python-pip python3-pip --yes
sudo python3 -m pip install pip --upgrade --force
sudo python -m pip install pip --upgrade --force # this line associates pip with Python 2

The other answers provided by others fail to mention that after running sudo pip3 install pip --upgrade you'll end up with the pip command installing packages in the python 3.x directories instead of the python 2.x directories.

Part of me thinks that we should just leave pip be after upgrading pip3 (even if it pip -> pip3), but there's a danger there that people already have an expectation that pip functions like python - both pointing to python 2.x. In other words, people are probably trained to use pip/python for python 2.x just like they are trained to use pip3/python3 for python 3.x.

4
  • 1
    This breaks pip for me ImportError: cannot import name 'main'.
    – hayd
    Jan 13, 2020 at 2:08
  • apt-get update && apt-get install python-pip -y && apt-get upgrade -y && python -m pip install pip --upgrade --force brought the solution for my python2 project. I would still try to stick to apt-get as much as possible, use pip only if not available in apt-get or if really needed, like here. apt-get automatically manages your versions in the best way. Mar 18, 2021 at 17:17
  • @hayd's problem occurs because python2 pip and python3 pip are both installed. That should be avoided, or if you do, you always need to run pip with sudo python3 -m or sudo python2 -m , do not trust your normal pip command anymore, it may default to a default version that you do not want. Mar 18, 2021 at 17:22
  • In a Python2 project, apt-get install --upgrade python-pip -y && python -m pip install --upgrade pip were enough, no --force needed. I am not even sure whether the --upgrade is needed, perhaps someone can check it. It does not harm either, I used it. I also changed as much package installers as possible to apt-get, see Command “python setup.py egg_info” failed with error code 1 in /tmp/pip-build-rnhk49o3/opencv-python/, perhaps that also solved it a bit. Mar 18, 2021 at 22:20
3

pip install -U pip

The shortest I know.

1
  • What does the -U stand for? Mar 18, 2021 at 17:30
2

Update the pip version using

sudo apt-get update

sudo apt install python3-pip
2

If pip install -U pip is used then pip first uninstalls itself and may hang up in the middle of the whole process. So it is safe to use:

python3 -m pip install -U pip for Python 3

python -m pip install -U pip for Python 2.7 (or any Python version if run from inside venv)

0

I face the same error and resolved it with the following commands.

sudo su root
apt-get purge -y python-pip
wget https://bootstrap.pypa.io/get-pip.py
python ./get-pip.py
apt-get install python-pip

ImportError: No module named packaging.version

1
  • apt-get purge -y python-pip worked for me Jul 25, 2019 at 6:53
0

Centos: First you need to install latest version of python (currently: python 3.8) link

[root@centos7 ~]# yum install gcc openssl-devel bzip2-devel libffi-devel -y
[root@centos7 ~]# curl -O https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
[root@centos7 ~]# tar -xzf Python-3.8.1.tgz
[root@centos7 ~]# cd Python-3.8.1/
[root@centos7 Python-3.8.1]# ./configure --enable-optimizations
[root@centos7 Python-3.8.1]# make altinstall

Now python3.8 is installed. Now u can install latest pip version (currently 21.1)

# python3.8 -m pip install --upgrade pip
# pip3.8 --version
# OUTPUT ==> pip 21.1 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)

For ubuntu - use apt-get instead of yum

0

Below command is to upgrade with the latest available pip version in ubuntu-

# python -m pip install --upgrade pip

For specific pip version i.e. 22.0.4 try below command-

# pip install pip==22.0.4
# pip --version
pip 22.0.4 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)

This way we can install any other available pip version.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .