5

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?

mgilson
  • 300,191
  • 65
  • 633
  • 696
Asy
  • 313
  • 1
  • 3
  • 11
  • 2
    Check [here](http://stackoverflow.com/questions/10366821/python-importerror-cannot-import-urandom-since-ubuntu-12-04-upgrade)- might be relevant. – dgel Nov 09 '12 at 20:30
  • 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 Nov 09 '12 at 20:38
  • Do you by any chance have a file called `os.py` or `os.pyc` in your python path (such as your project directory)? – dgel Nov 09 '12 at 20:45
  • Try the following in a python2.6 shell: `import os` then `print os.__file__` – dgel Nov 09 '12 at 20:46
  • `import os; print os.__file__` gives `/usr/lib/python2.6/os.pyc` Do I need to re-install python2.6? – Asy Nov 09 '12 at 20:58
  • No ideas. Try uninstalling and re-installing the python2.6 package. – dgel Nov 09 '12 at 21:04
  • 2
    @dgel: [python issue #14444](http://bugs.python.org/issue14444#msg158172) shouldn't affect 2.6.6 unless Ubuntu backported the security patch from Python 2.6.8 – jfs Nov 09 '12 at 21:09
  • is urandom the only affected module? it may either be a) virutalenv b) urandom 3) django 1.3 related... (note 1) and 2) are somewhat related) – Don Question Nov 09 '12 at 21:14
  • @DonQuestion, I don't have virtualenv installed, so I assume it's not from virtualenv. Or is that incorrect? – Asy Nov 09 '12 at 21:24
  • Can you run Django under Python 2.7? – Markus Unterwaditzer Nov 09 '12 at 21:31
  • @MarkusUnterwaditzer, I probably could, but it doesn't make sense what broke python2.6. Also, I want to avoid change the default python path to 2.7 because a lot of other apps uses 2.6. – Asy Nov 09 '12 at 22:19
  • @Asy - can you run `dpkg -l python2.6` to confirm the exact installed version? And update with `import sys; print sys.path` from a normal python2.6 shell? – rcoup Dec 05 '12 at 19:11
  • Totally forgot to update this. It ended up being problems with my Python paths. My /usr/bin/python is linked to python2.6. However, Django was using python2.7. Once I resolved this conflict the error went away. – Asy Oct 04 '14 at 00:06

1 Answers1

0

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.

Namey
  • 1,172
  • 10
  • 28