1

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.

Community
  • 1
  • 1

3 Answers3

2

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).

Fabio Zadrozny
  • 24,814
  • 4
  • 66
  • 78
2

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.

Community
  • 1
  • 1
jimbo
  • 11,004
  • 6
  • 29
  • 46
0

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

snakesNbronies
  • 3,619
  • 9
  • 44
  • 73
  • I tried adding all of the folders to the PYTHONPATH in Eclipse but no luck. If I run something like subprocess.call("ls") it works fine. – SpaceMonkey Jun 24 '12 at 04:42
  • From the Console, I checked which PYTHONPATHs were being used using >>> import sys >>> from pprint import pprint as pp >>> pp(sys.path). I took all of these directories and added them to PYTHONPATH in Eclipse/PyDev. Restarted Eclipse. The problem still exists. – SpaceMonkey Jun 25 '12 at 00:48