Python

Topics

Python boilerplate

Simple '__main__' usage

if __name__=='__main__':
	print("hello world")

With main function, runnable as program:

import sys
 
def main() -> int:
	a=2+2
	print(f"2+2={a}")
 
if __name__=='__main__':
	sys.exit(main())

Creating Python packages

ModuleNotFoundError: No module named XXXX

Context:

from myfolder.myClass import myClass

Error: ModuleNotFoundError: No module named 'myFolder'

Assuming you are using something like pyproject.toml, you can fix this by doing, from the home directory:

pip install -e .
python -m scripts.my_script.py

This ensures that Python set the home in the home of the project, and so