How do I tell PyCharm about the location of custom modules?

I have an application at work that is configured with the following structure.

/project_root /applications /app1 __init__.py /app2 __init__.py ... /appN /pkg /database __init__.py /toolbox __init__.py ... __init__.py __init__.py main_framework.py 

I am learning to use PyCharm (community edition) instead of the Eclipse PyDev tool. In __init__.py in app1 , I have an import statement that looks like this:

 import pkg.database 

PyCharm complains about importing with the message no module named pkg .

PyCharm Error (click to enlarge)

Due to how the structure is built, the system works very well. Each of appN knows about the pkg directory. How do I tell PyCharm that I have custom modules in the pkg directory to stop these warnings?

I am testing PyCharm 4.0.3 Community Edition.

A few things I tried, thanks for the comments below that didn't help:

  • In the Project Structure dialog box, project_root was listed as the only directory in the Content Root section on the right side of the dialog box. I added the pkg directory so that it is also listed. It had no effect.
  • In the original version of the question, I skipped __init__.py in pkg when displaying the structure. This file exists.
+5
source share
1 answer

The solution to this was a two-step process:

  • Add the pkg directory as the source root. Do this by choosing FileSettingsProject → (select project) → Project Structure , then select the pkg directory and add by clicking the Sources button. Click OK.
  • Then select File Invalidate Caches / Restart Invalidate and Restart

Wait for PyCharm to restart and rebuild its caching / index scans. Now the pkg directory has been detected as the original root, and my errors have disappeared.

+13
source

Source: https://habr.com/ru/post/1210276/


All Articles