5

I was building a new Linux environment and seeing following error on Python.

# python -c 'import random'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.7/random.py", line 47, in <module>
    from os import urandom as _urandom
ImportError: cannot import name urandom

I'm not using virtualenv, so it should not be the issue here.

I had compared the os.py with the file on my Ubuntu, which don't have this error. They are exactly the same, so looks like it is not this issue too.

By checking following, it seems in my Python there is no urandom in os or posix

# python
Python 2.7.1 (r271:86832, Feb 10 2012, 16:10:22) [GCC] on linux3
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.__file__
/usr/lib/python2.7/os.pyc
>>> dir(os)
['EX_CANTCREAT', 'EX_CONFIG', 'EX_DATAERR', 'EX_IOERR', 'EX_NOHOST', 'EX_NOINPUT', 'EX_NOPERM', 'EX_NOUSER', 'EX_OK', 'EX_OSERR', 'EX_OSFILE', 'EX_PROTOCOL', 'EX_SOFTWARE', 'EX_TEMPFAIL', 'EX_UNAVAILABLE', 'EX_USAGE', 'F_OK', 'NGROUPS_MAX', 'O_APPEND', 'O_ASYNC', 'O_CREAT', 'O_DIRECT', 'O_DIRECTORY', 'O_DSYNC', 'O_EXCL', 'O_LARGEFILE', 'O_NDELAY', 'O_NOATIME', 'O_NOCTTY', 'O_NOFOLLOW', 'O_NONBLOCK', 'O_RDONLY', 'O_RDWR', 'O_RSYNC', 'O_SYNC', 'O_TRUNC', 'O_WRONLY', 'P_NOWAIT', 'P_NOWAITO', 'P_WAIT', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'UserDict', 'WCONTINUED', 'WCOREDUMP', 'WEXITSTATUS', 'WIFCONTINUED', 'WIFEXITED', 'WIFSIGNALED', 'WIFSTOPPED', 'WNOHANG', 'WSTOPSIG', 'WTERMSIG', 'WUNTRACED', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_copy_reg', '_execvpe', '_exists', '_exit', '_get_exports_list', '_make_stat_result', '_make_statvfs_result', '_pickle_stat_result', '_pickle_statvfs_result', '_spawnvef', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'chown', 'chroot', 'close', 'closerange', 'confstr', 'confstr_names', 'ctermid', 'curdir', 'defpath', 'devnull', 'dup', 'dup2', 'environ', 'errno', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fchdir', 'fchmod', 'fchown', 'fdatasync', 'fdopen', 'fork', 'forkpty', 'fpathconf', 'fstat', 'fstatvfs', 'fsync', 'ftruncate', 'getcwd', 'getcwdu', 'getegid', 'getenv', 'geteuid', 'getgid', 'getgroups', 'getloadavg', 'getlogin', 'getpgid', 'getpgrp', 'getpid', 'getppid', 'getresgid', 'getresuid', 'getsid', 'getuid', 'initgroups', 'isatty', 'kill', 'killpg', 'lchown', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'major', 'makedev', 'makedirs', 'minor', 'mkdir', 'mkfifo', 'mknod', 'name', 'nice', 'open', 'openpty', 'pardir', 'path', 'pathconf', 'pathconf_names', 'pathsep', 'pipe', 'popen', 'popen2', 'popen3', 'popen4', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'sep', 'setegid', 'seteuid', 'setgid', 'setgroups', 'setpgid', 'setpgrp', 'setregid', 'setresgid', 'setresuid', 'setreuid', 'setsid', 'setuid', 'spawnl', 'spawnle', 'spawnlp', 'spawnlpe', 'spawnv', 'spawnve', 'spawnvp', 'spawnvpe', 'stat', 'stat_float_times', 'stat_result', 'statvfs', 'statvfs_result', 'strerror', 'symlink', 'sys', 'sysconf', 'sysconf_names', 'system', 'tcgetpgrp', 'tcsetpgrp', 'tempnam', 'times', 'tmpfile', 'tmpnam', 'ttyname', 'umask', 'uname', 'unlink', 'unsetenv', 'utime', 'wait', 'wait3', 'wait4', 'waitpid', 'walk', 'write']
>>> import posix
>>> dir(posix)
['EX_CANTCREAT', 'EX_CONFIG', 'EX_DATAERR', 'EX_IOERR', 'EX_NOHOST', 'EX_NOINPUT', 'EX_NOPERM', 'EX_NOUSER', 'EX_OK', 'EX_OSERR', 'EX_OSFILE', 'EX_PROTOCOL', 'EX_SOFTWARE', 'EX_TEMPFAIL', 'EX_UNAVAILABLE', 'EX_USAGE', 'F_OK', 'NGROUPS_MAX', 'O_APPEND', 'O_ASYNC', 'O_CREAT', 'O_DIRECT', 'O_DIRECTORY', 'O_DSYNC', 'O_EXCL', 'O_LARGEFILE', 'O_NDELAY', 'O_NOATIME', 'O_NOCTTY', 'O_NOFOLLOW', 'O_NONBLOCK', 'O_RDONLY', 'O_RDWR', 'O_RSYNC', 'O_SYNC', 'O_TRUNC', 'O_WRONLY', 'R_OK', 'TMP_MAX', 'WCONTINUED', 'WCOREDUMP', 'WEXITSTATUS', 'WIFCONTINUED', 'WIFEXITED', 'WIFSIGNALED', 'WIFSTOPPED', 'WNOHANG', 'WSTOPSIG', 'WTERMSIG', 'WUNTRACED', 'W_OK', 'X_OK', '__doc__', '__name__', '__package__', '_exit', 'abort', 'access', 'chdir', 'chmod', 'chown', 'chroot', 'close', 'closerange', 'confstr', 'confstr_names', 'ctermid', 'dup', 'dup2', 'environ', 'error', 'execv', 'execve', 'fchdir', 'fchmod', 'fchown', 'fdatasync', 'fdopen', 'fork', 'forkpty', 'fpathconf', 'fstat', 'fstatvfs', 'fsync', 'ftruncate', 'getcwd', 'getcwdu', 'getegid', 'geteuid', 'getgid', 'getgroups', 'getloadavg', 'getlogin', 'getpgid', 'getpgrp', 'getpid', 'getppid', 'getresgid', 'getresuid', 'getsid', 'getuid', 'initgroups', 'isatty', 'kill', 'killpg', 'lchown', 'link', 'listdir', 'lseek', 'lstat', 'major', 'makedev', 'minor', 'mkdir', 'mkfifo', 'mknod', 'nice', 'open', 'openpty', 'pathconf', 'pathconf_names', 'pipe', 'popen', 'putenv', 'read', 'readlink', 'remove', 'rename', 'rmdir', 'setegid', 'seteuid', 'setgid', 'setgroups', 'setpgid', 'setpgrp', 'setregid', 'setresgid', 'setresuid', 'setreuid', 'setsid', 'setuid', 'stat', 'stat_float_times', 'stat_result', 'statvfs', 'statvfs_result', 'strerror', 'symlink', 'sysconf', 'sysconf_names', 'system', 'tcgetpgrp', 'tcsetpgrp', 'tempnam', 'times', 'tmpfile', 'tmpnam', 'ttyname', 'umask', 'uname', 'unlink', 'unsetenv', 'utime', 'wait', 'wait3', 'wait4', 'waitpid', 'write']

