1

Presently I'm using Python on a Windows system. I installed Python 3.10 from Anaconda and also the Pycharm IDE. I have ensured that Python is in the correct path in the environment variable. I have also replicated this problem using two different versions of Python, 3.10 and 3.9.

Very simply, in PyCharm, I open a terminal and type

conda install -c numpy numpy. 

Then, I write a new "main.py" script. I have one line: "import numpy". I receive the error:

Traceback (most recent call last):
  File "C:\Users\---\PycharmProjects\pythonProject3\main.py", line 17, in <module>
    import numpy
ModuleNotFoundError: No module named 'numpy'

What am I doing wrong?

Going on advice from a friend, I created a new PyCharm project sitting not in my user directory but on the C: drive, and got the same error. Finally, when trying to re-install the package using either using either pip or conda, I get this message:

# All requested packages already installed.
wovano
  • 4,543
  • 5
  • 22
  • 49
Abed
  • 183
  • 7
  • Can you show how you invoke your script? Do you get the same problem on the interactive Python prompt? – joanis Jun 15 '22 at 22:12
  • E.g., if you're calling your script via `python3 main.py` but conda only installed the name `python` (which is the default), you could be getting the wrong instance of python. – joanis Jun 15 '22 at 22:12
  • I'm literally just running it in Pycharm. So I don't type "python3 main.py", I just hit the green play button and I get this error. Worth mentioning that when I hit "conda list" or "pip list," it shows that I have numpy installed. – Abed Jun 15 '22 at 22:14
  • So, now I really want to troubleshoot, does Pycharm see the same environment as you get on the command line. Run a script containing just `import os` and then `print(os.__file__)`. Do you get the same answer in both cases? What I get is `path_to_my_current_venv/lib/os.py` so if the result is different, that would be your problem. I'm going at the same thing as @Giorgios's answer, actually. – joanis Jun 16 '22 at 12:25
  • No, these are different lines. The first gives me "C:/Users/.../PycharmProjects/pythonProject5/main.py" and the second gives me "C:\Users\...\anaconda3\envs\pythonProject5\lib\os.py". What should I do to fix this? – Abed Jun 16 '22 at 13:58
  • Then the answers below are correct: you have PyCharm misconfigured and using the wrong environment. I believe the answer from @Giorgos is telling you want to do, but I have never use PyCharm so I cannot help you past this point. Comment under their answer if you don't understand their solution, they might be able to clarify it for you. – joanis Jun 16 '22 at 14:59

3 Answers3

1

There main reason for this is

You are running your main.py in different environment rather than where you installed numpy.

If you trying to run it via cmd use this method

Check which environment you are in right now. refer this and this. But the most easiest way to do this is use where command in windows cmd. C:\> where python or C:\> where python3. You will get the path of activated interpreter.

list conda envs - conda env list

activate conda env - conda activate <env name>

then run this command. pip freeze . and check is there numpy in the list. If not you have to find and activate the environment where you have installed numpy.

If you want to run it in pycharm

Refer this on how to change pycharm interpreter. https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html

Kavindu Ravishka
  • 711
  • 4
  • 11
1

You have 2 versions of Python:

  1. Default Python (used everytime you open your command prompt and type python or python3)
  2. Anaconda is installing packages in a virtual environment, using it's own Python (it is located in a different path)

You can see the path of your installed python using python -c "import os, sys; print(os.path.dirname(sys.executable))"

You have 2 Options:

  1. Configure the PyCharm in order to use the anaconda Python. https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html#view_list
  2. Open a command prompt in the project's folder (you can do it easily using PyCharm). Type conda env list. This will show you all available anaconda virtual environments. Choose 1 of them and type conda activate <env_name>, where <env_name>=the name of the environment. Then, run your program using python <name_of_your_program>

You can see the paths where the anaconda environments and packages are installed using conda info

Ermolai
  • 303
  • 4
  • 15
0

Many things can cause this, usually its one of these

  1. You may have to restart your terminal, or IDE if running in there, after installing a package to "refresh" the environmental path
  2. The package is not in the environmental path
tms2022
  • 42
  • 4
  • So I've restarted PyCharm and Anaconda several times. I've made sure that the environment path variables include a path to the version of Python I am using. I have made sure that the package is installed in the environment (eg, I have typed "conda list" or "pip list," the package shows up. – Abed Jun 15 '22 at 22:25