65

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 ...

CristiFati
  • 38,250
  • 9
  • 50
  • 87
Martinecko
  • 1,719
  • 4
  • 22
  • 35
  • if you have issues with pip install -e . installs see: https://stackoverflow.com/a/73418320/1601580 or if the language server doesn't underline things correct for you in the ide. – Charlie Parker Aug 19 '22 at 14:46

24 Answers24

61

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.

On Pycharm professional 2022.3

Open File -> Settings -> Python Interpreter, open the drop-down and pick "Show All..." (to edit the config) (1), right click your interpreter (2), click "Show Interpreter Paths" (3).

In that screen, manually add the "site-packages" directory of the virtual environment [looks like .../venv/lib/python3.8/site-packages (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.

Gulzar
  • 23,452
  • 27
  • 113
  • 201
Todor Minakov
  • 19,097
  • 3
  • 55
  • 60
  • 2
    This was exactly what I needed to fix this issue after having to reset my virtual environments after a python version upgrade via homebrew. – blord-castillo Jan 08 '20 at 18:51
  • The packages of the added paths didn't show up after I did this. However, it seems PyCharm was able to index to them because my code was detecting the libraries (i.e. not giving me squiggly red error lines) – jjkl Sep 25 '22 at 11:19
  • Though this works, I don't see why was `site_packages` not added in the first place, and how to avoid this every time I create a remote interpreter. – Gulzar Dec 14 '22 at 13:04
  • Also, How I couldn't find `Associate this virtual environment with the current project`. Can you please point toward it? – Gulzar Dec 14 '22 at 13:18
  • [Here](https://pasteboard.co/SNOGIyTLWsd5.png) is the default mapping Pycharm generates. You may also want to add `/usr/lib/python3.8`, `/usr/lib/python3.8/lib-dynload` in addition to what is stated in the answer. If your base interpreter is in `/usr/local/bin/python3.8`, use `/usr/local...` instead of just `/usr...`. – Gulzar Dec 14 '22 at 14:44
44

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

File->Default Settings->Project Interpreter

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.

Gulzar
  • 23,452
  • 27
  • 113
  • 201
user7650698
  • 537
  • 4
  • 2
15

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: gym@openai.com
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.

whiletrue
  • 10,500
  • 6
  • 27
  • 47
  • 4
    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 – X HOxha Sep 12 '20 at 02:08
  • Really helpful! By the way, in PyCharm CE 2020.2 the Go to step needs to be updated to Preferences -> Project -> Python Interpreter -> gear wheel -> Show All -> Select relevant interpreter -> click on tree icon (Show paths for the selected interpreter) – MichaelMaggs Nov 22 '20 at 12:17
  • 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? – ChrisDanger Apr 13 '22 at 15:27
10

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 :)

Ketav Sharma
  • 429
  • 4
  • 8
8

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

NightFury
  • 13,436
  • 6
  • 71
  • 120
7

Using dual python 2.7 and 3.4 with 2.7 as default, I've always used pip3 to install modules for the 3.4 interpreter, and pip to install modules for the 2.7 interpreter.

Try this:

pip3 install requests

insecure-IT
  • 2,068
  • 4
  • 18
  • 26
  • 1
    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. – lokilindo Aug 13 '16 at 02:55
  • One should specify the interpreter explicitly: `python3 -m pip install requests`. – Konstantin Sekeresh Jan 07 '19 at 19:21
7

This is because you have not selected two options while creating your project:- ** inherit global site packages ** make available to all projects Now you need to create a new project and don't forget to tick these two options while selecting project interpreter.

3

The solution is easy (PyCharm 2021.2.3 Community Edition). I'm on Windows but the user interface should be the same. In the project tree, open External libraries > Python interpreter > venv > pyvenv.cfg. Then change:

include-system-site-packages = false

to:

include-system-site-packages = true

PyCharm image

David Lopez
  • 353
  • 4
  • 13
3

Before going further, I want to point out how to configure a Python interpreter in PyCharm:
[SO]: How to install Python using the "embeddable zip file" (@CristiFati's answer). Although the question is for Win, and has some particularities, configuring PyCharm is generic enough and should apply to any situation (with minor changes).

There are multiple possible reasons for this behavior.

1. Python instance mismatch

  • Happens when there are multiple Python instances (installed, VEnvs, Conda, custom built, ...) on a machine. Users think they're using one particular instance (with a set of properties (installed packages)), but in fact they are using another (with different properties), hence the confusion. It's harder to figure out things when the 2 instances have the same version (and somehow similar locations)

  • Happens mostly due to environmental configuration (whichever path comes 1st in ${PATH}, aliases (on Nix), ...)

  • It's not PyCharm specific (meaning that it's more generic, also happens outside it), but a typical PyCharm related example is different console interpreter and project interpreter, leading to confusion

  • The fix is to specify full paths (and pay attention to them) when using tools like Python, PIP, .... Check [SO]: How to install a package for a specific Python version on Windows 10? (@CristiFati's answer) for more details

  • This is precisely the reason why this question exists. There are 2 Python versions involved:

    1. Project interpreter: /Library/Frameworks/Python.framework/Versions/3.4

    2. Interpreter having the Requests module: /opt/local/Library/Frameworks/Python.framework/Versions/3.4

    well, assuming the 2 paths are not somehow related (SymLinked), but in latest OSX versions that I had the chance to check (Catalina, Big Sur, Monterey) this doesn't happen (by default)

When dealing with this kind of error, it always helps (most likely) displaying the following information (in a script or interpreter console):

import os
import sys


print("Executable:", sys.executable)
print("Version:", sys.version)
print("CWD:", os.getcwd())
print("UName:", getattr(os, "uname", lambda: None)())
for evn in ("PATH", "PYTHONHOME", "PYTHONPATH"):
    print("{:s}: {:}".format(evn, os.environ.get(evn)))
print("sys.path:", sys.path)

2. Python's module search mechanism misunderstanding

3. A PyCharm bug

4. A glitch

  • Not likely, but mentioning anyway. Due to some cause (e.g.: HW / SW failure), the system ended up in an inconsistent state, yielding all kinds of strange behaviors

  • Possible fixes:

    Needless to say that the last 2 options should only be attempted as a last resort, and only by experts, as they might mess up other projects and not even fix the problem

Not directly related to the question, but posting:

CristiFati
  • 38,250
  • 9
  • 50
  • 87
2
  1. If you go to pycharm project interpreter -> clicked on one of the installed packages then hover -> you will see where pycharm is installing the packages. This is where you are supposed to have your package installed.

  2. Now if you did sudo -H pip3 install <package> pip3 installs it to different directory which is /usr/local/lib/site-packages

since it is different directory from what pycharm knows hence your package is not showing in pycharm.

Solution: just install the package using pycharm by going to File->Settings->Project->Project Interpreter -> click on (+) and search the package you want to install and just click ok.

-> you will be prompted package successfully installed and you will see it pycharm.

Pingolin
  • 3,161
  • 6
  • 25
  • 40
Myralyn
  • 21
  • 1
2

If any one faces the same problem that he/she installs the python packages but the PyCharm IDE doesn't shows these packages then following the following steps:

  1. Go to the project in the left side of the PyCharm IDE then
  2. Click on the venv library then
  3. Open the pyvenv.cfg file in any editor then
  4. Change this piece of code (include-system-site-packages = flase) from false to true
  5. Then save it and close it and also close then pycharm then
  6. Open PyCharm again and your problem is solved. Thanks
1

This did my head in as well, and turns out, the only thing I needed to do is RESTART Pycharm. Sometimes after you've installed the pip, you can't load it into your project, even if the pip shows as installed in your Settings. Bummer.

Beatrix Kidco
  • 402
  • 5
  • 12
1

For Anaconda:

Start Anaconda Navigator -> Enviroments -> "Your_Enviroment" -> Update Index -> Restart IDE.

Solved it for me.

Andreas
  • 8,694
  • 3
  • 14
  • 38
0

After pip installing everything I needed. I went to the interpreter and re-pointed it back to where it was at already. My case: python3.6 in /anaconda3/bin/python using virtualenv...

Additionally, before I hit the plus "+" sign to install a new package. I had to deselect the conda icon to the right of it. Seems like it would be the opposite, but only then did it recognize the packages I had/needed via query.

0

In my case the packages were installed via setup.py + easy_install, and the they ends up in *.egg directories in site_package dir, which can be recognized by python but not pycharm.

I removed them all then reinstalled with pip install and it works after that, luckily the project I was working on came up with a requirements.txt file, so the command for it was:

pip install -r ./requirement.txt

Ripley
  • 664
  • 1
  • 6
  • 16
0

I just ran into this issue in a brand new install/project, but I'm using the Python plugin for IntelliJ IDEA. It's essentially the same as PyCharm but the project settings are a little different. For me, the project was pointing to the right Python virtual environment but not even built-in modules were being recognized.

It turns out the SDK classpath was empty. I added paths for venv/lib/python3.8 and venv/lib/python3.8/site-packages and the issue was resolved. File->Project Structure and under Platform Settings, click SDKs, select your Python SDK, and make sure the class paths are there.

enter image description here

Dave Wolfe
  • 660
  • 5
  • 14
0
pip install --user discord

above command solves my problem, just use the "--user" flag

Siddy Hacks
  • 1,846
  • 14
  • 15
0

I fixed my particular issue by installing directly to the interpreter. Go to settings and hit the "+" below the in-use interpreter then search for the package and install. I believe I'm having the issue in the first place because I didn't set up with my interpreter correctly with my venv (not exactly sure, but this fixed it).

I was having issues with djangorestframework-simplejwt because it was the first package I hadn't installed to this interpreter from previous projects before starting the current one, but should work for any other package that isn't showing as imported. To reiterate though I think this is a workaround that doesn't solve the setup issue causing this.

enter image description here

blueblob26
  • 75
  • 9
0

If you are having issues with the underlying (i.e. pycharm's languge server) mark everything as root and create a new project. See details: https://stackoverflow.com/a/73418320/1601580 this seems to happy to me only when I install packages as in editable mode with pip (i.e. pip install -e . or conda develop). Details: https://stackoverflow.com/a/73418320/1601580

Charlie Parker
  • 5,884
  • 57
  • 198
  • 323
0

--WINDOWS-- if using Pycharm GUI package installer works fine for installing packages for your virtual environment but you cannot do the same in the terminal,

this is because you did not setup virtual env in your terminal, instead, your terminal uses Power Shell which doesn't use your virtual env

there should be (venv) before you're command line as shown instead of (PS)

if you have (PS), this means your terminal is using Power Shell instead of cmd

to fix this, click on the down arrow and select the command prompt

select command prompt

now you will get (venv) and just type pip install #package name# and the package will be added to your virtual environment

0

I tried all the answers but non of them worked!

""IF YOU ARE LAZY!! Temporary Answer is : ""

you should consider finding the path PyCharm is really installing libraries via pip in terminal and add that to your project like in the next pictures!

1_ go to interpreter settings :

2_ click on "Show All" :

3_ find your current projects interpreter and click on show Paths for selected interpreter :

4_ Now try adding different paths that you think might pip be installing libraries to and it adds all the libraries from that path to your new project :

MaYSaM
  • 21
  • 3
-1

On windows I had to cd into the venv folder and then cd into the scripts folder, then pip install module started to work

cd venv
cd scripts
pip install module
-1

instead of running pip install in the terminal -> local use terminal -> command prompt

see below image

pycharm_command_prompt_image

1

vimuth
  • 5,064
  • 33
  • 79
  • 116
-3

In your pycharm terminal run pip/pip3 install package_name

Anubhav
  • 1,984
  • 22
  • 17