PyCharm import external library

155,988

Solution 1

Since PyCharm 3.4 the path tab in the 'Project Interpreter' settings has been replaced. In order to add paths to a project you need to select the cogwheel, click on 'More...' and then select the "Show path for the selected interpreter" icon. This allows you to add paths to your project as before.

My project is now behaving as I would expect.

These are the windows you would see while following the instructions

Solution 2

Answer for PyCharm 2016.1 on OSX: (This is an update to the answer by @GeorgeWilliams993's answer above, but I don't have the rep yet to make comments.)

Go to Pycharm menu --> Preferences --> Project: (projectname) --> Project Interpreter

At the top is a popup for "Project Interpreter," and to the right of it is a button with ellipses (...) - click on this button for a different popup and choose "More" (or, as it turns out, click on the main popup and choose "Show All").

This shows a list of interpreters, with one selected. At the bottom of the screen are a set of tools... pick the rightmost one:

Show path for the selected interpreter button

Now you should see all the paths pycharm is searching to find imports, and you can use the "+" button at the bottom to add a new path.

I think the most significant difference from @GeorgeWilliams993's answer is that the gear button has been replaced by a set of ellipses. That threw me off.

Solution 3

Update (2018-01-06): This answer is obsolete. Modern versions of PyCharm provide Paths via Settings ⇨ Project Interpreter ⇨ ⚙ ⇨ Show All ⇨ Show paths button.


PyCharm Professional Edition has the Paths tab in Python Interpreters settings, but Community Edition apparently doesn't have it.

As a workaround, you can create a symlink for your imported library under your project's root.

For example:

myproject
    mypackage
        __init__.py
    third_party -> /some/other/directory/third_party

Solution 4

updated on May 26-2018

If the external library is in a folder that is under the project then

File -> Settings -> Project -> Project structure -> select the folder and Mark as Sources!

If not, add content root, and do similar things.

Solution 5

In my case, the correct menu path was:

File > Default settings > Project Interpreter

Share:
155,988
GeorgeWilliams993
Author by

GeorgeWilliams993

I'm a retired computer systems engineer and mathematician living in Thailand.

Updated on July 08, 2022

Comments

  • GeorgeWilliams993
    GeorgeWilliams993 almost 2 years

    I am using PyCharm as an editor for python code in Houdini. Whenever I try to import the main Houdini library (hou) I get an error flagged in PyCharm. If I include the code snippet:-

    try:  
       import hou  
    except ImportError:  
        # Add $HFS/houdini/python2.6libs to sys.path so Python can find the  
        # hou module.  
        sys.path.append(os.environ['HFS'] + "/houdini/python%d.%dlibs" % sys.version_info[:2])  
        import hou 
    

    my code executes, without problem, from both Houdini and my selected interpreter.

    My problem is with PyCharm itself. The editor flags 'import hou' as an error and any subsequent files that import this file flag modules imported by this file as errors as well. Hence I loose type ahead functionality and get an over abundance of error messages that make it hard to spot the real problems.

    How do I get PyCharm to recognize the path to the hou module.

    I have tried, for a couple of days, to Google a solution to this problem but they all seem to refer to tabs and settings that are not in my version of PyCharm (Community Edition 3.4.1). E.G. my 'Project Interpreter' setting only has a list of Packages and has no 'path' tab as stated in many 'fixes' to closely related problems.

  • GeorgeWilliams993
    GeorgeWilliams993 about 10 years
    Wow! I have only just started using PyCharm, I spent the last 20 years of my working life using MS Visual Studio developing C++ projects, and the idea of an IDE that does not allow you to add an external resource to a project with more than a single click is crazy. I have no idea how to start implementing your solution in Windows.
  • sthzg
    sthzg about 9 years
    Loving the Jetbrains products, but boy are they good at hiding certain things. (-;
  • senz
    senz over 7 years
    @GeorgeWilliams993 mklink /J "C:\Link To Folder" "C:\Users\Name\Original Folder"
  • Josephine Moeller
    Josephine Moeller almost 7 years
    This seems to be the right way to go for the current Linux version too.
  • jmh
    jmh over 6 years
    The path settings do exist in CE as well, this answer worked for me: stackoverflow.com/a/24206781
  • swizzlevixen
    swizzlevixen almost 6 years
    I just wrote up how to do this on macOS in PyCharm 2018.2. Essentially what you say above, but I included more screenshots, in case that's helpful for anyone: github.com/bobtiki/pycharm-blender-mac/blob/master/README.md
  • Nagabhushan S N
    Nagabhushan S N over 5 years
    As of PyCharm 2018.3.4 CE (2019-Feb-01), on ubuntu 18.04, they've again changed the location. Refer to Hghowe's answer below
  • pyrrhic
    pyrrhic over 4 years
    As of the year 2020, (specifically, February 2), this is the only answer that worked for me! Move this answer to the top!
  • Jean-François Savard
    Jean-François Savard about 4 years
    God, that's crazy UX. Thank you.
  • ruohola
    ruohola over 3 years
    If not, add content root This was a game changer, thank you!
  • petro4213
    petro4213 over 2 years
    Thanks a lot. I tried to add the library under my project with File -> Settings -> Project -> Project Interpeter -> "wheel" (Show All...) -> Interpreter Path, but this only helps for debugging/executing (if I'm not wrong), but definitely not for code insight. For that you need to mark the directory as source in your project structure as you say.
  • Ray Walker
    Ray Walker about 2 years
    Can you do the same for a remote python interpreter!? When having your code locally but using a remote interpreter on a more powerful machine?