0

I have installed python on my macbook with the python installer from Python.org. Subsequently, I went and installed pip, ipython, and numpy. Everything seemed fine. However, now I am getting the following problem. I can import numpy when I run ipython, but not when I run regular python.

E.g.

Logister-MacBook-Pro:~ Logister$ ipython
Python 2.7.6 (default, Sep  9 2014, 15:04:36)
Type "copyright", "credits" or "license" for more information.

IPython 3.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import numpy

In [2]: import site; site.getsitepackages()
Out[2]:
['/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/Library/Python/2.7/site-packages']

However, when I try to do the same thing in python 2.7.9:

Logister-MacBook-Pro:~ Logister$ python
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>> import site; site.getsitepackages()
['/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', 
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python',
 '/Library/Python/2.7/site-packages']

When I try to install numpy via pip it gives me the following response:

Logister-MacBook-Pro:~ Logister$ sudo -H pip install numpy
Requirement already satisfied (use --upgrade to upgrade): 
numpy in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python

It seems like python 2.7.6 understands where to find numpy, but 2.7.9 does not. Either, how can I point 2.7.9 to the right place, or how can I install numpy so 2.7.9 sees it as well?

Edit: I can run: site.addsitedir('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python') In 2.7.9 and then I am able to import numpy. But I dont want to do this every time I launch 2.7.9. Is there a permanent fix? Also, how do I get ipython to run 2.7.9 instead of 2.7.6?

Logister
  • 1,852
  • 23
  • 26
  • 1
    Try `pip2.7 install numpy` – l'L'l May 16 '15 at 16:23
  • Still get the "requirement already satisfied" response. – Logister May 16 '15 at 16:29
  • I found trying to install modules for multiple versions of python can be a royal `PITA`; you might consider using `virtualenv`, for it might save your sanity. – l'L'l May 16 '15 at 16:40
  • I fairly certain Macs come with a system version of Python installed by default. Using `virtualenv` would be a wise decision to avoid problems like this. Moving things around with you system Python can also cause some parts of your OS or other apps that rely on it to start acting funny – kylieCatt May 16 '15 at 16:51

3 Answers3

0

I had a similar problem. There is two differences versions of python installed on your computer. Then you´ve installed numpy on python 2.7.6. but the folder of python 2.7.9 haven't numpy. Do you understand?

Other thing your macOS have a groups of variables called "Environment Variables" there it's identifying the command python with python 2.7.9.

Then if you want to use numpy with the interpreter of python do you need change the Enviroment Variables for the command "python" take the version 2.7.6 or install numpy at python 2.7.9 and work with both versions.

Good luck!

Adolfo Correa
  • 813
  • 8
  • 17
0

I was able to solve the problem by adding the following line to my .bash_profile:

export PYTHONPATH=${PYTHONPATH}:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python

I then changed the Ipython version by following this stackoverflow question.

Community
  • 1
  • 1
Logister
  • 1,852
  • 23
  • 26
0

With 2 different version you must have 2 python interpreters on your machine. They are installed on different locations with different libraries. So its just right that you cannot find the module installed on interpreter A while using the interpreter B. The which command can be useful to you to figure out where those interpreter are. You can make a symbolic link manually from a folder to another but it would mess up your mind, and you probably gonna get lost later.

I recommed you to install python and ipython via Homebrew, so it would automatically do all the hard work for you.

Install homebrew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Since you said numpy, you are probably looking for scientific stuff so:

# set up some taps and update brew
brew tap homebrew/science # a lot of cool formulae for scientific tools
brew tap homebrew/python # numpy, scipy, matplotlib, ...
brew update && brew upgrade

# install a brewed python
brew install python

Later you can run, but I recommend you to follow this tutorial:

brew install zmq
pip install ipython[all]
CESCO
  • 7,305
  • 8
  • 51
  • 83