3 ms·
Pip.wtf: Inline dependencies for small Python scripts
- extraduder_ire 3y agoCool. I'm surprised this didn't exist earlier. Now someone upload it to pypi for even more hilarity.
- guappa 3y agoThere was (and is) no reason for it to exist since python supports running .zip files that contain all the dependencies.
- tarxvf 3y agoI had never heard of that, reading up on it now. Thanks for posting!
- lawtalkinghuman 3y agopex is a build tool built on the top of the zip trick. A pex file doesn't contain a bundled interpreter, but it zips up the entire virtualenv. Also no Windows support. https://pex.readthedocs.io/en/latest/ https://pex.readthedocs.io/en/latest/
- diarrhea 3y agoI was going to mention that an official solution to this problem is around the corner, but the corresponding PEP has been rejected. Anyone know what happened there? https://peps.python.org/pep-0722/ https://peps.python.org/pep-0722/
- jefferph 3y agoLooks like https://peps.python.org/pep-0723/ https://peps.python.org/pep-0723/ was accepted instead. See discussion here: https://discuss.python.org/t/pep-722-723-decision/36763 https://discuss.python.org/t/pep-722-723-decision/36763
- Flimm 3y agoFascinating! It looks like PEP 723 (the one that was accepted provisionally) will allow you to embed a comment in your script that looks like this, to specify dependencies on request and rich: # /// pyproject # [run] # requires-python = ">=3.11" # dependencies = [ # "requests<3", # "rich", # ] # /// import requests from rich.pretty import pprint resp = requests.get("https://peps.python.org/api/peps.json") data = resp.json() pprint([(k, v["title"]) for k, v in data.items()][:10])
- Flimm 3y agoA development version of pipx supports a feature that address this pain point. See pull request #916 [1] In your script example.py, you would specify your dependencies in a special comment like this: # Requirements: # requests import requests ... Then you could run the script like this: pipx run file:example.py This would install the dependencies in a temporary virtual environment, and run the script in that virtual environment. [1] https://github.com/pypa/pipx/pull/916 https://github.com/pypa/pipx/pull/916
- networked 3y agoThere is also fades (https://github.com/PyAr/fades https://github.com/PyAr/fades) and pip-run (https://github.com/jaraco/pip-run https://github.com/jaraco/pip-run). Debian and Ubuntu package fades.
- greensh 3y agonix can do something similar via nix shell https://nixos.org/manual/nix/stable/command-ref/nix-shell.html#use-as-a--interpreter https://nixos.org/manual/nix/stable/command-ref/nix-shell.ht...
- Kim_Bruning 3y agoI have used nix shell like this before; super handy!
- deivid 3y agoWhy not use something like shiv[0]? You can bundle all your dependencies into one file and distribute that. [0] https://pypi.org/project/shiv/ https://pypi.org/project/shiv/