ImportError: cannot import name urandom from os

Bug #954595 reported by Niran Babalola
84
This bug affects 19 people
Affects Status Importance Assigned to Milestone
Release Notes for Ubuntu
Fix Released
Undecided
Unassigned
Precise
Fix Released
Medium
Unassigned
python2.7 (Ubuntu)
Invalid
Undecided
Unassigned

Bug Description

1) Description: Ubuntu precise (development branch)
Release: 12.04

2) python2.7 2.7.3~rc1-1ubuntu2, libpython2.7 2.7.3~rc1-1ubuntu2, python2.7-minimal 2.7.3~rc1-1ubuntu2

(tribune)niran@niran-MacPro:~/tribune/texastribune$ pip install -U fabric
Traceback (most recent call last):
  File "/home/niran/.virtualenvs/tribune/bin/pip", line 9, in <module>
    load_entry_point('pip==1.0.2', 'console_scripts', 'pip')()
  File "/home/niran/.virtualenvs/tribune/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 337, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/home/niran/.virtualenvs/tribune/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 2279, in load_entry_point
    return ep.load()
  File "/home/niran/.virtualenvs/tribune/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 1989, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
  File "/home/niran/.virtualenvs/tribune/local/lib/python2.7/site-packages/pip/__init__.py", line 10, in <module>
    from pip.backwardcompat import u, walk_packages, console_to_str
  File "/home/niran/.virtualenvs/tribune/local/lib/python2.7/site-packages/pip/backwardcompat.py", line 66, in <module>
    from urllib2 import URLError, HTTPError
  File "/usr/lib/python2.7/urllib2.py", line 94, in <module>
    import httplib
  File "/usr/lib/python2.7/httplib.py", line 79, in <module>
    import mimetools
  File "/usr/lib/python2.7/mimetools.py", line 6, in <module>
    import tempfile
  File "/usr/lib/python2.7/tempfile.py", line 34, in <module>
    from random import Random as _Random
  File "/usr/lib/python2.7/random.py", line 47, in <module>
    from os import urandom as _urandom
ImportError: cannot import name urandom

(tribune)niran@niran-MacPro:~/tribune/texastribune$ python
Python 2.7.2+ (default, Aug 16 2011, 07:24:41)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.urandom
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'urandom'

Downgrading all three packages to 2.7.2-13ubuntu5 makes pip work again, and importing urandom from os succeeds as well.

Niran Babalola (niran)
description: updated
description: updated
Revision history for this message
Launchpad Janitor (janitor) wrote :

Status changed to 'Confirmed' because the bug affects multiple users.

Changed in python2.7 (Ubuntu):
status: New → Confirmed
Revision history for this message
Matthias Klose (doko) wrote :

works for me; maybe something is wrong with your virtualenv?

Changed in python2.7 (Ubuntu):
status: Confirmed → Invalid
L42y (l42y)
Changed in python2.7 (Ubuntu):
status: Invalid → Confirmed
Matthias Klose (doko)
Changed in python2.7 (Ubuntu):
status: Confirmed → Invalid
Revision history for this message
Keith Brisson (kbrisson) wrote :

I'll also retract my confirmation. I do suspect it might have been virtualenv-related.

Revision history for this message
L42y (l42y) wrote :

is it because virtualenvwrapper?

Revision history for this message
Keith Brisson (kbrisson) wrote :

It occurred for me without virtualenvwrapper.

Best guess: either the virtualenv needed to be reactivated or the system needed a restart (and the virtualenv needed to be reloaded). But I'm not sure.

Revision history for this message
L42y (l42y) wrote :

I installed the dev version of virtualenv(pip install virtualenv==dev), and the problem solved for me.

Revision history for this message
Kostiantyn Rybnikov (k-bx) wrote :

For those who need "quick fix" -- just re-run:

virtualenv /path/to/your/virtualenv

Revision history for this message
Anthony Dillon (ya-bo-ng) wrote :

I am still get this error. virtualenv was working fine in 11.10 but since upgrading to precise I get this error.

Revision history for this message
Frederik Elwert (frederik-elwert) wrote :