Here is what I had on my Linux.

# rpm -qa|grep python
python-base-2.7.1-4.21.i586
python-xml-2.7.1-4.21.i586
dbus-python-0.83.1-3.2.i586
libpython-2.7.1-4.21.i586
python-2.7.3-6.18.i686
#
# ls -l /dev/urandom
crw-rw-rw- 1 root root 1, 9 Dec 12 22:16 /dev/urandom

Am I missing any Python modules?

Community
  • 1
  • 1
Deqing
  • 14,098
  • 15
  • 84
  • 131
  • http://stackoverflow.com/questions/10366821/python-importerror-cannot-import-urandom-since-ubuntu-12-04-upgrade – Alexey Kachayev Dec 14 '12 at 09:47
  • @AlexeyKachayev thanks for the link. As I described, I'm not using virtualenv though, so I'm afraid it is not the case. – Deqing Dec 14 '12 at 09:52
  • Can you also give the dir(posix)? From http://hg.python.org/cpython/file/2.7/Modules/posixmodule.c, it looks like urandom() should be in the posix module unconditionally. I'm not sure how `__all__` is defined for C modules, though. – djc Dec 14 '12 at 10:00
  • Do you have `/dev/urandom`? The code should throw a `NotImplementedError` exception if it's not there, though. – tripleee Dec 14 '12 at 10:04
  • Updated my question. @djc urandom is not in posix too, what should I do next? @tripleee Yes, `/dev/urandom` is there. – Deqing Dec 14 '12 at 10:11
  • Actually, I was looking at 2.6. This specifically changed in 2.7. See also http://bugs.python.org/issue15340 but I see from your edit that this appears not to be the problem you are experiencing (unless your script runs very early in the boot process, and `/dev/urandom` is created only later). – tripleee Dec 14 '12 at 10:12
  • I'd file a bug with Ubuntu, this shouldn't happen. Hard to say how this can go wrong this way. – djc Dec 14 '12 at 10:20
  • @djc: The bug is not in Ubuntu but in his own home-grown RPM-based set-up. – tripleee Dec 14 '12 at 10:34
  • You're building a new Linux environment; from scratch? What's your kernel? I've had issues with uname, which appeared to be related to an old kernel. Not saying it's the same here, but might be something to think about. If you build Python from scratch, a verbose log or running the tests may also point you why urandom wasn't included in the build. –  Dec 14 '12 at 10:44
  • @Evert My system is based on `Tizen` OS, which contains packages in [this link](http://download.tizen.org/snapshots/2.0alpha/common/tizen-2.0_20121129.2/repos/tizen-base/ia32/packages/i586/) – Deqing Dec 14 '12 at 15:14
  • If you installed the Python package from there, you're probably best off asking on the Tizen developers list (if that exists): sounds like urandom was never compiled into that Python distro (eg, because the development platform didn't support it? I wouldn't know). –  Dec 14 '12 at 16:55

1 Answers1

5

The urandom in os changed between 2.7.1 (your version) and 2.7.3 (current). On 2.7.1 posix does not have a urandom and os.py has a fallback method (at the end of os.py). In 2.7.3 posix always has a urandom function. So maybe you're mixing os.py versions?

Elrond
  • 901
  • 9
  • 23
  • Thanks, do you know Where can I find the `os.py` of 2.7.1 to double check? – Deqing Dec 15 '12 at 04:03
  • I don't have one handy, but you can download the full 2.7.1 sources [here](http://www.python.org/ftp/python/2.7.1/). os.py is Python-2.7.1/Lib/. – Elrond Dec 15 '12 at 09:51
  • Had compared os.py versions, looks like you are right, my os.py versions were mixed. – Deqing Dec 17 '12 at 08:28