Numpy is not working

I have followed all the steps for:

  • pip install numpy

But when I run “import numpy” in python, I get the error message “module numpy not found”.

Mike

Two possibilities come to mind:

  1. Did pip install numpy complete successfully? You said that you ran it, but not what the output was. Perhaps something went wrong.
  2. Was the pip you used tied to the installation of python you are trying to import numpy in? There might be multiple versions on your system, and pip might point somewhere unexpected. A simple way to ensure this is to use the (recommended) installation method python -m pip install numpy, which will help make sure that the python binary is the same in both cases.

Hi,

you might have the same problem another user had about ~two weeks ago. The issue arises when
downloading a new module into the search path of a different Python version. For example, if you
are using Python v3.12 but numpy was downloaded into your v3.8, then you will run into this issue. So that you don’t run into this issue, uninstall ALL previous versions from your system and make sure you have the latest and greatest version of Python installed. You will have to perform the pip install again since I am assuming the numpy library was downloaded to the earlier version of the Python directory of which you have now uninstalled.

An easy way to tell is execute the following code snippet:

import sys
paths = sys.path
num = 1
for path in paths:
    print('{}. {}'.format(num, path))
    num += 1

There, you will see your active PYTHONPATH. In the search paths, you’ll see the active
search directories including python versions.

Update:
If you don’t want to uninstall your previous version, then make sure the directory is part of the active PYTHONPATH via the following commands:

 sys.path.append(r'C:\folder1\folder2\folder_n')  # Add your directory path here

where C:\folder1\folder2\folder_n is the location of the numpy library package.

Or open the same IDLE version where your numpy library package was downloaded to., of course. :wink:

Somewhat prefer:

>>> import sys, pprint
>>> pprint.pprint(sys.path)

But yes. The important thing @MikeSmith is to do it in the script where your import didn’t work.

Then, where you used the pip command pip debug will tell you what Python it runs with and what site-packages it comes from. This is also where it installed numpy (if it worked).

Are they the same?

I’d caution against this approach because:

  1. A package installed for one Python does not necessarily work in another.
  2. Your code is not portable: it only runs on your machine with these directories.

It’s better to figure out why pip and your script are not using the same installation and solve this by setting your executable search path consistently.

1 Like

Jeff:

import sys, pprint
does nothing

pprint.pprint(sys.path)
prints out several paths as per the attached screen capture.

Which path is correct?

Mike

James:
The answer I got was that numpy was installed already.

I am attaching a screen capture.

Mike

Paul:

I ran the code and got results as per the screen capture.

Which path is best?

Mike

Open a new Python IDLE editor and type the following:

import os
os.getcwd()

what do you get? This is to make sure and compare that the right IDLE version is the default (in this case v3.12 as per the list printed above)

By the way, the numpy package should have been installed here (at least on my system):
C:\Users\mycom\AppData\Local\Programs\Python\Python312\Lib\site-packages

Have you tried importing numpy again? It looks like the defaults are pointing to the latest IDLE. Can you verify by trying the following again:

import numpy

Check here for the numpy download location on your system (copy and paste on a file search header):
C:\Users\donca\AppData\Local\Programs\Python\Python312\Lib\site-packages\numpy

This screenshot shows that you are using a virtual environment (venv). By default, venv’s do not have access to packages installed globally, as in your 2 previous screenshots. Either create the venv with --system-site-packages, or use the pip command from the venv (C:\Users\donca\PycharmProjects\Test02\.venv\Scripts\pip.exe) to install numpy.

The location listed by @MikeSmith is from a Windows Store installation of Python which is just as usable as a python.org installation (it is the exact same files in both).

It’s not a question of which is right or best. This is the list of places the Python interpreter will look for imports. All we’re doing here is exploring why numpy is not found.

I can see from this and the answer you gave James @jamestwebber that pip has installed numpy in one of these places (the one with site-packages in the name). I’m fairly confident that if you had typed import numpy in the Python session you started at the console it would have worked.

Now, when you answer Paul @onePythonUser , I can see that you are running his suggested script in PyCharm, and as Jeremy @jkloth effectively points out, PyCharm is running your code in a virtual environment, which is a sort of isolated installation of Python just for your project. It has its own site-packages, not the one where pip installed numpy. I would guess the place where import numpy didn’t work for you was in a PyCharm project. That’s why I wrote:

It seems annoying at the moment, but it is in fact very useful to have your project isolated like this, so it works the same way whatever you may have installed for some other project.

The easy solution is to get PyCharm to install numpy for that project. At the place where you import numpy in the PyCharm editor, without installing it for the project, it should catch your mistake and if you hover the mouse on the red underline, it will offer to do the installation, like this:
image

By the way, if you’re posting code or error messages, people prefer that you not use an image, but paste the code than mark it up using the “</>” button or by typing back-ticks. I used an image here so you could see the hover.

1 Like

Jeff:

Yes, it worked! I think I am out of the woods.

Thank you so much.

Mike

Thank you Jeremy:

You are right, I downloaded the numpy package from the Microsoft Store.

I have added the link for Python.org to me favourites.

Thanks again.

Mike

Jeremy:
I eventually installed numpy by hovering over “import numpy” and followed the prompts as per Jeff’s advice.
Its outlined above.
It worked well.
Mike

Paul:
I have numby working now.

I ran the code you suggested and got the screen below.

Mike

Glad to know you got it figured out. :slightly_smiling_face:

For future reference, make sure when you are installing Python library packages, that they are being
installed to your active PYTHONPATH and not to either a previous version of Python or some virtual environment.

Cheers!

Paul:

Just for clarification.

I have learned how to install an application from inside PyCharm. That’s how I got numpy to work.

Is that the method to use to assure that it goes into the PyhthonPath?

Mike

As a disclaimer, I don’t and have never used PyCharm. However, I think that if you are using PyCharm, it will have its own designated PYTHONPATH just like IDLE does (which is what I use).

For example:

PyCharm → PYTHONPATH_1 (the path that PyCharm will search when importing modules)

IDLE → PYTHONPATH_2 (the path that IDLE will search when importing modules)

PYTHONPATH_1 != PYTHONPATH_2  # they may not be equal

Note here that I have added the _1 and _2 as arbitrary (to tell them apart). So, when I download library packages, they will be downloaded to the path as designated by python.org (IDLE). Being that you use PyCharm, it will have its own designated download path. So, what is the take away here, make sure that you are consistent with the environment that you are using or at the very least keep tabs on the path where you’re downloading the library packages to.

Remember, PYTHONPATHs are where your system searches for importing library packages that either come prepackaged with your Python environment or the ones that you download via pip.

Next time you run into this issue, you now have the experience to rectify it.

Thanks Paul:

Mike