python - Importing a code-containing __init__.py from a cousin folder? -
given directory structure:
program/ setup.py ilm/ __init__.py app/ __init__.py bin/ script.py note: setup.py not typical setup.py, rather custom-made setup uniquely py2app.
program/ilm/app/__init__.py non-empty: contains main() function, instantiates class in same file. question: in program/ilm/bin/script.py, if want import , execute main() function in program/ilm/app/__init__.py, valid ways of achieving this? reason ask script.py doing thus:
import ilm.app app if __name__ == '__main__': app.main() based on (admittedly limited) understanding of packaging , importing, shouldn't work, since have not explicitly told script.py project/ilm/app/__init__.py using ... , indeed, get:
macbook-pro-de-pyderman:program pyderman$ python ./bin/script.py traceback (most recent call last): file "./bin/script.py", line 5, in <module> import ilm.app app importerror: no module named ilm.app in contrast, when python interpreter started in /project, import ilm.app app works fine.
this apparently fully-functional production code should not have change running.
- is
importstatement valid, given directory structure, , if so, missing? - if not, recommended way of getting import work? add path using
sys.path.append()aboveimportstatement? or use..notation inimportstatement explicitly point program pickproject/ilm/app/__init__.py? fact__init__.pytrying import significant?
two things. need make sure iml directory in python path. either make sure running python right directory or add right path sys.path list. , need make sure both iml , app directory both have
__init__.py
file, since python needs interpret whole thing hierarchy of modules rather dirs. should able
iml import app
Comments
Post a Comment