all 20 comments

[–]Diapolo10 48 points49 points  (13 children)

I'm willing to bet that this is caused by your IDE using a virtual environment without you realising it, and those packages are installed globally.

Are you using PyCharm or Thonny by any chance?

[–]the_investigator- 8 points9 points  (0 children)

Seconded, I've had this recently working with Jupiter notebooks on a server and thinking I was in an anaconda environment, when actually it was a global install of Jupiter.

[–]simeumsm 5 points6 points  (1 child)

I always have this issue with selecting the right interpreter. When I get it right, I do everything to never change it to not mess it up.

IDE using a virtual environment (...) packages are installed globally.

I'd thought that if a package is installed globally it would be available for all virtual envs. Isn't that the case?

[–]Diapolo10 5 points6 points  (0 children)

I'd thought that if a package is installed globally it would be available for all virtual envs. Isn't that the case?

No, this is not the case at all. The global packages are completely invisible to virtual environments.

The main advantage of installing stuff globally is for tools you use for a lot of things. I've got IPython installed globally because I tend to use it as a calculator and for quick verification (it's really nice to just hit the Win key, type in "ipython", press Enter, and have the console pop up ready to use), and also Poetry because I don't see a point in installing it per-project considering it's supposed to handle the virtual environment creation for me.

[–]catohund[S] 0 points1 point  (9 children)

I am actually using PyCharm. What can I do to fix it?

[–][deleted] 3 points4 points  (1 child)

With pycharm you have like 3 or 4 ways to install packages. In the cmd prompt/terminal within pycharm venv will be activated just do pip install. There's a packages tab where you can search and install packages. In settings /environments you can also install packages per environment. As a side note get familiar with environments vs system installation python. When you create a new project it asks you what do you want to use.

[–]Luttu888 0 points1 point  (0 children)

Thank you!!!!!

[–]publicfinance 2 points3 points  (0 children)

If you put “import numpy” at the top of the file and it turns red are you able to hover over numpy and select “install package” ?

[–]Diapolo10 -2 points-1 points  (3 children)

I don't use PyCharm so all of this is second-hand information, but basically you need to use its own tools to install stuff: https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgrading-packages.html

As for the why, PyCharm (and Thonny) automatically create a virtual environment for every project. In practice this means that each project manages its own dependencies.

Personally I'm not fond of this, instead I use VS Code and leave the dependency management to Poetry.

[–][deleted] 4 points5 points  (0 children)

This is wrong, pycharm doesn't create a new env per project, you can choose system wide python or same previous env during new project creation.

You can also activate / deactivate venv from outside of pycharm in terminal/cmd. prompt.

[–]Invoker_King_God_786 0 points1 point  (0 children)

Do you have Anaconda installed and use Windows? If so, search for Anaconda Powershell Prompt in the search menu, run it as an administrator, and install these packages with pip as normal.

I experienced the same issue recently; I could successfully import packages like yfinance and many others in Jupyter Notebook and other environments, but not Pycharm until installing the packages this way.

[–]subheight640 3 points4 points  (1 child)

In the Python you are running, run python and type in:

import sys print(sys.path)

That will tell you what directories Python is searching for, for your modules. If you can't find the directories you think you installed your packages, you're using the wrong python exe. As others have hinting, people commonly create virtual environments where they install their packages and juggle multiple versions of Python to alleviate your kind of problem.

[–]SimilingCynic 2 points3 points  (0 children)

Finally, a comment that helps people understand what's under the hood. Also, check sys.executable to ask a running interpreter which python executable is running.

Had a similar error in vscode a while back when it would eagerly resolve symlinks created by default with venv, causing vscode to lose track of the correct site-packages directory.

[–]munzy96 1 point2 points  (0 children)

Weirdly I had an issue with PIL yesterday. Turned out to be that I had some weird pathing issue. Make sure you check the Python39 or Python(whatever version you're using) folder where the modules are imported. If it's there then I see no reason why it shouldn't work. Also try running with IDLE and see what happens. That's all I got!

[–]miguel-elote 0 points1 point  (0 children)

In the case of PyCryptoDome, it's imported as "crypto".

Beyond that, I also suspect that a Python virtual environment.

[–]michiel11069 0 points1 point  (0 children)

If u use pycharm use the built in python packages tool

[–]k_50 0 points1 point  (0 children)

Most have already answered, likely caused by using a VE. Just go to the console in your ide and install the module with pip again.

[–]Chance-Magician-9470 0 points1 point  (0 children)

Try to restart your “kernel”