PyCharm 和 PYTHONPATH

我有一个用于 PYTHONPATH: c:\test\my\scripts\的目录。在这个目录中,我导入了一些模块。它在我的 Python shell 中运行良好。

如何将此目录路径添加到 PyCharm,以便导入该目录中的内容?

114752 次浏览

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

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

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.

enter image description here

you can go to setting in pycharm and set interpreter python

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!