简体   繁体   中英

ImportError: cannot import name urandom in Eclipse/PyDev

When I attempt to run the script below in Eclipse(PyDev):

import subprocess
subprocess.call("/usr/local/bin/mitmdump")

An error is returned:

Traceback (most recent call last):
  File "/usr/local/bin/mitmdump", line 19, in <module>
    from libmproxy import proxy, dump, cmdline
  File "/Library/Python/2.7/site-packages/libmproxy/proxy.py", line 22, in <module>
    import shutil, tempfile, threading
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py", line 34, in <module>
    from random import Random as _Random
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py", line 47, in <module>
    from os import urandom as _urandom
ImportError: cannot import name urandom

If I run the same script from the bash, it works fine. What gives?

$ python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call("/usr/local/bin/mitmdump")
127.0.0.1 GET http://google.com/

There seems to be a related problem with Ubuntu Python ImportError cannot import urandom Since Ubuntu 12.04 upgrade but my environment is OSX.

To properly diagnose that, do the following:

Edit /usr/local/bin/mitmdump, and make:

try:
    from libmproxy import proxy, dump, cmdline
except ImportError:
    import sys
    print 'Executable:', sys.executable
    print '\n'.join(sorted(sys.path))
    raise

And then check if what you're seeing is actually what you expected... (you can do those same prints in the command line to when the exception is not raised and check what's the difference and then, probably, update your PYTHONPATH inside Eclipse/PyDev).

I had the same symptoms but in python proper on the command line in Mac OS X. Found the answer here: Python: cannot import urandom module (OS X)

Not sure if it's the same problem you're having or how to invoke hash -r from Eclipse.

I would imagine your python path is net set properly. If so, python can't find the model and therefore can't import.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM