by DangitBobby · 3y ago · 0 comments
How should I be managing environments for these scripts? Do I install dependencies in shared system python? Should I create a shared venv? Where should I store it? Any tools out there that make this decision for you and manage it?
Just the fact that homebrew occasionally updates my installed Pythons and breaks everything makes me reluctant to use Python as a scripting language at all. It's pretty reliable when I have a folder dedicated to a project (poetry is good for this) but I'm pretty fed up with how brittle it all is.
<name>/
pyproject.toml
<name>.py
The pyproject.toml would look like this (using poetry, but you could use a different tool for this): [tool.poetry]
# NOTE: Set <name> to your project name
name = "<name>"
description = "My Script"
version = "0.0.0"
authors = ["Me <me@example.com>"]
[tool.poetry.dependencies]
python = "^3.11"
requests = "^2.28.2"
[tool.poetry.scripts]
# NOTE: Change <name> to match your package name in [tool.poetry]
<name> = "<name>:main"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Then you'd run `pipx install -e .` and the the executable script will be installed in ~/.local/bin.EDIT: Thinking about this some more, doing `brew install pipx` would keep pipx from breaking on brew upgrades, but I guess your installed scripts would still fail if you upgrade from Python 3.N to 3.N+1.
So the only solution, I think, is to keep around all the versions of Python you use, assuming you don't want to upgrade all your scripts when upgrading Python.
I use pyenv to manage Python versions rather than Homebrew, and this seems to avoid the issues you're having, because the Python version used to install whichever tool via pipx sticks around until I explicitly uninstall it.
If you want this automated use pipx, but it is overkill and loses simplicity.
The reason folks give dire warnings against simplicity is that sysad skills have plummeted in recent years. It’s easy to fix the rare conflict by putting the troublemaker in a venv. Outside of a large work project I never need to.
---
Disclaimer before I continue with my own suggestions: I have made small code contributions to pip and pip-tools, and maintain a Zsh frontend to pip-tools+venv, called zpy.
---
Suggestions:
- each folder of code maps to its own default venv, and may map to more venvs for different Python runtimes
- each folder of code has a requirements.in file with top-level dependencies, and a requirements.txt as a lock file
- you can either add a shebang line for your script which explicitly invokes the venv's Python and link that file into ~/.local/bin/, or instead create an external launcher script in ~/.local/bin/.
---
Here's an example of how that might be done using zpy functions:
$ mkdir simple-scripts
$ cd simple-scripts
$ envin # or in subcommand form: zpy envin
==> creating -> ~/.local/share/venvs/280…/venv :: ~/Code/simple-scripts
$ pipacs httpx
==> appending -> requirements.in :: ~/Code/simple-scripts
httpx
==> compiling requirements.in -> requirements.txt :: ~/Code/simple-scripts
anyio==3.6.2 # via httpcore
certifi==2022.12.7 # via httpcore, httpx
h11==0.14.0 # via httpcore
httpcore==0.16.3 # via httpx
httpx==0.23.3 # via -r requirements.in
idna==3.4 # via anyio, rfc3986
rfc3986==1.5.0 # via httpx
sniffio==1.3.0 # via anyio, httpcore, httpx
==> syncing requirements.txt -> env :: ~/Code/simple-scripts
$ print -rl -- 'from httpx import get' 'print(get("https://ifconfig.co/json").json())' >do_it.py
$ vpyshebang do_it.py # or: zpy vpyshebang do_it.py
$ ln -s $PWD/do_it.py ~/.local/bin/do_it
If a Python runtime update breaks your venvs, you can probably fix things up using zpy's pipup function. #!/bin/bash
# myscript.sh
venv/bin/python3 myscript.py
You could also skip the shell script and use aliases in your .bashrc.I'm sure I could come up with solutions that are very robust for my particular machine, but I would like something that allows me to share it as a gist and a teammate could just as easily use it, or I can use it myself on another machine without hassle. In other words, a solution contained within the script iteself and maybe one or two binaries outside that make it possible.
I've actually started using a lot of different CLI tools with Docker, especially when the tool isn't available for my OS.