Live data from Hacker News

Pip.wtf: Inline dependencies for small Python scripts

pip.wtf

1–10 of 13 posts

Re: Pip.wtf: Inline dependencies for small Python scripts

#4
post #3

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/

Looks like https://peps.python.org/pep-0723/ was accepted instead. See discussion here: https://discuss.python.org/t/pep-722-723-decision/36763

Re: Pip.wtf: Inline dependencies for small Python scripts

#6
post #4
post #3

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/

Looks like https://peps.python.org/pep-0723/ was accepted instead. See discussion here: https://discuss.python.org/t/pep-722-723-decision/36763

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

Re: Pip.wtf: Inline dependencies for small Python scripts

#7
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

Re: Pip.wtf: Inline dependencies for small Python scripts

#9
post #5

Cool. I'm surprised this didn't exist earlier. Now someone upload it to pypi for even more hilarity.

There was (and is) no reason for it to exist since python supports running .zip files that contain all the dependencies.

I had never heard of that, reading up on it now. Thanks for posting!
Post reply on HN