How This Package Was Built Using Cookiecutter¶
The cookiecutter docs are not super easy to follow. Here’s all the steps I did to put this package together.
I’m running on my Mac M4 running Sequoia 15.6.
- Set up a development environment using conda
https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
conda create --name myEnvconda activate myEnv
- Installing and setting up cookiecutter
conda install pippip install -U cookiecuttercookiecutter https://github.com/audreyfeldroy/cookiecutter-pypackage.gitFill out the question prompts
This will create a local directory containing their boilerplate code
- Writing the package
- Code:
mypackage/src/mypackage/mypackage.pyis where all your main functions goYou can put other modules in
mypackage/src/mypackageand call them from mypackage.py (ie, a utils.py file)
- Tests:
Yes, you do have to write tests for your code
mypackage/tests/test_mypackage.pyis the main test fileyou can write separate test_{name}.py test files in this same directory for other submodules
pip install pytestthen from the top level directory you can run the tests as
python -m pytest
- Installing the package:
Install locally:
pip install .from inside the directory- Install locally and editable:
pip install -e . You don’t have to re-install every time you change your code
- Install locally and editable:
- Using your shiny new package:
From a different directory
from mypackage import mypackage, utils utils.do_something_useful() x = mypackage.foo()
- Writing and building documentation:
pip install sphinxcd into
mypackage/docs/sphinx-quickstartsets everything upfollow prompts
I set it up with separate source and build directories - if you don’t you’ll have to change the sphinx-build command
cd back up to
mypackage/- run
sphinx-apidoc -o docs/ src/mypackage This will auto-generate the API docs
- run
cd back into
mypackage/docs/run
sphinx-build source buildafter this point can just run
make htmlevery time you want to update the pagesyou can now open the .html files from build/ in your browser.
- write some documentation
- you can link other files’ content into .rst pages using
.. include:: ../../README.rst this gave me a ton of grief…just keep messing with it until it does what you want.
- you can link other files’ content into .rst pages using
- Docstrings and formatting
In conf.py you can add:
extensions = ['sphinx.ext.napoleon'] napoleon_google_docstring = False napoleon_use_param = False napoleon_use_ivar = True
Which just does a nicer job of handling the docstring formatting in more standard Google/NumPy formatting
- Hooking into readthedocs
- Get the repo onto GitHub
these directions were useful for me: https://docs.github.com/en/migrations/importing-source-code/using-the-command-line-to-import-source-code/adding-locally-hosted-code-to-github#adding-a-local-repository-to-github-using-git
Do NOT put your public access token into your work or you will be bonked when you commit and push
- Follow the readthedocs instructions: https://docs.readthedocs.com/platform/stable/tutorial/index.html
Main thing I ran into: the new .readthedocs.yaml file needs the path to the conf.py file, which for me was in docs/source/conf.py and not the default docs/conf.py
voila!
- Other bits and pieces:
dependencies (like numpy or matplotlib or what have you) go into pyproject.toml file