Python
Sammelsurium with respect to PythonDeveloping applications and packages in python can be daunting. Here are a few recipes.
This post is for the development of python programs that span multiple files and also might need a proper bundling and setup.
A few things about Python #
Before Python there were script languages like AWK and Perl which were used for more advanced tasks than a simple shell script could handle. Perl was notorious for being difficult to read. Python came with a simple syntax, indentation was mandatory and a replacement for nested parentesis. Moreover, the runtime made it easy to combine python with compiled code and therefore python acts like a glue for more complex libraries.
Vast ecosystem of libraries #
If you need something done, want to avoid writing compiled programs from scratch, and it involves commonly used functionality, then the chances are good that there exist a python library that can be used. Due to the readability of Python, examples are easy to read and to adapt to ones task. Python code also tends to be open source, hence there is good vibe coding support.
Global interpreter lock #
Python uses reference counting for automatic memory management. Due to historic reasons, all python instructions are executed while holding the global interpreter lock. With a few exceptions, this means that python code is executed in a serial manner, even though one can run python in several threads. The exceptions are calls to native libraries, which can release the lock, carry out complex calculations and then require the lock again before returning results.
Freedom, rules and conventions #
Before implementing something and making design decisions, search if there are recommendations how to setup and structure the program. Python itselfs has Python Enhancement Proposals, short PEP, which outline common rules. For instance, one can use an indent of 1 space, but PEP 8 mandates 4 spaces, prohibits mixing of tabs and spaces. Some tools only work, if certain conventions are followed.
Tools #
Miniconda or Micromamba #
Depending on the operating system and licensing requirements, either might be used to setup an environment.
In the following, I am using miniconda, but the commands for micromamba are similar.
miniconda can create and update virtual environments for Python, so called venv based on a specification in YAML.
|
|
The previous command will use the following file and create an environment with pytorch with CUDA support and a few other dependencies.
|
|
UV (formerly called puffin) #
The name puffin can be used when asking ChatGPT for advice, it is the former work title for
uv, the fast package manager and runner for python. UV is a fast package manager for Python by Astral. Documentation is here.
For development, use uv instead of conda.
Not only does it handle a virtual environment, it also installs packages, updates the pyproject.toml file.
Moreover, it can run tools.
Common uv commands #
When starting a fresh project or maintaining an existing one, these commands are the essentials:
| command | explanation |
|---|---|
uv init --package <myapp> |
Create a new project with a pyproject.toml. |
uv venv |
Create or update the local virtual environment. |
uv add <package> / uv remove <package> |
Add or drop a dependency and refresh the lock file. |
uv sync |
Install everything from the lock file into the virtual environment. |
uv run <myapp> |
Run the app (automatically inside the venv). |
uv run <cmd> |
Run arbitrary commands inside the environment (for example uv run python main.py). |
uv lock |
Regenerate the lock file after manual edits. |
uv tool install <pkg> |
Install CLI tools (formatters, linters) managed by uv. |
uv tree |
Inspect the dependency tree to see what pulls in which package. |
uv format |
Format all source files. |
uv build |
Build a wheel for distribution. |
uvx twine |
Upload packages to a repository. |
uv version --bump <level> |
bump the project version to the next level, f.e. dev |
Visual Studio Code or VS Codium #
Ctrl+Shift+Pgives you easy access to commands that allow you to select environment and interpreter.
One can install the “Python” and the “Python Debugger” extensions from “ms-python”, together with “Ruff” for formatting. It is enough to get a running debugger. Make sure that the right debugger is selected and runs in the right environment.