Search code examples
pythonpython-3.xpython-module

How to import submodules from relative path?


I have multiple modules in my project and specify some execution point. But when I try to import files from submodules, it doesn't work.

So, how to specify submodules to execute from selected execution file?

project
--bin
---- executeFile
--modules
---- __init__.py
----fileA.py

in executeFile, I try:

from ..modules.fileA import * 

but get the error:

Traceback (most recent call last):
File "./bin/muexecute", line 10, in <module>
  from ..modules.os import *
SystemError: Parent module '' not loaded, cannot perform relative import

Solution

  • I found solution. The problem was in my opinion about using init.py. I placed in executable scripts path to including and it works fine

    PACKAGE_PARENT = '..'
    SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
    sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))