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

Run scripts with inline script metadata #1594

Open
wenkokke opened this issue Jan 20, 2025 · 3 comments
Open

Run scripts with inline script metadata #1594

wenkokke opened this issue Jan 20, 2025 · 3 comments
Labels

Comments

@wenkokke
Copy link

wenkokke commented Jan 20, 2025

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.

@dukecat0
Copy link
Member

dukecat0 commented Jan 20, 2025

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.

@wenkokke
Copy link
Author

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.

@dukecat0
Copy link
Member

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 dukecat0 added the docs label Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants