Reputation: 245
Questions similar to this have been asked before, but I only can find explanations of the problem, not solutions.
I am running OS X Mavericks, and learning python. I installed Python but little did I know it was built in.
I use sublime text as my IDE, and I tried to install a few libraries (scipy, numpy, matplotlib). In Sublime Text, when I import the libraries, they work. I can import scipy and print scipy.pi perfectly. However, when I ran it in the terminal, it said it couldn't find those libraries.
Others have explained that the terminal is running the operating system's python and couldn't find the library, but didn't explain where one should place the libraries for it to run properly in the terminal.
Any help is appreciated, thank you
Upvotes: 0
Views: 2838
Reputation: 133
You need to find out exactly what you are calling from Sublime Text. This solution ONLY applies if what others are saying is true and Sublime Text is calling another python interpreter that is different from the one that you are calling from the shell.
Step 1
Quick and dirty way to find out what Sublime Text is calling:
This should give you an error message like this:
/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't find '__main__' module in ''
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u ""]
[dir: /Users/someuser/Downloads]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
The first line is giving you the path to python interpreter that Sublime Text is using.
Step 2
Next, you should find out what you are calling in the command line:
% which python
/usr/local/bin/python
Using the information above, you can locate where /usr/local/bin/python is by:
% ls -la /usr/local/bin/python
lrwxr-xr-x 1 someuser wheel 33 Jul 29 23:14 /usr/local/bin/python@ -> ../Cellar/python/2.7.5/bin/python
If that the two paths are the same, then stop: my solution will not apply to your situation.
Step 3
If the two paths are different, then you will need to either specifically use the path to the python interpreter that sublime text is calling:
% echo 'print "hello"' | /usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
hello
Or, if you like to just use python to call in command line, then you can change the link that came up by "which python" (e.g., /usr/local/bin).
e.g. (DO NOT COPY AND PASTE THIS - please understand where the paths came from and why):
ln -s PATH_TO_YOUR_PYTHON /usr/local/bin/python
For better discussion about changing default python on OSX, see "How to change which Python version gets used in Snow Leopard?".
Another thing - if you want to run different python from Sublime Text, then take a look at this.
Upvotes: 1
Reputation: 1414
The short answer to your question is that you need to modify your PYTHONPATH environment variable to point to the modules that you want. If you're using bash (which you most likely are), you can do that like this:
export PYTHONPATH=$PYTHONPATH:'/path/to/your/modules/'
However, rather than running the OS X version of python, I would recommend that you run the python version that you installed. If you installed using the .dmg from python.org, you can probably just type 'python2.7' or 'python3' (depending on which version you installed) in the terminal and everything will just work.
If you want to, you can set the command 'python' to run the version of python that you installed rather than the OS X default python by putting in an alias or modifying your PATH environment variable in your ~/.bash_profile file. The easier option is to use the "Update Shell Profile" script that lives in the python folder after the installation.
One more possibility for you to mull over -- consider setting up a virtual environment with virtualenv, and installing scipy, numpy, etc. in there. This option might be more trouble than it's worth when you're first starting out, but in the long run it's a good skill to have and will make your development cleaner and easier to maintain.
Finally, since it sounds like you might be doing scientific computing, you could take a look at EPD free, which comes with many scientific packages preinstalled, and as a bonus will setup the necessary shell variables for you automatically.
Upvotes: 1