119

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 well in my Python shell.

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

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
Trying_hard
  • 8,931
  • 29
  • 62
  • 85
  • 1
    "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 https://stackoverflow.com/questions/48947494/add-directory-to-python-path-in-pycharm – user1596683 Jul 09 '18 at 17:24

4 Answers4

183

Out of date. See Duane's answer below for pycharm version newer than 2019.2.

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

yang piao
  • 144
  • 1
  • 7
Hendrik F
  • 3,690
  • 3
  • 21
  • 24
  • There is no longer a **More** entry. Now there is **Add Local...**. Not sure of the next step. (Pycharm 2017.3.3 on MacOS) – Ray Salemi Mar 06 '18 at 22:12
  • 7
    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. – Oriol Mirosa Mar 11 '18 at 22:25
  • Any suggestions for folks using Intellij with Python plugin? – Pratik Khadloya May 28 '20 at 13:13
  • 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... – Oleksandr Novik Nov 27 '21 at 14:13
99

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
Duane
  • 4,572
  • 6
  • 32
  • 33
55

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.

alkalinity
  • 1,750
  • 2
  • 21
  • 32
  • 1
    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"? – YvesgereY Feb 08 '21 at 13:56
  • 1
    @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 – alkalinity Feb 08 '21 at 21:35
  • Excellent. This helped me in no time. (I have a large project where Pythonic project is in `src/build` and have to be added to PYTHONPATH or Sources Root to view in Pycharm). – kinORnirvana Oct 23 '22 at 14:47
17

The following answer is for PyCharm 2022.1.3+ (Professional Edition).

  1. Open up Preferences (mac: + ,) enter image description here

  2. Click on Project and then Project Interpreter enter image description here

  3. Click on the three vertical dots and the on Show All...

enter image description here enter image description here

  1. Choose your interpreter and then click on the directory structure icon enter image description here

  2. Click on the + icon and add the path you need to add to PYTHONPATH. enter image description here

That's it!

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
  • 1
    Thank you! I have no idea why JetBrains would make it this hard for something that is so routinely needed. Also, as of version 2022.2.3, "Preferences" is now called, "Settings." – etotheipi Nov 01 '22 at 17:17