Cannot use Python3 numpy (or matplotlib etc.) in my mcbook pro from outside VSCode

Viewed 2643

(Similar questions I have found on StackOverflow are answered for a different OS)

I'm facing some issues using numpy in my new macbook (os Big Sur).

I've used pip3 to install numpy etc but it only seems to work inside VSCode.

pip3 install numpy 

Inside VSCode everything runs smoothly, even the ipynbs.

But whenever I try to run numpy using either a terminal or Jupyter notebook

import numpy as np

I'm getting the following error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
~/Library/Python/3.8/lib/python/site-packages/numpy/core/__init__.py in <module>
     21 try:
---> 22     from . import multiarray
     23 except ImportError as exc:

~/Library/Python/3.8/lib/python/site-packages/numpy/core/multiarray.py in <module>
     11 
---> 12 from . import overrides
     13 from . import _multiarray_umath

~/Library/Python/3.8/lib/python/site-packages/numpy/core/overrides.py in <module>
      6 
----> 7 from numpy.core._multiarray_umath import (
      8     add_docstring, implement_array_function, _get_implementing_args)

ImportError: dlopen(/Users/ahsantarique/Library/Python/3.8/lib/python/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so, 2): no suitable image found.  Did find:
    /Users/ahsantarique/Library/Python/3.8/lib/python/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so: mach-o, but wrong architecture
    /Users/ahsantarique/Library/Python/3.8/lib/python/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so: mach-o, but wrong architecture

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
<ipython-input-2-0aa0b027fcb6> in <module>
----> 1 import numpy as np

~/Library/Python/3.8/lib/python/site-packages/numpy/__init__.py in <module>
    138     from . import _distributor_init
    139 
--> 140     from . import core
    141     from .core import *
    142     from . import compat

~/Library/Python/3.8/lib/python/site-packages/numpy/core/__init__.py in <module>
     46 """ % (sys.version_info[0], sys.version_info[1], sys.executable,
     47         __version__, exc)
---> 48     raise ImportError(msg)
     49 finally:
     50     for envkey in env_added:

ImportError: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.8 from "/Library/Developer/CommandLineTools/usr/bin/python3"
  * The NumPy version is: "1.19.4"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: dlopen(/Users/**/Library/Python/3.8/lib/python/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so, 2): no suitable image found.  Did find:
    /Users/**/Library/Python/3.8/lib/python/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so: mach-o, but wrong architecture
    /Users/**/Library/Python/3.8/lib/python/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so: mach-o, but wrong architecture
2 Answers

I've found a workaround.

Seems like the problem was I didn't have the python bin in the path variable

So I removed numpy completely first

pip3 uninstall numpy

added the path

PATH=$PATH:the/python/bin/directory/that/was/showing

Then reinstalled numpy

pip3 install numpy

I'm not sure if that's the way to go but it solved this ImportError for now.

VSCode lets you choose which interpreter you are using,This is most probabaly your probelm because VSCode is using a differnt interpreter,with different pacakages, than your system.

The fisrt step is:

First you need to know what version of python your system is using, to know which version of python your system uses type python3 --version in the terminal

The second step is:

Then you need to make VSCode use the same version of python as your system ,to do this type ctrl+shift+p to open the command palette in VSCode then in the command palette type or choose “Python: Select Interpreter then select the same version that your system is using.

Select intrpreter part 1

Select inetrepreter part 2

The last step is:

Since the system install of python doesnt have numpy installed you need to install it by typing pip3 install numpy

Related