pipx icon indicating copy to clipboard operation
pipx copied to clipboard

Documentation for inline script metadata support

Open wenkokke opened this issue 11 months ago • 3 comments

I propose that we extend pipx to run scripts with inline metadata, as:

pipx run-script ./my-script.py

How would this feature be useful?

Inline script metadata lets script authors specify dependency metadata inline in a script. Presently, this is intended to be used by IDEs when interacting with the script.

At the time of writing, there is no convenient way to run a script with inline metadata, even though all the required information is there. If I have a script, my-script.py, with inline metadata, I would have to:

  1. Create a virtual environment.
  2. Activate the virtual environment.
  3. Manually read the metadata from the script.
  4. Manually install each dependency, or create a requirements.txt or pyproject.toml file.
  5. Install the dependencies into the virtual environment.
  6. Run my script.

Describe the solution you'd like

I suggest that we extend pipx to perform the steps I outlined above, either when running pipx run ./my-script.py (which extends the behaviour of the existing run commandi) or when running pipx run-script my-script.py (which creates a new command to ensure backwards compatibility).

All the necessary information is present in the inline metadata block and there is a reference implementation for extracting that metadata from the script.

This would significantly ease the use of scripts with inline metadata and would allow us to use a shebang such as #!/usr/bin/env pipx run-script.

Describe alternatives you've considered

Since pipx is the canonical tool for executing Python scripts in on-the-fly virtual environments, I believe this is the correct tool to support this feature.

wenkokke avatar Jan 20 '25 19:01 wenkokke

Which version of pipx are you using? The latest version already supports this feature. You can run the script using pipx run ./my_script.py.

dukecat0 avatar Jan 20 '25 23:01 dukecat0

Which version of pipx are you using? The latest version already supports this feature. You can run the script using pipx run ./my_script.py.

Woops, my bad! That's great news!

I couldn't find this on the documentation, but maybe I looked in the wrong places.

wenkokke avatar Jan 21 '25 18:01 wenkokke

I couldn't find this on the documentation, but maybe I looked in the wrong places.

You can find an example in this section: https://pipx.pypa.io/stable/examples/#pipx-run-examples

However, I think the docs or help page can be improved so it's easier to discover this functionality.

dukecat0 avatar Jan 22 '25 13:01 dukecat0