Making Python Fun Again
1 min readApr 29, 2024
The big pain of Python is dependency management — it’s slow, complicated, and no clear best approach to do it.
There is a new tool called uv that is a drop-in replacement for pip-tools that is super fast.
Here is a simple shell script that demonstrates how to use it!
Shell Script
I named this shell script py.
#!/bin/bash
set -e
[ -f "pyproject.toml" ] || (
echo "Expected pyproject.toml"
exit 1
)
[ -d ".venv" ] || (
echo "Creating virtual environment"
python3 -m venv .venv
.venv/bin/python3 -m pip install uv
)
[ "requirements.txt" -nt "pyproject.toml" ] || (
echo "Updating requirements.txt"
.venv/bin/uv pip compile -q --all-extras --generate-hashes pyproject.toml -o requirements.txt
)
[ ".venv/installed_requirements.txt" -nt "requirements.txt" ] || (
echo "Updating environment"
.venv/bin/uv pip sync requirements.txt
cp requirements.txt .venv/installed_requirements.txt
)
CMD=$1
shift
exec ".venv/bin/${CMD}" "$@"
Starter pyproject.toml
A minimal pyproject.toml can be as follows
[project]
name = "test-project"
dependencies = [
"pandas",
]
Try it out
Try using Pandas by calling py python3
then calling import pandas
.
Now you want a new package? Add it in. For example, add rich into pyproject.toml. And run py python3
again and you’ll have it.
Conclusion
I hope this inspires some ideas of how to leverage uv.