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

4 Answers4

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

duhaime
  • 638
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.

Csaba Toth
  • 1,498
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
2

Assume you need to recreate virtualenv:

cd <virtualenv_folder>
virtualenv .
zx485
  • 2,865
Some
  • 121