4 ms·
A bit unrelated, but one thing I absolutely love is the fact you can install it by copying a single file to a folder in your PATH. I have been trying to follow
by polyrand 3y ago
A bit unrelated, but one thing I absolutely love is the fact you can install it by copying a single file to a folder in your PATH. I have been trying to follow this approach for my Python scripts (standard library only, everything in one file) and I really enjoy the experience. Most of the features I need only require Python 3.8, and Ubuntu comes with Python pre-installed, so
rsync
chmod +x
"just works".
- BiteCode_dev 3y agoYou can use shiv to make any script with deps a single file
- polyrand 3y agoYes, I've used shiv and PEX in the past, and I love those. But that means "adding a build step". Also, as far as I understand, by default, those tools normally decompress the generated zip file on startup, but the decompressed artefacts have to be manually cleaned. I think they are worth for more complex apps (I actually have a draft blog post with some experiments I did using PEX + gunicorn), but for my use case, it's not worth the effort when I only need the standard library.
- networked 3y agoYou can try https://github.com/jaraco/pip-run https://github.com/jaraco/pip-run or https://github.com/PyAr/fades https://github.com/PyAr/fades (or eventually pipx: https://github.com/pypa/pipx/issues/913 https://github.com/pypa/pipx/issues/913). They don't require a build step. For example, #! /usr/bin/env -S pip-run Jinja2==3.* from jinja2 import Environment env = Environment(autoescape=True) template = env.from_string("Hello, {{ name }}!") print(template.render(name="world")) There is a downside. Because pip-run recreates the virtualenv every time, the script takes a second to start up. pipx will cache virtualenvs once the single-file script feature is released. I haven't used fades yet. Edit: fades caches virtualenvs. #! /usr/bin/env fades from jinja2 import Environment # fades Jinja2==3.* env = Environment(autoescape=True) template = env.from_string("Hello, {{ name }}!") print(template.render(name="world"))
- linsomniac 3y agoIf you do that, you may want to know about this magic to test code that's in a single file Python CLI program: https://linsomniac.com/post/2023-03-21-python_testing_a_cli_script_without_py/ https://linsomniac.com/post/2023-03-21-python_testing_a_cli_... I like to set up at least some tests on my scripts so that I can reduce the number of times I push something out that is obviously broken. pre-commit can also help with preventing shipping things with syntax errors if you enable the "ast" check, which does a simple syntax check on the code.