prj
-pkg_1
--file1.py
-pkg_2
--file2.py
Внутри file2.py
у нас есть что-то вроде:
# Ye be in dis 'ere file which be name-ed `file2.py`
if __name__ == '__main__':
## MAGICAL IMPORT STATEMENT HERE ###
# import Klass from pkg_1/file1.py
# definitely do not write `from . [blah, blah, blah]`
# NO DOTS ALLOWED AFTER `from` inside main script!
else: # We not be da main script
from ..pkg_1.file1 import Klass
# go to parent directory of current file
# (parent dir of `file2.py` is `pkg2`)
# go to parent_directory_of(`pkg2`) == `proj`
# look for `pkg_1` under `proj`
# look for `file1` under `pkg_1`
# look for `Klass` inside `file1`
Чем мы можем заменить MAGICAL IMPORT STATEMENT
, чтобы при запуске file2.py
в качестве основного сценария мы не получали "ValueError: attempted relative import beyond top-level package"