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

Support installing single-file scripts with inline script metadata #1388

Open
jwodder opened this issue May 3, 2024 · 0 comments
Open

Support installing single-file scripts with inline script metadata #1388

jwodder opened this issue May 3, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@jwodder
Copy link

jwodder commented May 3, 2024

How would this feature be useful?

I have a number of small Python scripts with third-party dependencies in my ~/bin that I'm currently using pipx run in their shebangs to run. At the moment (pipx 1.5.0), this results in their venvs being cleaned up after 14 days, resulting in time spent recreating the venvs the next time the scripts are run. Even if an option is added to keep a pipx run venv around (as suggested here), if I later decide to stop using a script, I'll have no way to delete its venv and free up space. Hence, I request that pipx gain the ability to install a single-file Python script as though it were a Python package with dependencies read from inline script metadata.

As a bonus feature, if installing from a URL pointing to a Python script is supported, this would enable highly lightweight "packaging" of scripts. Instead of having to fill out a pyproject.toml and going through all that, authors would be able to distribute single-file programs by just specifying the dependencies inline and telling their users to install with pipx.

Describe the solution you'd like

Add a pipx install-script <path> command1 that takes a path or URL to a Python file, extracts any dependency information from inline script metadata, creates a non-ephemeral virtual environment with the dependencies installed, copies the script into the venv's bin/ directory, makes the script executable, and creates a symlink to the script in $PIPX_BIN_DIR. By default, the basename of the installed script will be unchanged from the source script, but the user can pass a --name <BASENAME> option to specify a new basename (useful for getting rid of .py extensions on installation).

The venv thus created acts just like a "normal" pipx venv created by pipx install: you can get rid of it with pipx uninstall, it shows up in pipx list (albeit without any version info), you can pipx (un)inject stuff into it, you can pipx reinstall it, etc. (I'm not sure what pipx upgrade should do on such venvs; probably the same thing as pipx reinstall.)

Describe alternatives you've considered

  • Preparing piddling projects as proper Python packages: Too much overhead for things rarely over 150 lines
  • Using pipx run as a script's shebang: See above

Footnotes

  1. This could also be implemented by extending pipx install, but for the moment I'm keeping the --name option separate from that.

@chrysle chrysle added the enhancement New feature or request label May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants