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.

  1. is import statement valid, given directory structure, , if so, missing?
  2. if not, recommended way of getting import work? add path using sys.path.append() above import statement? or use .. notation in import statement explicitly point program pick project/ilm/app/__init__.py? fact __init__.py trying 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

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -