How do I use installed packages in PyCharm?

607,472

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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

Solution 5

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.

Share:
607,472

Related videos on Youtube

smurff
Author by

smurff

Updated on July 08, 2022

Comments

  • smurff
    smurff almost 2 years

    In PyCharm, I've added the Python environment /usr/bin/python. However,

    from gnuradio import gr
    

    fails as an undefined reference. However, it works fine in the Python interpreter from the command line.

    GNURadio works fine with python outside of Pycharm. Everything is installed and configured how I want it.

    Gnuradio is located at /usr/local/lib/python2.7/site-packages/gnuradio

    Also:

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

    • LotusUNSW
      LotusUNSW over 10 years
      Ok, so how did you install gnuradio?
    • avp
      avp over 5 years
      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.
  • Ankesh Anand
    Ankesh Anand about 9 years
    @FakeRainBrigand Can't see the Paths tab in PyCharm4.
  • TheGrimmScientist
    TheGrimmScientist about 9 years
    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. @AnkeshAnand
  • lmiguelvargasf
    lmiguelvargasf over 8 years
    It worked for me, just right click on the directory and then Mark Directory As: Sources root. Thank you @michaelsnowden
  • Peter
    Peter over 8 years
    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.
  • Yonatan Simson
    Yonatan Simson about 8 years
    Nice solution. I found out that my interpreter was set to virtual environment that did not include numpy. I simply set it back to the standard interpretor and this solved my problem.
  • JuKe
    JuKe about 8 years
    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 active
  • Rob Osborne
    Rob Osborne over 7 years
    Worked for me; the bizarre part is that this is like my 15th PyCharm project and the first time I've had to do this.
  • ArmenB
    ArmenB over 7 years
    When I select 3.5.3, the Add/remove/up buttons in the package area are grayed out.
  • Kim Miller
    Kim Miller almost 7 years
    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.
  • Kim Miller
    Kim Miller almost 7 years
    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.
  • ofekp
    ofekp over 6 years
    For Mac users, press the PyCharm at the top left then Preferences...->Project->Project Interpreter->Click the + sign to add a package
  • Stav Bodik
    Stav Bodik over 6 years
    Finally normal answer , just wanted to know where to paste the default pip size_packages folder .
  • JRsz
    JRsz about 6 years
    I have done the same to add the path to a .jar file so I ca use some java libraries. However, I do not know how to specify the jar file and that it is supposed to be used by my script. Could you help me further?
  • Kanishk Tanwar
    Kanishk Tanwar about 6 years
    do you know what is the default source root for pycharm, if it is not specified?
  • Alexandr S.
    Alexandr S. almost 5 years
    Thanks, works great if imports in the project look broken.
  • András Aszódi
    András Aszódi over 3 years
    @KimMiller I had exactly the same situation 3 years after you. PyCharm can find local packages/modules only if one marks them as "sources root".
  • merv
    merv over 2 years
    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.