Forums

Failing on import of os.urandom within virtualenv

I've seen this a few different times now and only within a virtualenv. My code will import some standard lib module which will import another which will eventually try to import urandom - which fails.

Here's a sample of the last few lines of one such traceback:

  File "/usr/local/lib/python2.7/mimetools.py", line 6, in <module>
    import tempfile
  File "/usr/local/lib/python2.7/tempfile.py", line 34, in <module>
    from random import Random as _Random
  File "/usr/local/lib/python2.7/random.py", line 47, in <module>
    from os import urandom as _urandom
ImportError: cannot import name urandom

It seems that as standard lib modules are trying to import this module, then it is a problem with python (as configured) rather than my code. But that it only happens within a virtualenv is really strange to me. Thoughts?

Do you get the problem only with virtualenvs you created a while back? We did upgrade our Python binaries quite recently, and it looks like there might be a compatibility issue between virtualenvs created on the old one (2.7.2) running on the new on (2.7.3) -- see this thread.

Let us know, either way, and we'll see if we can work out a workaround -- it might take a little while, though :-S

Ah, yes, this problem only occurs for virtualenvs created with Python 2.7.2. Newer virtualenvs created with Python 2.7.3 work fine.

Sounds like that was the problem, then. Do you have any old virtualenvs that need converting, or can you work with just the new ones?