Page 1 of 1

cannot import name urandom

Posted: Sat May 11, 2013 12:14 am
by fellows
from within Blender (2.49b), when I try to run a Python script containing the line: "from os import urandom"

I get the error: "ImportError: cannot import name urandom"

My setup:
Ubuntu 12.04 LTS, 64-bit version
Blender 2.49b (blender-2.49b-linux-glibc236-py26-x86_64)
Python2.6.8

from within stand-alone python the same command ("from os import urandom") works fine, but not from within Blender's python.

I've searched around for hours and have not found a solution.

By the way, I am running the older Blender version because I am trying to use it with MBDyn and this requires the older Blender.

Any suggestions would be greatly appreciated.

Posted: Sat May 11, 2013 12:42 am
by CoDEmanX
are there two different python installations maybe? One with that module, and another without?

Posted: Sat May 11, 2013 12:52 am
by fellows
CoDEmanX wrote:are there two different python installations maybe? One with that module, and another without?
Thanks for the quick reply.

There are several versions of python on my machine, yes, and they all have the os module since it is a built-in.

I don't think that's the problem, though, because Blender is able to find the correct Python ("Compiled with PYthon version 2.6.2. Checking for installed Python... got it!"). Also, "import os" from within Blender works.

Posted: Sat May 11, 2013 7:06 pm
by CoDEmanX
if "import os" works, can't you do

Code: Select all

import os
os.urandom(#)
?

Posted: Mon May 13, 2013 6:11 pm
by fellows
CoDEmanX wrote:if "import os" works, can't you do

Code: Select all

import os
os.urandom(#)
?
Nope, that doesn't work either. I get "AttributeError: 'module' object has no attribute 'urandom'

Incidentally, if I run the following in Blender
import os
import inspect
print inspect.getfile(os)

I get /usr/lib/python2.6/os.pyc

which is also what I get when running the same code from Python in a terminal (outside Blender). So it looks like they are calling the same os.pyc code, yet in one case it can find urandom and in the other it can't. Strange.

Posted: Mon May 13, 2013 7:41 pm
by CoDEmanX
that's really weird, maybe ask over at python.org?

Posted: Tue May 14, 2013 4:50 pm
by CoDEmanX
It appears to be an OS-related problem, here's a possible fix:

https://gist.github.com/PureForm/5340738

Maybe this helps to test if urandom exists:

Code: Select all

os_module = __import__("os")
print(hasattr(os_module, "urandom"))