ImportError when trying to import python module in SublimeText2

2024/7/2 15:14:44

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.

Answer

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"
}
https://en.xdnf.cn/q/73372.html

Related Q&A

sendfile() failed (32: Broken pipe) while sending request to upstream nginx 502

I am running Django, uwsgi, ngix server. My server works fine for GET, POST requests of smaller size. But when POSTing requests of large size, nginx returns 502: nginx error.log is: 2016/03/01 13:52:1…

Python introspection - how to check current module / line of call from within function

I have a function:# utils.py def hello(name=World):# Detect where Im being called from.print(Hi, %s. You called this from %s at line # %d. % (name, mod, lineno))# ``mod`` and ``lineno`` on previous lin…

Flask blueprints with gevent working outside of application context

I am trying to send emails asynchronously with in Flask with gevent via flask-mail. I am getting "working outside of application context". I am aware of with app.app_context() but I cannot ge…

how to optimally count elements in a python list

This is almost the same question than here, except that I am asking about the most efficient solution for a sorted result.I have a list (about 10 integers randomly between 0 and 12), for example:the_li…

How to install and run virtualenv on MacOS correctly

Hi Im a beginner of python, I dont remember when and how I installed python3.8 on my Macbook air, only knew the installed path: % which python /usr/bin/python % which python3 /usr/local/bin/python3The …

comparing a string 1-d numpy array elementwise

I have two 1-d arrays (a and b) containing strings, which I want to compare element wise to get output c like shown below. I tried converting it to set and comparing, however that does not give the cor…

Can Atom work with Python virtualenvwrapper

I want to start a Flask app. I installed virtualenvwrapper to manage the packages but I cant let Atom know that the current project should use the virtualenvs python binary.from flask import Flask, ren…

Concatenation of tuples

Normal text:Im having some problems with coding on python 3.2.1. Actually Im taking online lectures that are on python 2.5.Here is the code:x = 100 divisors = () for i in range(1,x):if x%i == 0:divisor…

How do I create a new data table in Orange?

I am using Orange (in Python) for some data mining tasks. More specifically, for clustering. Although I have gone through the tutorial and read most of the documentation, I still have a problem. All th…

How to turn off MySQL query cache while using SQLAlchemy?

I am working with a fairly large MySQL database via the SQLAlchemy library, and Id love to turn off MySQLs query caching to debug performance issues on a per-session basis. Its difficult to debug slow …