How do I use installed packages in PyCharm?
Asked Answered
C

14

218

In PyCharm I've added the Python environment /usr/bin/python and GNU Radio works fine outside of PyCharm. However, importing it fails in PyCharm with an undefined reference.

from gnuradio import gr

GNU Radio is located at /usr/local/lib/python2.7/site-packages/gnuradio and I've added it to the PYTHONPATH.

My PYTHONPATH is /usr/local/lib/python2.7/site-packages:/usr/local/lib/python2.7/site-packages/gnuradio.

Curious answered 10/11, 2013 at 3:7 Comment(3)
Ok, so how did you install gnuradio?Polio
You can install gnuradio from the available repositories in PyCharm. PyCharm -> Preferences (Settings on Windows) -> Project -> Project Interpreter -> click on '+' sign in packages section and search for the required package. You may have to add repositories if you the required package isn't available in already added repositories. HTH.Desuetude
For me there is also another issue. If you try to add a folder that in the past had a .idea folder, but your current project has it's own .idea folder your pycharm might get confused for some reason -- even if you have the right python/conda env. For me deleting the .idea folder of the other project fixed the confusion that it could find the obviously correctly installed pkgs. Then it was it was able to find them in the pycharm editor GUI snf stopped underlyinging them in red.Busty
L
246

Adding a Path

Go into File → Settings → Project Settings → Project Interpreter.

Then press configure interpreter, and navigate to the "Paths" tab.

pycharm path tab

Press the + button in the Paths area. You can put the path to the module you'd like it to recognize.

But I don't know the path..

Open the python interpreter where you can import the module.

>> import gnuradio
>> gnuradio.__file__
"path/to/gnuradio"

Most commonly you'll have a folder structure like this:

foobarbaz/
  gnuradio/
    __init__.py
    other_file.py

You want to add foobarbaz to the path here.

Labuan answered 10/11, 2013 at 3:17 Comment(5)
@FakeRainBrigand Can't see the Paths tab in PyCharm4.Pronucleus
In PyCharm 4 CE: Go to the 'Project Interpreter'. Click the gear to the right of the interpreter's path. It will bring up a short drop-down menu, from which you should select "More..". On the right hand side of the new pop-up, there will be an icon with the mouse-over text of "Show paths for the selected interpreter". Click that button. This new 'Interpreter Paths' pop-up is where you can add paths. @AnkeshAnandBier
When I select 3.5.3, the Add/remove/up buttons in the package area are grayed out.Inexpiable
For Mac users, press the PyCharm at the top left then Preferences...->Project->Project Interpreter->Click the + sign to add a packageNalley
For me there is also another issue. If you try to add a folder that in the past had a .idea folder, but your current project has it's own .idea folder your pycharm might get confused for some reason -- even if you have the right python/conda env. For me deleting the .idea folder of the other project fixed the confusion that it could find the obviously correctly installed pkgs. Then it was it was able to find them in the pycharm editor GUI snf stopped underlyinging them in red.Busty
E
101

You should never need to modify the path directly, either through environment variables or sys.path. Whether you use the os (ex. apt-get), or pip in a virtualenv, packages will be installed to a location already on the path.

In your example, GNU Radio is installed to the system Python 2's standard site-packages location, which is already in the path. Pointing PyCharm at the correct interpreter is enough; if it isn't there is something else wrong that isn't apparent. It may be that /usr/bin/python does not point to the same interpreter that GNU Radio was installed in; try pointing specifically at the python2.7 binary. Or, PyCharm used to be somewhat bad at detecting packages; File > Invalidate Caches > Invalidate and Restart would tell it to rescan.

This answer will cover how you should set up a project environment, install packages in different scenarios, and configure PyCharm. I refer multiple times to the Python Packaging User Guide, written by the same group that maintains the official Python packaging tools.


The correct way to develop a Python application is with a virtualenv. Packages and version are installed without affecting the system or other projects. PyCharm has a built-in interface to create a virtualenv and install packages. Or you can create it from the command line and then point PyCharm at it.

$ cd MyProject
$ python2 -m virtualenv env
$ . env/bin/activate
$ pip install -U pip setuptools  # get the latest versions
$ pip install flask  # install other packages

In your PyCharm project, go to File > Settings > Project > Project Interpreter. If you used virtualenvwrapper or PyCharm to create the env, then it should show up in the menu. If not, click the gear, choose Add Local, and locate the Python binary in the env. PyCharm will display all the packages in the selected env.

choose an env

manually locate env


In some cases, such as with GNU Radio, there is no package to install with pip, the package was installed system-wide when you install the rest of GNU Radio (ex. apt-get install gnuradio). In this case, you should still use a virtualenv, but you'll need to make it aware of this system package.

$ python2 -m virtualenv --system-site-packages env

Unfortunately it looks a little messy, because all system packages will now appear in your env, but they are just links, you can still safely install or upgrade packages without affecting the system.


In some cases, you will have multiple local packages you're developing, and will want one project to use the other package. In this case you might think you have to add the local package to the other project's path, but this is not the case. You should install your package in development mode. All this requires is adding a setup.py file to your package, which will be required anyway to properly distribute and deploy the package later.

Minimal setup.py for your first project:

from setuptools import setup, find_packages

