3 ms·
a python script with no dependencies can be distributed as easily as a bash script a python script with dependencies can be pushed and made available to the wo
by GhostWhisperer 3y ago
a python script with no dependencies can be distributed as easily as a bash script
a python script with dependencies can be pushed and made available to the world through PyPI, it then becomes really easy to install in isolated environments with `pipx`
- raffraffraff 3y agoI can't remember the last time I saw a python script that didn't require some pip install. Yesterday I was asked to create a small docker image containing a python script to be used by AWS Lambda. The official AWS container for Python Lambda is over 600mb. I've also had a hell of a time in the past trying to build small python docker images. If you really know what you're doing (and for a novice, that takes a lot of reading) you can usually halve the size of most official containers. Like, how do I create a python venv that also contains the python runtime (not a symbolic link), and libpython, and how do I pip install something that has no wheels, and wtf are wheels. And now I realize that I absolutely need a pip repo to store the wheels I've built, or my pipelines will run for an hour every time. So I have to figure out how to add my repo to pip. And now, can I statically compile my wheels to make them easier to install in a generic container, without having to find libthis and libthat? Finally, how do I copy '/app' into a clean busybox container and activate the venv to run the app. If you do all of that you can have small (ish) containers and fast pipelines (except for the first run that build wheels and runs a script to upload recently built wheels to your repo so you never have to build them again). Phew. What a lot of work. And remember, none of this is "Learning to program in Python", but if you're learning to program in Python from scratch nowadays, all of that stuff becomes extremely important. And the range of different tools is confusing. "Are we still using pip? What about venv? Wtf is poetry?" Or I could simply spend all of that time learning enough Go so I can produce a single binary that can be dropped into a 'FROM scratch' container. Or, as is the case most of the time, I can use alpine Linux and `apk install bash jq curl` and do an astonishing amount of stuff, albeit in nasty bash.
- networked 3y agopipx has merged, though not yet released, support for single-file scripts with dependencies (https://github.com/pypa/pipx/issues/913 https://github.com/pypa/pipx/issues/913). There is also pip-run (https://github.com/jaraco/pip-run https://github.com/jaraco/pip-run) with similar functionality to the new feature in pipx and other options. Their approach of listing dependencies in a comment at the top pf the script has been standardized (with minor differences) in a recent PEP: https://peps.python.org/pep-0722/ https://peps.python.org/pep-0722/. All of this looks good for the future of Python scripts. I have already adopted pipx run script.py in a project: https://github.com/dbohdan/structured-text-tools/blob/d8dc1ac173ceb85776b3dc87c12acea234c96079/render-template.py https://github.com/dbohdan/structured-text-tools/blob/d8dc1a.... It has been convenient and has not caused me problems so far.