Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError when trying to import python module in SublimeText2

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.

like image 314
Gabriel Bianconi Avatar asked Nov 28 '12 13:11

Gabriel Bianconi


1 Answers

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"
}
like image 179
Gabriel Bianconi Avatar answered Sep 22 '22 11:09

Gabriel Bianconi