6

I'm new to SublimeText2. So far I have found it excellent, but I just came across a problem I did not manage to solve. I'm trying to import a Python module, mechanize, into my script. However, whenever a run it (just the import mechanize 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 Terminal, it works perfectly:

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 guess this is a problem with ST2. I saw that ST2 on Mac uses the system's python, so I don't see why it doesn't recognize the module.

Thank you very much.

Edit: I'm on OS X Mountain Lion.

Gabriel Bianconi
  • 1,179
  • 3
  • 19
  • 37
  • hope it will help : http://stackoverflow.com/questions/13239839/sublime-text2-import-error-no-module-named-gnuplot – Cool-T Nov 28 '12 at 13:27

2 Answers2

12

I managed to solve this problem. ST2 was not using the same python as the Terminal. For anyone having the same mistake, you can solve this by selecting: Sublime Text 2 > Preferences > Browser Packages... Then go into 'Python' folder and open 'Python.sublime_build'. Now edit the 'cmd' parameter to match your desired Python path. For me (using MacPorts), the result was:

{
    "cmd": ["/opt/local/bin/python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}
Gabriel Bianconi
  • 1,179
  • 3
  • 19
  • 37
  • +1, excellent. i had a similar problem w/ getting the python REPL to locate and use the correct python install, even ST2 was doing this correctly except for REPL. (http://stackoverflow.com/questions/10712390/directing-sublime-text-2-packages-to-the-correct-python-installation) – doug Jan 10 '13 at 08:30
0

This piece of code helped me (enter in in the linux terminal):

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

(source)

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Mehdi Rostami
  • 139
  • 2
  • 5