3

Somehow my python is broken and emits the error:

jseidel@EDP15:/etc/default$ python -c 'import random'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.6/random.py", line 47, in <module>
    from os import urandom as _urandom
ImportError: cannot import name urandom

This is NOT the virtualenv error that is so commonly documented here and elsewhere: I don't use python directly, I have never setup a virtualenv explicitly, and there is no virtualenv directory or python script that I can find anywhere on my system.

I'm running Kubuntu 10.04 and until just recently my KPackageKit worked just fine and handled updates with no problem. Now it shows nothing... maybe because of this python error, maybe because of something else.

How do I go about finding the error and fixing python?

JESii
  • 4,678
  • 2
  • 39
  • 44
  • Have you tried apt-getting/aptitud-ing an upgrade to python? – Dan Steingart Nov 24 '12 at 20:40
  • 1
    Start by editing /usr/lib/python2.6/os.py, to add "print 42" at the end of it. The original file ends with a definition of urandom, precisely. Check if 42 appears. If not, you're importing another os.py from somewhere else; check by adding, just before the crashing line in random.py, "import os; print os.__file__". – Armin Rigo Nov 24 '12 at 22:14
  • Thanks very much... I went to the os.py file and added the print statement as you suggested and got '42' printed. Looking at the file, there was NO definition for urandom at the end(!); the last line was 'pass' from the try block for statvfs. So, I got another copy of os.py from python 2.7 and copied the urandom definition in and - voila! - everything works now. If you'll make your response an answer, I'll definitely vote you up. Cheers. – JESii Nov 24 '12 at 23:33
  • Interestingly, when I went to the source (http://hg.python.org/cpython/file/2.6/Lib/os.py#l758) that patch shows no definition of urandom either... did it somehow get dropped on an update? – JESii Nov 24 '12 at 23:36
  • @Dan... Thanks for your comment, too. I may try that down the road, but had problems some time back trying to upgrade python and wanted to save that as a last resort. – JESii Nov 25 '12 at 13:56
  • The problem seems related to [this python bug](http://bugs.python.org/issue15340). Presumably you've recently updated to python-2.6.8. What happens if you try `python -c 'from posix import urandom'`? – ekhumoro Nov 26 '12 at 03:53
  • I got same error on my new Linux system too. However, this error is not seen on my Ubuntu, which has exactly same os.py with this Linux system. Btw, the os.py also contains no definition of urandom, and as @JESii said, the last line is 'pass' from the try block. – Deqing Dec 14 '12 at 08:35
  • 1
    Possible duplicate: http://stackoverflow.com/questions/13876090/importerror-cannot-import-name-urandom – Anis Abboud Feb 28 '13 at 15:04
  • 2
    Following @JESii's advice, I tacked this onto the end of os.py, problem solved! https://gist.github.com/PureForm/5340738 – PureForm Apr 08 '13 at 21:40

1 Answers1

0

As suggested by @Armin Rigo, this worked for me:

1) Add a print 42 at the end of the /usr/lib/python2.6/os.py file.

2) If you see "42", then that is the correct os.py file and the urandom module is not include. Add the statement to include urandom (you can find a sample from another os.py file). This was what worked for me.

3) If you don't see "42", then that's not the os.py file that you're using. Find the random.py file that is crashing and insert import os; print os.__file__ to get more information about the failure.

JESii
  • 4,678
  • 2
  • 39
  • 44