4

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 ?

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
  • in terminal you need to see from which directory location you are executing the code, is that code depends on other packages, in pycharm it is assumed the directory is the base of the project and all code is executed from that directory level not from the package/module level like that u r tring to do in terminal, if you execute it from directory level it may work – sahasrara62 Mar 19 '20 at 21:45
  • @sahasrara62 I am running from within `Pycharm` not the terminal – WestCoastProjects Mar 19 '20 at 21:55
  • `from . logger import *` here is space typo error ? if yes then do `from .logger import *`, also this may [help](https://napuzba.com/a/import-error-relative-no-parent) – sahasrara62 Mar 19 '20 at 22:00
  • @sahasrara62 I tried with and without the `space` before the `.`: no difference in behavior. Also i had already seen that other reference: it is not as authoritative or complete as the SOF question I linked above. Also none of them talks about the specfics of running inside `pycharm` – WestCoastProjects Mar 19 '20 at 22:03
  • i just tried created this sample project code, if you just do `from logger import *` and run the way you are executing this script, it will work fine (don't know why `.` is not taking work here ideally) – sahasrara62 Mar 19 '20 at 22:35
  • `from logger import *` does not work because `logger` is not the base level package: see my updated question. – WestCoastProjects Mar 19 '20 at 22:45
  • if in your project relative import done using `.` then remove those – sahasrara62 Mar 19 '20 at 22:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209967/discussion-between-javadba-and-sahasrara62). – WestCoastProjects Mar 20 '20 at 02:41

1 Answers1

4

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
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560