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_project
Add files
.gitignore
,LICENSE
,README.md
tomy_new_project
Create a virtual environment under
my_new_project/.venv
Activate the virtual environment,
pip install
any required packages, and add them to arequirements.txt
fileAdd a
pyproject.toml
to 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_name
containing__init__.py
To allow nonlocal imports, follow [https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder/50194143#50194143](these directions):
- Add a minimal
setup.py
to the toplevelmy_new_project
directory. - With the virtualenv activated, run
pip install -e .
from themy_new_project
dir.
- 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 module
to__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_name
need their own__init__.py
files containing imports for that subdirectory. Modules in that subdir can import modules from other subdirs asfrom package_name.other_subdir import other_module
.