setup(
    name='mypackage',
    version='0.1',
    packages=find_packages(),
)

Then install it in your second project's env:

$ pip install -e /path/to/first/project
Erinnerinna answered 2/10, 2015 at 15:52 Comment(0)
C
56

For me, it was just a matter of marking the directory as a source root.

Challis answered 31/8, 2015 at 21:2 Comment(2)
It worked for me, just right click on the directory and then Mark Directory As: Sources root. Thank you @ChallisFeatherstone
My local modules could be found at runtime, but showed with red squiggles as if they could not be seen. "mark directory as sources root" took the squiggle off, though not sure that's the correct method.Herrle
M
45

Add path in PyCharm 2017

File -> Settings (or Ctrl+Alt+S) -> Project -> Project Interpreter

enter image description here Show all

enter image description here Select bottom icon on the right side

enter image description here Click on the plus button to add new path to your module

Michaelis answered 29/11, 2017 at 20:43 Comment(1)
Finally normal answer , just wanted to know where to paste the default pip size_packages folder .Melton
B
41

My version is PyCharm Professional edition 3.4, and the Adding a Path part is different.

You can go to "Preferences" --> "Project Interpreter". Choose the tool button at the right top corner.

Then choose "More..." --> "Show path for the selected interpreter" --> "Add". Then you can add a path.

Burnedout answered 19/11, 2014 at 2:22 Comment(2)
For me at least, when I do this and click "ok", I go back, find it still doesn't work. Then I go back to the list of paths associated with the current interpreter and find that the path I just added is gone.Gallicize
after adding the path you have to click the refresh. close the window. now the apply button is not active. switch around the settings and click apply if it is activeShad
G
6

DON'T change the interpreter path.

Change the project structure instead:

File -> Settings -> Project -> Project structure -> Add content root

Gravamen answered 8/5, 2019 at 14:30 Comment(0)
S
4

In PyCharm 2020.1 CE and Professional, you can add a path to your project's Python interpreter by doing the following:

1) Click the interpreter in the bottom right corner of the project and select 'Interpreter Settings'

Select Interpreter Settings

2) Click the settings button to the right of the interpreter name and select 'Show All':

Select Show All Interpreters

3) Make sure your project's interpreter is selected and click the fifth button in the bottom toolbar, 'show paths for the selected interpreter':

Show paths for the selected Python interpreter

4) Click the '+' button in the bottom toolbar and add a path to the folder containing your module:

enter image description here

Spirula answered 28/4, 2020 at 4:35 Comment(0)
T
3

For PyCharm Community Edition 2016.3.2 it is:

"Project Interpreter" -> Top right settings icon -> "More".

Then on the right side there should be a packages icon. When hovering over it it should say "Show paths for selected interpreter". Click it.

Then click the "Add" button or press "alt+insert" to add a new path.

Tool answered 16/3, 2017 at 7:52 Comment(0)
A
3

As quick n dirty fix, this worked for me: Adding this 2 lines before the problematic import:

import sys
sys.path.append('C:\\Python27\\Lib\site-packages')
Amadavat answered 6/7, 2018 at 8:14 Comment(0)
A
1

On Project Explorer, you can right click on the folder where the module is contained and set as 'Source'.

It will be parsed in the Index for code completion as well as other items.

Ancel answered 17/11, 2016 at 11:38 Comment(1)
My local modules could be found at runtime, but showed with red squiggles as if they could not be seen. "mark directory as sources root" took the squiggle off, though not sure that's the correct method.Herrle
B
0

For me there is also another issue. If you try to add a folder that in the past had a .idea folder, but your current project has it's own .idea folder your pycharm might get confused for some reason -- even if you have the right python/conda env. For me deleting the .idea folder of the other project fixed the confusion that it could find the obviously correctly installed pkgs. Then it was it was able to find them in the pycharm editor GUI snf stopped underlyinging them in red.

Busty answered 10/11, 2013 at 3:7 Comment(0)
C
0

I'm new to PyCharm (using 2018.3.4 CE) and Python so I rotely tried to follow each of the above suggestions to access the PIL (Pillow) package which I knew was in system-site-packages. None worked. I was about to give up for the night when I happened to notice the venv/pyvenv.cfg file under my project in the Project Explorer window. I found the line "include-system-site-packages = false" in that file and so I changed it to "true". Problem solved.

Columbus answered 27/2, 2019 at 18:52 Comment(1)
Good to note, but experienced virtual environment users usually do not want packages from other locations leaking into their isolated environment, hence the default setting to exclude system-level packages. It leads to unpredictable behavior when the same package is installed in multiple locations, especially when they are different versions.Speedwell
M
0

In my PyCharm 2019.3, select the project, then File ---> Settings, then Project: YourProjectName, in 'Project Interpreter', click the interpreter or settings, ---> Show all... ---> Select the current interpreter ---> Show paths for the selected interpreter ---> then click 'Add' to add your library, in my case, it is a wheel package

Mononuclear answered 28/9, 2020 at 20:33 Comment(0)
S
-2

Download anaconda https://anaconda.org/

once done installing anaconda...

Go into Settings -> Project Settings -> Project Interpreter.

Then navigate to the "Paths" tab and search for /anaconda/bin/python

click apply

enter image description here

Senhorita answered 21/8, 2017 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.