Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: fix outdated "run with inline dependencies" example #1244

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,38 @@ pipx run --spec test-py test.py # Always a package on PyPI

You can also run scripts that have dependencies:

If you have a script `test.py` that needs a 3rd party library like requests:
If you have a script `test.py` that needs 3rd party libraries,
you can add [inline script metadata](https://packaging.python.org/en/latest/specifications/inline-script-metadata/)
in the style of PEP 723.

```
# test.py

# Requirements:
# requests
#
# The list of requirements is terminated by a blank line or an empty comment line.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "requests<3",
# "rich",
# ]
# ///

import sys
import requests
project = sys.argv[1]
pipx_data = requests.get(f"https://pypi.org/pypi/{project}/json").json()
print(pipx_data["info"]["version"])
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()][:3])
```

Then you can run it as follows:

```
> pipx run test.py pipx
1.1.0
> pipx run test.py
[
│ ('1', 'PEP Purpose and Guidelines'),
│ ('2', 'Procedure for Adding New Modules'),
│ ('3', 'Guidelines for Handling Bug Reports')
]
```

## `pipx inject` example
Expand Down