54

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

xxjjnn
  • 14,591
  • 19
  • 61
  • 94
  • @Mark0978 If you have issue with the closing of a question, ask on [meta]. Do *not* troll questions and add comments because you don't like what happened. I've reopened the question based on the first comment, but what you're doing is not appropriate for the site. Please refrain from that in the future, or further moderator action may be taken. – casperOne Nov 16 '12 at 20:26

5 Answers5

44

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.

Adam Cadien
  • 1,137
  • 9
  • 19
  • Thank you for the link. It suggests rebuilding one's virtual environments. However, some of us have a few dozen virtualenvs, each with a lot of dependencies. Do you know if there are there any other possible solutions that might not require this re-build for each? – Joe J May 01 '12 at 21:10
  • I'm confused as to how that would be possible to work around. If each virtual machine has its own locally installed python interpreter how could you possibly make a change to one and fix the others? – Adam Cadien May 02 '12 at 02:51
  • 4
    As Adam Victor wrote here: https://bugs.launchpad.net/ubuntu/+source/python-defaults/+bug/989856 You can try $: virtualenv ~/path/to/virtualenv or when You are in virtualenvs dir $: ls | xargs -I{} virtualenv {} – lechup May 03 '12 at 15:09
  • 14
    Ironically, your answer is now (for me, anyways) the top result in Google! :-) – JohnJ Jul 02 '12 at 16:33
  • 5
    It helps to be mindful that Google shows [different results](http://webmasters.stackexchange.com/questions/24783/why-do-different-browsers-return-different-search-results-at-google-and-how-can) to different users based on a variety of factors. *The top result for you may not be the top result for me.* In fact, your top result may not show up on the first page of my results. – Inactivist Dec 15 '12 at 14:56
36

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 {}
brandizzi
  • 26,083
  • 8
  • 103
  • 158
  • 1
    That's great - however note that if you have custom options to your virtual environments (notably different python versions), the above may remove them – David Fraser Jul 03 '12 at 10:51
  • for ve in * ; do python="\`{ ls $ve/bin/python ; ls $ve/bin/python*.* 2>/dev/null | sort ; } | tail -n 1\`"; python=\`basename "$python"\`; virtualenv --python $python $ve; done – David Fraser Jul 03 '12 at 11:14
  • Worked for me on 10.04 and a python 2.6 venv for an extensive django app venv. I didn't even have to do a `pip install -r requirements/base.txt` on the new (refreshed) venv because all the python modules were still installed. – hobs Oct 05 '12 at 17:49
7

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

jbarham
  • 140
  • 5
1

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.

Rai Ehtisham
  • 151
  • 1
  • 2
  • 11
0

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

SuperPrograman
  • 1,814
  • 17
  • 24