ImportError when trying to import python module in SublimeText2

I am new to SublimeText2. So far I have found this excellent, but I just ran into a problem that I could not solve. I am trying to import a Python module, mechanize, in my script. However, whenever it starts (only the import mechanization line), I get:

Traceback (most recent call last): File "/Users/gabrielbianconi/Desktop/test.py", line 1, in <module> import mechanize ImportError: No module named mechanize [Finished in 0.3s with exit code 1] 

However, when I run this line in the terminal, it works fine:

 gb-air:~ gabrielbianconi$ python Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import mechanize >>> 

I think this is a problem with ST2. I saw that ST2 on Mac uses system python, so I don’t understand why it does not recognize the module.

Many thanks.

Edit: I'm on OS X Mountain Lion.

+6
source share
2 answers

I managed to solve this problem. ST2 did not use the same python as the Terminal. For those who have the same error, you can solve this by choosing: Sublime Text 2> Preferences> Browser Packages ... Then go to the Python folder and open Python.sublime_build. Now edit the 'cmd' parameter to match your desired Python path. For me (using MacPorts) the result is:

 { "cmd": ["/opt/local/bin/python", "-u", "$file"], "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.python" } 
+12
source

This piece of code helped me (go to linux terminal):

 echo "export PATH=~/bin:$PATH" >> ~/.profile 

( source )

0
source

All Articles