I am a programmer with experience in multiple of programming languages,
but not yet in python. I want to start a bigger project, but while
googling I find a lot of conflicting advice on how to structure it.
Can someone provide me a few links to what’s the standard today, if
there even is 1 standard?
There isn’t just one standard. And you can always change your standard.
Basically, I need an equivalent to the conventions given by Java’s maven, go’s go, rust’s cargo, …
I have these needs:
- Dependency management, isolated to the project (so multiple projects can have conflicting versions of the same dependency)… Ecosystem seems to be pip,pipenv, pipx,venv, but it is unclear to me how these work together and how much they overlap.
There are several of these. The easiest is venv, ships with Python, is
simple and effective. The others: do more, I’ve not needed them
personally, you can always upgrade once you’re comfoartable.
- folder structure: Where do I place source code, tests, documentation, config, in relation to the project folder
Many people put it in a “src/” subfolder. My personal habit is
“lib/python/blah/…”, and usually there’s a top level “blah” there
representing the project top package name (keeps its modules out of the
way of other names). I put a convenience symlink “blah” →
“lib/python/blah” to make for easy to use names for editors.
- tooling: What are best practices for enforcing linting, formatting, testing, …
There are many linters; I keep a script which runs a few with my
preferred tunings.
“ruff” is getting very popular - it is fast and covers a lot of the
other linters’ tests.
Formatting: lots of people use an autoformatter to apply a style close
to PEP8 (which is the style for Python’s stdlib). The “black” tool is
very popular because is basicly applies an opinionated PEP 8 style with
nearly no tuning options. For various reasons I use yapf, with a style
which is PEP8 with a few tweaks.
I found this that seems to give 80% of the answer, but I don’t know if it really represents todays standards, and it has some holes (e.g. where do I put source code?)
How to set up a perfect Python project
I also found this, but I have the impression it gives tooling but not much conventions
12. Virtual Environments and Packages — Python 3.12.0 documentation
Conventions are largely a personal choice. Do something simple which
you’re already comfortable and make an informed choice later when you’ve
hit some pain points, if any.
Cheers,
Cameron Simpson cs@cskk.id.au