Documentation for inline script metadata support
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:
- Create a virtual environment.
- Activate the virtual environment.
- Manually read the metadata from the script.
- Manually install each dependency, or create a
requirements.txtorpyproject.tomlfile. - Install the dependencies into the virtual environment.
- 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.
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.
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.
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.