Python
Topics
- Python pip β python package manager
- Python venv β python virtual environments
- pyproject.toml β instead of
requirements.txtorsetup.py - MyPy (TODO) β a type checker for python
- Python ruff β https://docs.astral.sh/ruff/ a fast python linter and code formatter
- Python Conda
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 myClassError:
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
Python Launch Manager
I installed this manager from https://www.python.org/downloads/release/pymanager-262/ due to a compatibility issue between (AWS) ParallelClusterβs internal asyncio usage and the newer Python versions.
To launch a different version of python you can run:
py -3.11and you can also create a virtual environment i.e.
py -3.12 -m venv my-venv