How to Import A Upper Level Package in Python 3 -
this question has answer here:
i have file structure this:
/mypkg /__init__.py /apkg /__init__.py how can import /mypkg/__init__.py /mypkg/apkg/__init__.py without using sys.path , within package. mean:
# not want since path outside package. import sys sys.path.append('../../') import mypkg thanks again.
the short answer can't. can import 2 locations:
- your modules located in directories in python path (
sys.path). - your current working directory.
to import parent directory, need add directory path.
furthermore, you're creating circular dependency of sorts. suggest reconsider overall structure because having module import "parent" doesn't make sense. fact need other files higher-level directory suggests maybe files in "apkg" should @ same level "mypkg".
in reply comment: don't import __init__.py files. act directory module can utilize objects , definitions contained in module. can perform initialization on module contents when imported, if necessary. there shouldn't code in __init__.py file want import in script.
Comments
Post a Comment