WestCoastProjects
WestCoastProjects

Reputation: 63259

Relative imports in Pycharm

Some authoritative names have commented and/or answered about the general problem of relative imports when trying to run a script within the module directory here: Relative imports in Python 3

My issue is that the Pycharm IDE is parsing the module and running it in this same directory. So I end up with import errors. Note that there is an __init__.py in the package. Looks fine to pycharm parser

enter image description here

from . logger import *   # Logger is a module in same package

But ..

enter image description here

Traceback (most recent call last):
  File "/git/bluej/fusion/python/pointr/bluej/util/DB.py", line 6, in <module>
    from . logger import *
ImportError: attempted relative import with no known parent package

the Run Configuration was created by right-clicking in the module. The auto generated version is like this: having the working directory the same as the script dir. /git/bluej/fusion/python/pointr/bluej/util (note the package is pointr.blue.util):

enter image description here

I tried out putting the Working Directory at the base python directory level: /git/bluej/fusion/python/

enter image description here

However that did not have any effect.

Responding to a comment: logger is not the root package and so the following code does not work:

enter image description here

How can I develop, debug, and run modules with relative imports in Pycharm ?

Upvotes: 6

Views: 2268

Answers (1)

WestCoastProjects
WestCoastProjects

Reputation: 63259

enter image description here

  • Change script name to module name : see the first selection under Configuration
  • Change working directory to root of the hierarchy
  • hope for the best

Upvotes: 7

Related Questions