I also experienced this, and I can confirm the fix from #7 works (thanks, Konstantine).

So if this is invalid as a python bug, maybe it is a virtualenv bug? Maybe there should be some upgrade automatism, or at least an upgrade hint? Or are virtualenvs supposed to break on upgrades?

Revision history for this message
Bruce Wang (number5) wrote :

I got the same error in my virtualenv when I upgraded from 11.10 to 12.04

Turned out to be the python2.7-minimal didn't get properly upgrade, so /usr/bin/python is 2.7.3, but /usr/bin/python2.7 is 2.7.2

`aptitude reinstall python2.7-minimal` then recreate the virtualenv fixed the problem.

Revision history for this message
David Niergarth (jdnier) wrote :

While the #7 fix solved the urandom import problem for me -- my previous virtualenvs had been installed under 11.10 with Python 2.7.1 -- I found I needed to rebuild my virtualenvs from scratch to solve other import issues. My advice: try rebuilding your virtualenvs from scratch.

Revision history for this message
Natim (site-remy) wrote :

Same for me, I needed to remake all my virtualenv

Revision history for this message
Barry Warsaw (barry) wrote : Re: [Bug 954595] Re: ImportError: cannot import name urandom from os

On Apr 27, 2012, at 09:33 AM, Natim wrote:

>Same for me, I needed to remake all my virtualenv

This is a known release issue with upstream Python 2.7.3. The cause is the
security fix for the hash randomization DoS vulnerability. You will need to
re-run virtualenv in order to pick up the security fix.

Revision history for this message
Scott Kitterman (kitterman) wrote :

On Friday, April 27, 2012 02:34:57 PM Barry wrote:
> This is a known release issue with upstream Python 2.7.3. The cause is the
> security fix for the hash randomization DoS vulnerability. You will need to
> re-run virtualenv in order to pick up the security fix.

Should this go in the release notes?

Revision history for this message
Barry Warsaw (barry) wrote :

On Apr 27, 2012, at 06:40 PM, Scott Kitterman wrote:

>On Friday, April 27, 2012 02:34:57 PM Barry wrote:
>> This is a known release issue with upstream Python 2.7.3. The cause is the
>> security fix for the hash randomization DoS vulnerability. You will need to
>> re-run virtualenv in order to pick up the security fix.
>
>Should this go in the release notes?

Probably so.

Revision history for this message
Scott Kitterman (kitterman) wrote :

On Friday, April 27, 2012 06:54:19 PM you wrote:
> >Should this go in the release notes?
>
> Probably so.

OK. I added a task on ubuntu-release-notes. Would you please draft something
up?

Revision history for this message
Barry Warsaw (barry) wrote :

On Apr 27, 2012, at 07:02 PM, Scott Kitterman wrote:

>OK. I added a task on ubuntu-release-notes. Would you please draft
>something up?

Python 2.7.3 includes a fix for a security vulnerability affecting Python's
dict and set implementations. Carefully crafted, untrusted input could lead
to extremely long computation times and denials of service. Although disabled
by default, vulnerable applications such as CGI scripts can explicitly enable
"hash randomization" to prevent this exploit. Due to implementation details
of this fix, virtualenvs created with older 2.7.x releases may not work with
2.7.3. Specifically, the os module may not appear to have a urandom function.
This problem can be solved by recreating the broken virtualenvs with the newer
Python 2.7.3 version.

See http://bugs.python.org/issue13703 for full details.

Revision history for this message
Chee-Chan Keng (kengcc) wrote :

Fix #7 works for me, thanks!

Revision history for this message
Mark Huang (zhenghao1) wrote :

To fix the problem, I did a

> sudo pip install --upgrade virtualenv

and this upgraded my virtualenv in Ubuntu 12.0.4. This fixed the urandom problem for me.

Revision history for this message
Lorrin Nelson (lhn-5) wrote :

Fixed on Lucid with:

dpkg-reconfigure python2.6-minimal
/etc/init.d/apache2 restart

Revision history for this message
Colin Watson (cjwatson) wrote :

This has been release-noted as Barry suggested.

Changed in ubuntu-release-notes:
status: New → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.