7

I've followed pycharm documentation to set up the IDE to resolve imports. However it seems that each folder containing *.py files needs to be explicitly added as 'sources root' in order for the IDE to resolve all references. Can this be done recursively from a root folder?

Is this the correct way to get the IDE to resolve all codebase references, or have I not set up my project structure correctly?

I have already followed other methods for resolving references in the IDE here and here but to no avail. It seems that the IDE will only resolve them if I manually add each folder as a 'sources root'. Without the recursive functionality, large codebases will be laborious when setting up the IDE!

Community
  • 1
  • 1
rnoodle
  • 534
  • 2
  • 5
  • 21

2 Answers2

3

If you have not used __init__.py, you should add it in each sub-directory to mark it as a package. By adding it, Python will treat the directories as containing packages making you modules visible to other directories and therefore able to be imported.

afxentios
  • 2,502
  • 2
  • 21
  • 24
  • 5
    From python 3.3 this is no longer true https://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html#the-init-py-trap – fafl Jul 05 '18 at 12:12
0

It's possible to select all folders in root a at once by going to Project Structure in Preferences then selecting all and clicking the blue Sources icon at the top. This was at least doable with PyCharm CE 2021.2 running on Big Sur.

jmdeamer
  • 337
  • 2
  • 16