vendredi 8 juillet 2016

Import successfully without __init__.py?


I am just curious about what is exactly the use of __init__.py? Yes, the answer is this file makes the directory an importable package. However, Consider the following example:

project/

foo/
   __init__.py
   a.py
   bar/
       b.py

if I want to import a into b, I have to add following statement:

sys.path.append('/path_to_foo')
import foo.a

This will run successfully with or without __init__.py. However, if there is not sys.path.append statement, an no module error will occur, also, with or without __init__.py. Then it seems that only the system path matters and __init__.py does not have any effect. Can anyone explain this? Thanks you.


Aucun commentaire:

Enregistrer un commentaire