1

I'd like to use numpy 1.9 without deleting the version of numpy (1.8) that came with my Mac:

>>> import numpy
>>> numpy.version.version
'1.8.0rc1'
>>> numpy.__file__
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/__init__.pyc'

Since that version of numpy is in System, I'd rather not delete it. That said, pip recognizes that I have numpy 1.9 installed but I don't know how to reference it in my import statement.

$ pip list
...
numpy (1.9.0)
...

Thoughts?

jds
  • 7,910
  • 11
  • 63
  • 101

2 Answers2

1

It is important to note that there are two different installations of Python on your Mac. There is the System Python (/usr/bin), and also the /usr/local/bin python.

There are also two installations of pip. For example:

$ which pip
/usr/local/bin/pip
$ ls -l /usr/local/bin/pip
lrwxr-xr-x  1 dmao  admin  30 Feb 14 19:09 /usr/local/bin/pip -> ../Cellar/python/2.7.9/bin/pip

This is the homebrew pip. I assume you have numpy installed on the homebrew version of pip.

There is no System version of pip installed by default. The usual solution is to run easy_install pip and install a system version of pip, then pip install numpy (using system pip). However, you mentioned you wanted to leave the system numpy.


If you need to leave the system numpy untouched, you can run the /usr/local Python as your default Python instead of the system Python. Here we create a symbolic link from the default python to the local python, so that the local python becomes the default.

sudo ln -s /usr/bin/python /usr/local/bin/python

Then your default Python version becomes the one which matches your default version of pip.

You can restore your default Python version anytime by replacing the symlink. /usr/bin has the links you need.

$ ls -l /usr/bin/ | grep python
lrwxr-xr-x   1 root   wheel        76 Feb 21  2014 pythonw2.5 -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/pythonw2.5
lrwxr-xr-x   1 root   wheel        76 Feb 21  2014 pythonw2.6 -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/pythonw2.6
lrwxr-xr-x   1 root   wheel        76 Feb 21  2014 pythonw2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7

Alternatively, if your System Python is being used for something, and/or you need to keep switching between versions of python packages, you could use virtualenv, which makes this much easier.


There are many different ways to manage python modules on a Mac. For example, What is the most compatible way to install python modules on a Mac?

Community
  • 1
  • 1
icedtrees
  • 6,134
  • 5
  • 25
  • 35
  • This is pretty dumb, but I wasn't using `virtualenv` correctly. I assumed that any Python executing in a directory with a `virtualenv` subdirectory automatically pulled dependencies from the `virtualenv`. Once I activated the env, installed numpy 1.9 and then executed my code, I was fine. Thanks. – jds Feb 24 '15 at 16:04
0

For anyone reaching this in 2018, the proper way to do this is to make sure your brew packages folder precedes system packages folder in PATH:

  1. Brew install <mypackage> - python or numpy in this case
  2. Test that your brew package is now being found instead of system package by typing which <package> in terminal. It should say /usr/local/bin/<package> instead of /usr/bin/<package>.
  3. Explanation: System packages are found in /usr/bin while brew packages start in /usr/local/.. e.g /usr/local/bin, /usr/local/opt/<package>/bin.
  4. If system package is being found instead, test your PATH order by typing echo $PATH in your terminal. Is /usr/local/.. appearing earlier than system path?
  5. If not, you need to make sure /usr/local/bin comes first before /usr/bin so that the brew-installed executable is found before the system executable.
  6. To edit PATH: Run export PATH=/usr/local/bin:$PATH in terminal and also put it in ~/.bash_profile config file to make it permanent
  7. Test your PATH changes worked (echo $TEST), and that your brew package is being found instead of system package (which <package>)
  8. General Info: Brew will install packages in /usr/local/Cellar/.. and symbolically link the binaries (executables) to /usr/local/bin files
  9. Troubleshooting: Note that Brew can sometimes install packages with non-standard names like Python3, gcc-7 etc. So even if you have /usr/local/bin before /usr/bin in your PATH, when you test with which python it will still point to /usr/bin/<package>. In this case you need to manually create a symlink in your /usr/local/bin with the expected executable name. E.g in /usr/local/bin do ln -s python3 python
a20
  • 5,495
  • 2
  • 30
  • 27