PyCharm and PYTHONPATH

99,466

Solution 1

Out of data, see Duane's answer below.

You need to go to the Main PyCharm Preferences, which will open up a separate window. In the left pane, choose Project:... > Project Interpreter. Now, in the main pane on the right, click the settings symbol (gear symbol) next to the field for "Project Interpreter". Choose More or Show All in the menu that pops up. Now in the final step, pick the interpreter you are using for this project and click on the tree symbol at the bottom of the window (hovering over the symbol reveals it as "Show paths for the selected interpreter"). Add your path by click in the "plus" symbol.

It took me ages to find, so I hope the detailed instructions will help. Further details are available in the PyCharm docs.

It is good practice to have __init__.py in each subfolder of the module you are looking to add, as well as making your project folder a 'Source Root'. Simply right-click on the folder in the path bar and choose 'Mark Directory as ...'

Solution 2

For Pycharm Community 2019.3

  • File
  • Settings
  • Project
  • Project Interpreter
  • Cog in top right => Show all
  • Select the interpreter
  • On the right toolbar, there are 5 icons, bottom one looks like a folder tree, folder treeclick it
  • Press plus +
  • Select directory, press OK

Solution 3

In PyCharm Community 2019.2/2019.3 (and probably other versions), you can simply:

  • right-click any folder in your project
  • select "Mark Directory As"
  • select "Sources Root"

Modules within that folder will now be available for import. Any number of folders can be so marked.

Share:
99,466

Related videos on Youtube

Trying_hard
Author by

Trying_hard

Updated on March 03, 2020

Comments

  • Trying_hard
    Trying_hard over 4 years

    I am new to PyCharm. I have a directory that I use for my PYTHONPATH: c:\test\my\scripts\. In this directory I have some modules I import. It works fine in my Python shell.

    How do I add this directory path to PyCharm so I can import what is in that directory?

    • user1596683
      user1596683 almost 6 years
      "You need to go to the Main PyCharm Preferences, which will ...." There is no longer such things as "Main PyCharm" or "Preferences" in 2018.1.4 Community Edition, see stackoverflow.com/questions/48947494/…
  • jds
    jds almost 8 years
    Not sure why, but this is the first solution that has worked for me.
  • alisa
    alisa over 7 years
    I also would rather have chosen this one. It works. Thanks!
  • ScoPi
    ScoPi almost 7 years
    Correct answer.
  • Brent Faust
    Brent Faust over 6 years
    @alisa You can change the most-helpful answer by just marking another one as the answer.
  • Ray Salemi
    Ray Salemi over 6 years
    There is no longer a More entry. Now there is Add Local.... Not sure of the next step. (Pycharm 2017.3.3 on MacOS)
  • Oriol Mirosa
    Oriol Mirosa over 6 years
    Instead of the More entry, which doesn't exist anymore, you should now click on Show All... once you are in the project interpreter page and click on the settings icon.
  • Danijel
    Danijel over 5 years
    PyCharm 2018/03: there is no "folder tree" icon.
  • Duane
    Duane over 5 years
    Added the icon for clarity. From 2018.3.5
  • jMike
    jMike over 4 years
    In current version (2019.3.1) the cogwheel is in position "top right".
  • Pratik Khadloya
    Pratik Khadloya about 4 years
    Any suggestions for folks using Intellij with Python plugin?
  • zwcloud
    zwcloud almost 4 years
    Where is the config file that storing such information?
  • YvesgereY
    YvesgereY over 3 years
    What about a local package which isn't part of the project? What would be the implications of including this foreign directory in the project and marking it as "Sources Root"?
  • alkalinity
    alkalinity over 3 years
    @YvesgereY Assuming you can see the directory in the first place (can you?) I don't see a problem with that. So far as I know, 'Sources Root' doesn't have any special meaning but to use that directory for resolving imports
  • Oleksandr Novik
    Oleksandr Novik over 2 years
    Works like charm! Though it's weird that CLion uses this way of assigning paths to look for modules in. PYTHONPATH seems to be more trivial and known to everybody...
  • grreeenn
    grreeenn about 2 years
    no such icon anymore (2021.3.3 pro)... how was this dialog called?