Testing package layouts and import structure due to issues with circular imports in other packages.
- Package underneath src directory
- This avoids issues with accidentally running local code instaed of installed package when using python -m
- Primary package code in
__main__.py- No requirement for if
__name__==__main__as__name__will always be__main__when this module is run
- No requirement for if
__main__:maincallsoneimported fromone.pyandthreeimported fromsub_package.three.pyonecallstwoimported fromtwo.py- Both
oneandtwocallmodule_and_function_name_matchimported frommodule_and_function_name_match.py - Both
one,twoandthreecalltestimported frommodule_and_function_name_match.py
- Modules import functions and modules using absolute explicit imports, using the name of the package then name of the module, then name of the function if required
- e.g.: from package_name.module_name import function_name
- Then function is called with
function_name()
- Then function is called with
- or: from package_name import module_name
- Then function is called with
module_name.function_name()
- Then function is called with
- e.g.: from package_name.module_name import function_name
- Setup python venv:
python -m venv .env - Activate venv:
source .env/bin/activate - Install as editable package:
pip install -e . - Run on cli with
sample_package
This works due to entry_points option within setup.py. sample_package has been added as an executable to the path within this environment.
- Setup python venv:
python -m venv .env - Activate venv:
source .env/bin/activate - Install as editable package:
pip install -e . - Run as package:
python -m sample_package
- Setup python venv:
python -m venv .env - Activate venv:
source .env/bin/activate - Install as editable package:
pip install -e . - Import into other python projects with
import sample_package
- Navigate to
src/ - Run as package:
python -m sample_package