There is a huge difference to load module when python file run as script (e.g. python main.py
) or module (e.g. python -m main
). It also impacts how __init__.py
file is loaded and its objects are imported.
- Loaded as script, e.g,
python testimport/main.py
, orpython main.py
. Alternatively, go to each level of directory and then run./run_as_sript.sh
from all levels of directories. Python visibility limits WITHIN but exclude the script's parent DIRECTORY (testimport
). Absolute import is based on the (within) directory visibility, unable to perform relative import.__package__: None
- Succeeded,
import foo
# visibility within the directory only - Failed,
import testimport.foo
# visibility exclude the directory,testimport
is unrecognized - Failed,
from testimport import foo
# visibility exclude the directory,testimport
is unrecognized - Failed,
from . import foo
# unable to perform relative import
- Succeeded,
- Loaded as top level "module", e.g,
python -m main
. Alternatively,cd TestImport/testimport; ./run_as_module.sh
. Python visibility limits WITHIN but exclude current DIRECTORY (testimport
). Absolute import is based on the (within) directory visibility, unable to perform relative import.__package__:
- Succeeded,
import foo
# visibility within the directory only - Failed,
import testimport.foo
# visibility is WITHIN but excludetestimport
, sotestimport
is unrecognized - Failed,
from testimport import foo
# visibility is WITHIN but excludetestimport
, sotestimport
is unrecognized - Failed,
from . import foo
# unable to perform relative import
- Succeeded,
- Loaded as part of a package, e.g,
python -m testimport.main
. Alternatively,cd TestImport; ./run_as_module.sh
. Python visibility available to current PACKAGE (testimport
). Absolute import by specifying package full path, e.g,testimport.***
. Or use.
for relative import.__package__: testimport
- Failed,
import foo
# visibility is packagetestimport
, need to specify the full path, starting fromtestimport
- Succeeded,
import testimport.foo
# visibility is packagetestimport
, and it does start fromtestimport
- Succeeded,
from testimport import foo
# visibility is packagetestimport
, and it does start fromtestimport
- Succeeded,
from . import foo
# visibility is packagetestimport
, use relative import - Failed,
from .. import upper_most_module
# visibility is packagetestimport
and it's already the top level, the..
goes beyond that
- Failed,
- Loaded as part of a package, e.g,
python -m Test.testimport.main
. Alternatively,./run_as_module.sh
. Python visibility available to current PACKAGE (TestImport.testimport
). Absolute import by specifying package full path, e.g,TestImport.***
. Or use.
for relative import.__package__: TestImport.testimport
- Failed,
import foo
# visibility is packageTestImport.testimport
, need to specify the full path, starting fromTestImport
- Failed,
import testimport.foo
# visibility is packageTestImport.testimport
, need to specify the full path, starting fromTestImport
- Failed,
from testimport import foo
# visibility is packageTestImport.testimport
, need to specify the full path, starting fromTestImport
- Succeeded,
import TestImport.testimport.foo
# visibility is packageTestImport.testimport
, and it does start fromTestImport
- Succeeded,
from . import foo
# visibility is packageTestImport.testimport
, use relative import - Succeeded,
from .. import upper_most_module
# visibility is packageTestImport.testimport
, so..
is actuallyTestImport
, it's still in scope
- Failed,
Rule of thumb: The value of __package__
determines the package or directory visibility. Run the .sh
files each level of directory to understand more details.
DO NOTE, python on Windows using Cygwin doesn't seem to work in that way, but who cares!?