5
votes

I was working on an old Django project (Django version 1.3.2, Python version 2.6.6). Running "python manage.py runserver" gives me: "ImportError: cannot import name urandom".

Searching online returns a bunch of results about solving the problem by re-running virtualenv. This doesn't make sense because I never had virtualenv installed and things worked before...so I figured it must be a problem with my python installation.

I decided to remove and re-install Django by following the Django installation guide by running

pip install Django

Doing django-admin.py startproject project gives me the same ImportError. So I uninstalled Django again using pip uninstall Django.

Now running python starts the python2.6.6 shell, typing from os import urandom gives me the "ImportError: cannot import name urandom" message, while doing the same in python2.7 does not give such error.

What's going on here? What's breaking my python2.6.6?

1
Check here- might be relevant. - dgel
I came across that post, but it simply suggests running virtualenv again. But I don't even have virtualenv installed, and I'm on ubuntu 11.04... - Asy
Do you by any chance have a file called os.py or os.pyc in your python path (such as your project directory)? - dgel
Try the following in a python2.6 shell: import os then print os.__file__ - dgel
@dgel: python issue #14444 shouldn't affect 2.6.6 unless Ubuntu backported the security patch from Python 2.6.8 - jfs

1 Answers

0
votes

While I don't know if I can provide any more info that the many helpful comments available, here is my stab at the possible reasons for this failure:

  1. Your OS is no longer providing urandom properly to Python 2.6.6. From what I can tell here OSError with "import random" when /dev/urandom doesn't exist, this should result in an OSError or NotImplementedError (rather than an ImportError) but docs have been wrong before. A decent way to check if you can get to the system's urandom is to run:

    from posix import urandom
    

    If that doesn't work, try:

    from os import O_RDONLY
    aURandom = open("/dev/urandom", O_RDONLY)
    

    If neither work, you're probably dealing with an inability to get to the system function by any means. That probably means OS setup is somehow to blame. You might want to actually check if urandom is actually available on your Ubuntu installation where python expects it (/dev/urandom). Your version of Python 2.7 might also be failing to access urandom too, but failing silently instead of crashing.

  2. If the above snippet of code works, then your os.py file or a compiled version of such is messed up. In that case, your Python installation is broken. You'd need to uninstall it, clear out any intermediate pyc or pyo files, then re-install it.

Given that the python 2.6.6 interpreter can't find urandom on its own, Django is unlikely to be the issue.