Fixing virtualenv after Upgrading Your Distribution/Python

After you upgrade your python/distribution (specifically this happened to me after upgrading from Ubuntu 11.10 to 12.04), your existing virtualenv environments may stop working. This manifests itself by reporting that some modules are missing. For example when I tried to open a Django shell, it complained that urandom was missing from the os module. I guess almost any module will be broken.

Apparently, the solution is dead simple. Just re-create the virtualenv environment:

virtualenv /PATH/TO/EXISTING/ENVIRONMENT

or

virtualenv --system-site-packages /PATH/TO/EXISTING/ENVIRONMENT

(depending on how you created it in the same place). All the modules you’ve already installed should keep working as before (at least it was that way for me).

8 thoughts on “Fixing virtualenv after Upgrading Your Distribution/Python”

  1. Thank you!

    The exact error that I got was:

    ImportError: cannot import name urandom

    (Hoping to give this post some more google-juice šŸ™‚

  2. I am alittle new and stuck in same issue: so what is /PATH/TO/EXISTING/ENVIRONMENT ?

  3. @Debraj: This means you should provide the full path to the directory where your virtualenv environment resides. For example /home/debraj/my_project.env

  4. I’m actually using a VENV folder from a git respository, so… I guess maybe my symlinks were broken by the 12.04 -> 14.04 upgrade. I got some errors running virtualenv /var/ww/project/venv (“unknown URL type” a few times, and something about pip failing) but it totally fixed my problem. Really happy this was the top result on Google — thank you!

  5. I owe you a beverage.

    This fixed it for me after broken virtualenvs from a dist-upgrade from debian jessie to debian stretch.

    Here’s a good oneliner for anyone having a large amount of virtualenvs located in the default path:

    find ~/.virtualenvs/* -maxdepth 0 -type d -exec virtualenv {} \;

    Thanks for this solution!

  6. Thanks! This easy fix solved the same problems I had after upgrading ubuntu. The specific error message I had been getting was: “ImportError: cannot import name _remove_dead_weakref”. Perhaps, this’ll help someone in the same position.

  7. Thanks man, That worked for me. Previously I was not able to access the packages installed in my virtual environment rather, the “pip freeze” command shows all the packages on the system except the environment-specific ones. I am wondering what caused that problem in the first place.

Leave a Reply

Your email address will not be published. Required fields are marked *