简体   繁体   中英

How to install numpy and matplotlib in the right python version?

I'm trying to install numpy, matplotlib, and scipy in the right python version.

Initially I was testing with different python versions (3.2, 2.7, 2.6).

I removed all these versions using: How to uninstall Python 2.7 on a Mac OS X 10.6.4?

Afterwards, I reinstalled Python 2.7.11. when I try to install numpy, matplotlib and scipy, using pip, I get the following message: Requirement already satisfied (use --upgrade to upgrade) ...

In my terminal, I tried the following:

$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python

$ python
Python 2.7.11 (v2.7.11:.....)
.....
>>> import numpy
ImportError: No module named numpy

$ /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python
Python 2.7.10 (default, ......
.....
>>> import numpy
>>> numpy.__version__
'1.8.0rc1'

for some reason these packages got installed in 2.7.10 and not 2.7.11, which is the version I downloaded from python.org. Also, I don't even know how I got the 2.7.10 version.

How can I fix this issue?

You can also use macports ( https://www.macports.org/ ) to install different versions of python, numpy, and matplotlib. It's really quite simple.

Alternatively, you can perhaps use anaconda ( https://www.continuum.io/downloads ), which uses conda, to achieve your goal.

I recommend using virtualenv (with virtualenvwrapper: https://virtualenvwrapper.readthedocs.org ). It is very easy to setup and you'll have absolutely no problems in future when you deal with multiple Python installations.

I work with virtualenv for years now and create for each project a separate virtual environment, which is always clean and I never have to deal with PATH , PYTHONPATH or whatever.

If you followed the virtualenvwrapper installation guide, you can simply create for example one virtualenv for everyday work via:

mkvirtualenv common -p /Library/Frameworks/Python.framework/Versions/2.7/bin/python

this will create the virtualenv and automatically activate it, so you can instantly install the packages you want:

pip install matplotlib numpy scipy

and every time you want to use it you type:

workon common

As you see above, you can specify the python executable via the -p flag. Each virtualenv will be a completely fresh and independent Python installation where you can use pip to install whatever you want (without root access of course).

It is likely to mean that you used pip or easy_install from another python version.

When you install your modules, make sure to use the correct pip version.

It might be /usr/local/bin/pip2.7 for example.

If you install Anaconda from continuum.io, you'll get access to versions of many packages that have been tested to work with the version of Python that you are interested in working with. Here's the list that come with the current version of their distribution.

You also get access to conda , which is a package and environment manager. Think pip + virtualenv.

Once you have that, you can do

conda create -n my_env python=3.6 numpy pandas

This will install Python 3.6 and all of the dependencies for numpy and pandas into a virtual environment called my_env . Conda will make sure that you have the most up to date packages that work together.

To access your environment, you can do:

activate my_env

Now you're running Python in that environment with those installed packages. If you need more packages, you can either do conda install package_name . If conda can't find the package, you can still do pip install package_name .

Note that as an added bonus, you get an optimized and pre-compiled version of Numpy by way of the Intel MKL.

(From my comment on a previous answer)

I'd second the recommendation for going the Anaconda route. Particularly if you're using Numpy or anything that depends on Numpy (Pandas, Scipy, Sci-kit Learn). Continuum has access to the Intel MKL which gives you significant optimizations and pre-compiled C code specific to your operating system. docs.continuum.io/mkl-optimizations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM