33
votes

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

15
what does which -a python output? - Padraic Cunningham
You should set up a virtualenv for the project and install requests in that. - jonrsharpe
which -a python outputs following: 23:47:55|mymac [~]:which -a python /opt/local/bin/python /usr/bin/python - Matthew Lowe
Then pip is installing for /usr/local/bin/python3. set your interpreter to /usr/local/bin/python3. If you usually use Python 3.4.3 I would remove the other, having a few different versions of the same python can be a pain as you have found out. It is safe to remove, you only need to be careful when dealing with your system python. - Padraic Cunningham
Your system python would be 2.7 which is /usr/bin/python, your OS would use your system python so removing or changing it would almost certainly break it, you can remove python3 safely and /opt/local/bin/python also . As john suggested you can use a virtualenv for different projects which is an isolated environment virtualenv.pypa.io/en/latest. I also like pyenv github.com/yyuu/pyenv. So keep your /usr/bin/python always and you can remove or keep any of the rest. - Padraic Cunningham

15 Answers

38
votes

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

27
votes

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.

10
votes

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

7
votes

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

7
votes

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

7
votes

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.

5
votes

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.

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

1
votes

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.

0
votes

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
votes

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

0
votes

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
0
votes

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

0
votes

I got this issue when I created the project using Virtualenv.

Solution suggested by Neeraj Aggarwal worked for me. However, if you do not want to create a new project then the following can resolve the issue.

  1. Close the project
  2. Find the file <Your Project Folder>/venv/pyvenv.cfg
  3. Open this file with any text editor
  4. Set the include-system-site-packages = true
  5. Save it
  6. Open the project
-2
votes

In your pycharm terminal run pip/pip3 install package_name