Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I inform PyCharm of the location of custom modules?

Tags:

python

pycharm

I have an application at work that is set up 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'm investigating using PyCharm (community edition), instead of Eclipse's PyDev tool. In __init__.py in app1 I have an import statement that looks like this:

import pkg.database

PyCharm complains about the import with a no module named pkg message.

PyCharm Error (click for larger image)

Due to how the framework is built, though, the system runs just fine. Each of the appNs know about the pkg directory. How do I inform PyCharm that I have custom built modules in the pkg directory so that these warnings stop?

I am testing PyCharm 4.0.3 Community Edition.

A couple things I've tried, thanks for comments below, that haven't helped:

  • In the Project Structure dialog, project_root was listed as the only directory under "Content Root" on the right hand side of the dialog. I've added the pkg directory so that it is listed as well. This had no effect.
  • In the initial version of the question, I missed the __init__.py in pkg when displaying the structure. This file does exist.
like image 440
Andy Avatar asked Jan 02 '15 17:01

Andy


1 Answers

The solution to this was a two step process:

  1. Add the pkg directory as a source root. Do this by selecting File -> Settings -> Project -> (select the project) -> Project Structure then select the pkg directory and add by clicking the Sources button. Click Ok.
  2. Then select File -> Invalidate Caches / Restart -> Invalidate and Restart

Wait for PyCharm to restart and rebuild it's cache / scan the indexes. Now the pkg directory is detected as a source root and my errors are gone.

like image 143
Andy Avatar answered Sep 21 '22 22:09

Andy