How I Start a New Python Project



Recently, I've found myself following the same set of steps every time I start a new Python project. I want certain style checking things in place, I want a tests directory, I want a readme and a license, I want a github repository spun up, I want pre-commit set up; there's a lot of small stuff, regardless of the project. It doesn't take that long, but it'd be nice to make the process quick, easy, update-able, and consistent.


With the above context, I want the following for every project I spin up:


I found cookiecutter (opens in a new tab), a tool that lets you create templates of projects. There are lots of templates out there, one of which is probably already what I want, but hey this isn't my job so I'm happy to roll my own and learn a bit. I made my own template to my taste (given the goals I stated above). You can check out the template and the details of the files along with options here (opens in a new tab). My template creates a project structure that looks like this (with a number of optional files shown here):

├─ .github
├─── workflows
├───── on-pull-request.yaml
├───── on-merge-to-main.yaml
├─ pyproject.toml
├─ src
├─ tests
├─ tox.ini
├─ .pre-commit-config.yaml
├─ .gitignore
├─ Dockerfile

But I need more than just the project directory structure to look the way I want! I want things installed, I want git set up! So what's next?

Well, cookiecutter has pre and post generation scripts! You can write these in Python or in your shell script of choice. Here (opens in a new tab) is my post-generation script. It does the following:

So cookiecutter addresses pretty much everything I set out to do! Here is the Python cookiecutter repo I made (opens in a new tab), with instructions for use in the readme as well!

Future improvements

Now what would make this better?

I prefer staying in Python land for any personal projects, so I won't entertain "other languages" as a future improvement. I'm happy with my quick and scrappy custom project creation script. I might make it better at some point, but this works for now!



© Raaid Arshad.RSS