python project setup: Nonlinear Function
Created: April 09, 2022
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.

  1. Create a new git repo and clone into a directory my_new_project

  2. Add files .gitignore, LICENSE, README.md to my_new_project

  3. Create a virtual environment under my_new_project/.venv

  4. Activate the virtual environment, pip install any required packages, and add them to a requirements.txt file

  5. Add 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"]
  6. Create a subdir package_name containing __init__.py

  7. To allow nonlocal imports, follow [https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder/50194143#50194143](these directions):

    1. Add a minimal setup.py to the toplevel my_new_project directory.
    2. With the virtualenv activated, run pip install -e . from the my_new_project dir.
  8. Now you can write code adding files under package_name. For each new file module.py:

    1. Add a corresponding import from package_name import module to __init__.py.
    2. Add any unit tests in module_test.py. These do not need to be imported in __init__.py.
  9. 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 as from package_name.other_subdir import other_module.