Python ImportError cannot import urandom Since Ubuntu 12.04 upgrade

PythonUbuntuRandom

Python Problem Overview


Upgraded Ubuntu to Precise Pangolin (12.04), and Python's Random is now broken... I suspect other things might be broken too. How do I fix Python?

    File "/usr/lib/python2.7/random.py", line 47, in <module>
    from os import urandom as _urandom
ImportError: cannot import name urandom

Alas, poor Python! I knew him, Random, a module of infinite jest, of most probable chaos. He hath bore me on his randint() a thousand times, and now how abhorr'd in my workspace it is! My Exception Handler rises at it.

edit Running in virtualenv

Python Solutions


Solution 1 - Python

Is this your problem?

https://bugs.launchpad.net/ubuntu/+source/python-defaults/+bug/989856

Seems to be caused by running it in a virtual environment, and there is a work around.

BTW this was the top result in google.

Solution 2 - Python

I had the same problem when upgrading to 12.10, and when upgrading to 16.04 (In the 16.04, the message was no module named datetime.)

To solve it just ran virtualenv over the same installation and it worked. Give it the --no-site-packages flag to preserve your modules:

$ virtualenv --no-site-packages ~/lib/virtualenv/netunong
Overwriting /home/adam/lib/virtualenv/netunong/lib/python2.7/site.py with new content
New python executable in /home/adam/lib/virtualenv/netunong/bin/python
Installing distribute.....<skippping dots, of course>.......done.
Installing pip................done.
Overwriting /home/adam/lib/virtualenv/netunong/bin/activate with new content
Overwriting /home/adam/lib/virtualenv/netunong/bin/activate_this.py with new content

Since I have all of my virtualenvs in the same dir, I just ran:

$ ls | xargs -I{} virtualenv  --no-site-packages {}

Solution 3 - Python

I was getting this same error and fixed it by just re-running virtualenv (e.g., virtualenv --no-site-packages ~/venv/myvirtualenv/).

Solution 4 - Python

Another solution is to remove pip if you installed it with apt-get use wget to fetch it

wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py

and then run

python get-pip.py or sudo python get-pip.py if you're not logged in with root.

This worked for me.

Solution 5 - Python

Okay, maybe it is just not configured? Check this site, and look at the Configuring section.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionxxjjnnView Question on Stackoverflow
Solution 1 - PythonAdam CadienView Answer on Stackoverflow
Solution 2 - PythonbrandizziView Answer on Stackoverflow
Solution 3 - PythonjbarhamView Answer on Stackoverflow
Solution 4 - PythonRai EhtishamView Answer on Stackoverflow
Solution 5 - PythonSuperProgramanView Answer on Stackoverflow