Python 2.7.3 tempfile.py, ln34, from random import Random as _Random

Peter Otten __peter__ at web.de
Mon Mar 25 08:43:44 EDT 2013


dbv wrote:

> In Python 2.7.3, at ln34 the source file tempfile.py states:
> 
> from random import Random as _Random
> 
> But, Random is a Class.

This is not a problem. You can import arbitrary objects from a module.

 
> The project imports werkzeug and using PyInstaller, the Traceback is:
> 
> Traceback (most recent call last):
>   File "<string>", line 9, in <module>
>   File

[...]   
>   line 268, in load_module
>     exec(bytecode, module.__dict__)
>   File
>   
"//home//ubuntu//Programs//myproject//myproject_pi2/build/pyi.linux2/myproject/out00-
PYZ.pyz/tempfile",
>   line 34, in <module>
> ImportError: cannot import name Random
> 
> Is this a bug?  Or, is there some workaround available?  Thx.

I don't know about PyInstaller or werkzeug, but the most likely cause is 
that you shaded the standard library's random module with your own random.py 
which doesn't contain an object named "Random". Here's a demonostration:

Normal behaviour:

$ python -c 'from random import Random as _Random'

Now add an empty random.py:

$ touch random.py
$ python -c 'from random import Random as _Random'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name Random

Back to normal:

$ rm random.pyc random.py
$ python -c 'from random import Random as _Random'





More information about the Python-list mailing list