30

I just updated from 16.04 to 17.10, and every time I attempt to run any python2.7 command, I'm getting this error:

pip install --upgrade pip
Traceback (most recent call last):
File "/home/XXXX/bin/pip", line 7, in <module>
    from pip import main
  File "/home/XXXX/local/lib/python2.7/site-packages/pip/__init__.py", 
line 5, in <module>
    import logging
  File "/usr/lib/python2.7/logging/__init__.py", line 26, in <module>
    import sys, os, time, cStringIO, traceback, warnings, weakref, 
collections
  File "/home/XXXX/lib/python2.7/weakref.py", line 14, in <module>
    from _weakref import (
ImportError: cannot import name _remove_dead_weakref

Another user had a similar problem, but this affects any python program I attempt to execute. I can't install anything via pip, or use any python scripts. I really have no idea what to do, assistance would be greatly appreciated.

Output of "python --version" is

Python 2.7.12

however "update python" produces:

python is already the newest version (2.7.14-2ubuntu1).
2
  • I updated from 17.04 to 17.10 and facing the same ImportError: cannot import name _remove_dead_weakref. My Python is 2.7.14
    – Csaba Toth
    Jan 1, 2018 at 2:29
  • In my case I'm using virtual environments and I suspect that the python inside those virtual environments is now out of sync with the system version so much that this error occur. I suspect that rebuilding the virtual environment from the ground up may solve my problem, although it was struggle to install all packages :(
    – Csaba Toth
    Jan 1, 2018 at 2:36

4 Answers 4

26

This isn't an Ubuntu solution, but because this question appears high in search results for the error, I wanted to say that I had this error on OSX because my default system Python was 2.7, and I brew installed "python@2" while installing another package.

If you're on OSX and are using homebrew, check your packages installed with brew by running brew list, and if "python@2" appears in that list, and Python 2.7 is your system Python, you can remove the brew installed package with brew remove python@2 --ignore-dependencies

4
  • 2
    This happened after a brew upgrade replacing many package with their package@version new ones... Thanks!
    – Hugo H
    Apr 17, 2018 at 9:23
  • 2
    This helped me. When running swift on Mac I got the above mentioned errors. Turned out it was because it was using python2 from brew. Removing it fixed my issue.
    – jmurphyau
    May 7, 2018 at 22:05
  • 2
    Thanks for this. It helped solve my lldb warning. Sep 21, 2018 at 18:00
  • Thanks for the heads up. I just updated my symlinks in /usr/local/bin/python to point to latest Homebrew python, rather than system python
    – matt
    Sep 13, 2019 at 23:18
13

In case you were using a virtual environment, you'll need to re-create it. The other possibility is that you use some source based or side-installed python version which clashes with the system one. In that case you have to sort out that the right folders and paths are configured looked from the various environments.

5

In my case, it was an error related to the virtual environment. So, creating a new virtual environment worked for me. Here is the list of commands that may help you in the same.

Installing virtual environment:

pip install virtualenv

Creating a new virtual environment:

virtualenv -p {env_name}

Activating the virtual environment:

source env_name/bin/activate

Deactivating your virtual environemnt:

deactivate
1
  • The OP makes no mention of a virtual env so your answer does not address the issue when a virtual env is not used.
    – Michael M
    Aug 2, 2019 at 1:26
2

Assume you need to recreate virtualenv:

cd <virtualenv_folder>
virtualenv .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.