diarrhea2 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?
# /// 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])
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.
Now someone upload it to pypi for even more hilarity.
https://pex.readthedocs.io/en/latest/