What is __init__.py used for?
Files named __init__.py are used to mark directories on disk as Python package directories. For example, suppose you have these files:
dir/spam/__init__.py
dir/spam/module.py
and dir is on your python path, then you can import the module.py as
import spam.module
or
from spam import module
If you remove the __init__.py file, Python will no longer look for submodules inside that directory, so attempts to import the module will fail.
The __init__.py file is usually empty, but can be used to export selected portions of the package under more convenient name, hold convenience functions, etc. Given the example above, the contents of the init module can be accessed as
import spam
Reference http://effbot.org/pyfaq/what-is-init-py-used-for.htm
dir/spam/__init__.py
dir/spam/module.py
and dir is on your python path, then you can import the module.py as
import spam.module
or
from spam import module
If you remove the __init__.py file, Python will no longer look for submodules inside that directory, so attempts to import the module will fail.
The __init__.py file is usually empty, but can be used to export selected portions of the package under more convenient name, hold convenience functions, etc. Given the example above, the contents of the init module can be accessed as
import spam
Reference http://effbot.org/pyfaq/what-is-init-py-used-for.htm
评论
发表评论