Modified: April 09, 2022
python project setup
This page is from my personal notes, and has not been specifically reviewed for public consumption. It might be incomplete, wrong, outdated, or stupid. Caveat lector.General procedure for setting up a new Python project.
Create a new git repo and clone into a directory
my_new_projectAdd files
.gitignore,LICENSE,README.mdtomy_new_projectCreate a virtual environment under
my_new_project/.venvActivate the virtual environment,
pip installany required packages, and add them to arequirements.txtfileAdd a
pyproject.tomlto configure code formatting, etc. For example:[tool.isort] profile = "google" known_jax = ["numpy","jax"] known_jax_ecosystem = ["brax","flax","optax","tensorflow_probability"] sections=["FUTURE","STDLIB","THIRDPARTY","JAX","JAX_ECOSYSTEM","FIRSTPARTY","LOCALFOLDER"] [tool.yapf] style = "google" [tool.pyright] ignore = ["notebooks", "**ipynb"]Create a subdir
package_namecontaining__init__.pyTo allow nonlocal imports, follow [https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder/50194143#50194143](these directions):
- Add a minimal
setup.pyto the toplevelmy_new_projectdirectory. - With the virtualenv activated, run
pip install -e .from themy_new_projectdir.
- Add a minimal
Now you can write code adding files under
package_name. For each new filemodule.py:- Add a corresponding import
from package_name import moduleto__init__.py. - Add any unit tests in
module_test.py. These do not need to be imported in__init__.py.
- Add a corresponding import
Any subdirectories under
package_nameneed their own__init__.pyfiles containing imports for that subdirectory. Modules in that subdir can import modules from other subdirs asfrom package_name.other_subdir import other_module.