简体   繁体   中英

Unable to import python modules (numpy, scipy, matplotlib)

I've used the following tutorial to install Python, numpy, scipy and matplotlib:

https://penandpants.com/2012/03/01/install-python-2/

I downloaded each and installed by double-clicking. I'm using a Mac with OS X Yosemite Version 10.10.5

When I try to import numpy, scipy and matplotlib in IDLE I get the follow error message:

`>>> import numpy

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    import numpy
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/__init__.py", line 137, in <module>
    import add_newdocs
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/add_newdocs.py", line 9, in <module>
    from numpy.lib import add_newdoc
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/__init__.py", line 4, in <module>
    from type_check import *
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/type_check.py", line 8, in <module>
    import numpy.core.numeric as _nx
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/__init__.py", line 5, in <module>
    import multiarray
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/multiarray.so, 2): no suitable image found.  Did find:
    /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/multiarray.so: no matching architecture in universal wrapper`

Can anyone please explain to me how to resolve this?

Thanks

use terminal and type

sudo pip install numpy
sudo pip install matplotlib
sudo pip install scipy 

for python3+ use pip3

I can't say for sure what's wrong with your installation, but I suspect it's due to a some PATH -conusion... Python 2.7 and Numpy should be installed on your Mac by default.

If you run the command which python , you'll see which executable the python -commands invokes... is it the same as the one you installed?

If you write python in the terminal, and press tab twice, do you see multiple options of installed python versions?

Alternative method of installation

I prefer the Anaconda Python Distribution, which you can download like the full version or minimal version , which is the one I prefer.

If you install this, you should consider uninstalling the one you just installed.

Here's how to install the mini version: Download the installer, and run it by opening a terminal in the folder with the installer, and running the command bash Miniconda2-latest-MacOSX-x86_64.sh . This will start the text-based installer, and if you read the instructions on screen, you'll be fine. Notice that you have to scroll to the end of the licence agreement, before you can write yes when asked to accept the terms stated.

Say yes when it asked you if you'd like it to add Anaconda Python to your path.

Anaconda Python is different from other python distributions in the regard that it comes with it's own package and environment manager conda . With conda you can have multiple versions of Python installed at the same time (or several environments of the same Python version, but with different packages or versions of packages).

To create a new environment with the name py27 , which is using Python 2.7, write conda create -n py27 python=2.7 --yes . Once created, activate the environment by source activate py27 . With the new environment now active, install the packages you need by using conda: conda install scipy matplotlib --yes . I'd also recommend installing Ipython, which is an improved shell, and possibly the Jupyter notebook: conda install ipython --yes and conda install notebook --yes .

Hope it helps.

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