PyCharm doesn't recognise installed module

118,415

Solution 1

If you are using PyCharms CE (Community Edition), then click on:

File->Default Settings->Project Interpretor

Screenshot: Interpretor Settings

See the + sign at the bottom, click on it. It will open another dialog with a host of modules available. Select your package (e.g. requests) and PyCharm will do the rest.

MD

Solution 2

In my case, using a pre-existing virtualenv did not work in the editor - all modules were marked as unresolved reference (running naturally works, as this is outside of the editor's config, just running an external process (not so easy for debugging)).
Turns out PyCharm did not add the site-packages directory... the fix is to manually add it.

Open File -> Settings -> Project Interpreter, pick "Show All..." (to edit the config) (1), pick your interpreter (2), and click "Show paths of selected interpreter" (3).

In that screen, manually add the "site-packages" directory of the virtual environment (4) (I've added the "Lib" also, for a good measure); once done and saved, they will turn up in the interpreter paths.

the steps

The other thing that won't hurt to do is select "Associate this virtual environment with the current project", in the interpreter's edit box.

Solution 3

Open python console of your pyCharm. Click on Rerun. It will say something like following on the very first line

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py 52631 52632

in this scenario pyCharm is using following interpretor

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 

Now fire up console and run following command

sudo /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 -m pip install <name of the package>

This should install your package :)

Solution 4

This issue arises when the package you're using was installed outside of the environment (Anaconda or virtualenv, for example). In order to have PyCharm recognize packages installed outside of your particular environment, execute the following steps:

Go to

Preferences -> Project -> Project Interpreter -> 3 dots -> Show All -> Select relevant interpreter -> click on tree icon Show paths for the selected interpreter

Now check what paths are available and add the path that points to the package installation directory outside of your environment to the interpreter paths.

To find a package location use:

$ pip show gym
Name: gym
Version: 0.13.0
Summary: The OpenAI Gym: A toolkit for developing and comparing your reinforcement learning agents.
Home-page: https://github.com/openai/gym
Author: OpenAI
Author-email: [email protected]
License: UNKNOWN
Location: /usr/local/lib/python3.7/site-packages
...

Add the path specified under Location to the interpreter paths, here

/usr/local/lib/python3.7/site-packages

Then, let indexing finish and perhaps additionally reopen your project.

Solution 5

Pycharm is unable to recognize installed local modules, since python interpreter selected is wrong. It should be the one, where your pip packages are installed i.e. virtual environment.

I had installed packages via pip in Windows. In Pycharm, they were neither detected nor any other Python interpreter was being shown (only python 3.6 is installed on my system).

enter image description here

I restarted the IDE. Now I was able to see python interpreter created in my virtual environment. Select that python interpreter and all your packages will be shown and detected. Enjoy!

enter image description here

Share:
118,415
Matthew Lowe
Author by

Matthew Lowe

code &amp; travel

Updated on July 09, 2022

Comments

  • Matthew Lowe
    Matthew Lowe almost 2 years

    I'm having trouble with using 'requests' module on my Mac. I use python34 and I installed 'requests' module via pip. I can verify this via running installation again and it'll show me that module is already installed.

    15:49:29|mymac [~]:pip install requests
    Requirement already satisfied (use --upgrade to upgrade): requests in /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages
    

    Although I can import 'requests' module via interactive Python interpreter, trying to execute 'import requests' in PyCharm yields error 'No module named requests'. I checked my PyCharm Python interpreter settings and (I believe) it's set to same python34 as used in my environment. However, I can't see 'requests' module listed in PyCharm either.

    PyCharm Python interpreter settings

    It's obvious that I'm missing something here. Can you guys advise where should I look or what should I fix in order to get this module working? I was living under impression that when I install module via pip in my environment, PyCharm will detect these changes. However, it seems something is broken on my side ...

  • Jayan
    Jayan about 8 years
    Please add some context to the answer. Do you mean pip3 instead pip will help?
  • lokilindo
    lokilindo over 7 years
    This was actually very helpful for me. I am now using pip3 instead of pip, and PyCharm (on Python3.x) is upgrading and adding the Project Interpreters on its own. Thanks.
  • Mehmet Burak Sayıcı
    Mehmet Burak Sayıcı almost 6 years
    Sometimes that plus sign doesn't install the packages, then I install via pip but Pycharm can't see this, what to do about it?
  • gented
    gented over 5 years
    This answer does not address the problem in the question.
  • Konstantin Sekeresh
    Konstantin Sekeresh over 5 years
    One should specify the interpreter explicitly: python3 -m pip install requests.
  • gust
    gust over 4 years
    But pycharm can't see /usr/local though, no?
  • blord-castillo
    blord-castillo over 4 years
    This was exactly what I needed to fix this issue after having to reset my virtual environments after a python version upgrade via homebrew.
  • X HOxha
    X HOxha over 3 years
    Although in breach of Stack Overflow codes , I can't stress it enough how much you've helped me with the presented answer. I was stuck for 4 hours in front of the screen ,jumping from a question to another. Your solution worked perfectly for me
  • whiletrue
    whiletrue over 3 years
    glad I was able to help you
  • Christian Vincenzo Traina
    Christian Vincenzo Traina over 3 years
    Yes, but why? There are packages that weigh gigabytes, why should I install them twice?
  • MichaelMaggs
    MichaelMaggs over 3 years
    Really helpful! By the way, in PyCharm CE 2020.2 the Go to step need to be updated to
  • MichaelMaggs
    MichaelMaggs over 3 years
    Really helpful! By the way, in PyCharm CE 2020.2 the Go to step needs to be updated to <code>Preferences</code> -> <code>Project</code> -> <code>Python Interpreter</code> -> <code>gear wheel</code> -> <code>Show All</code> -> <code>Select relevant interpreter</code> -> click on tree icon (Show paths for the selected interpreter)
  • ChrisDanger
    ChrisDanger about 2 years
    When I click the tree icon then add I get a finder window that does not show any hidden files, so I am unable to add a path. How did you add the path?
  • Charlie Parker
    Charlie Parker almost 2 years
    my site packages IS there but the editor still underlines in red that it doesn't recognize the name :/