Settings

Theme

Pip.wtf: Inline dependencies for small Python scripts

pip.wtf

57 points by forgottofloss 2 years ago · 12 comments

Reader

Flimm 2 years ago

A 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

deivid 2 years ago

Why not use something like shiv[0]? You can bundle all your dependencies into one file and distribute that.

[0] https://pypi.org/project/shiv/

diarrhea 2 years ago

I 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/

  • jefferph 2 years ago
    • Flimm 2 years ago

      Fascinating! 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])
greensh 2 years ago

nix can do something similar via nix shell https://nixos.org/manual/nix/stable/command-ref/nix-shell.ht...

extraduder_ire 2 years ago

Cool. I'm surprised this didn't exist earlier.

Now someone upload it to pypi for even more hilarity.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection