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:

  1. your modules located in directories in python path (sys.path).
  2. 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

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -