Allow running scripts with dependencies using pipx
See original GitHub issueHow would this feature be useful? At the moment, to run a Python script that depends on 3rd party packages, it is necessary to manually create a virtual environment, populate it with the dependencies, and then run the script. And then delete the environment (or keep it if you think you’re likely to re-run the script and remember to delete it later).
pipx run
allows you to run packages in a temporary venv with all dependencies set up, but it doesn’t work for scripts. It allows you to run scripts, but that doesn’t use any of the venv mechanisms, just running the script with the system Python interpreter.
Describe the solution you’d like If a script needs dependencies, use a temporary virtualenv, the same as for packages, and run the script in that.
To determine if a script needs dependencies, a very simplistic approach would be to check if --pip-args
was specified. So invocation would be something like:
pipx run --pip-args numpy file:myscript.py
Even better would be to parse the script, looking for an embedded list of dependencies to install. This would require agreeing on a format for the list. My initial proposal would be something simple, like:
# Requirements:
# <Specifier>
# <Specifier>
<blank line>
The specifiers could be any requirement spec acceptable to pip, so numpy
, or click>=7.0
, or even a URL.
Describe alternatives you’ve considered
The pip-run
command offers similar functionality, but it recreates the environment every time, which makes for very slow runtimes. It also has (IMO) a clumsy UI for specifying the packages to install and the script invocation.
Possible Issues Script dependencies could potentially change more frequently than package dependencies do. So there’s a potential for the cached environment to no longer match the declared requirements. This is also a problem with a package, though, so it’s possibly something we can live with. Maybe having an option to re-create the cached environment would be enough to alleviate this issue?
Implementation I would be happy to create a PR implementing this feature, if the view is that it would be a good idea.
Issue Analytics
- State:
- Created 10 months ago
- Reactions:4
- Comments:16 (9 by maintainers)
Top GitHub Comments
I have a working proof of concept. There’s one significant issue I need to resolve before submitting an initial PR, which is that pipx assumes that an environment will have a “main package” (this is checked in
_validate_before_write
when writing the pipx metadata file). For the case of an environment supporting a script, this clearly won’t be the case. For now, I’ve hacked it by arbitrarily saying the first requirement is the “main package”, but that’s just to check my logic.I think I need to refactor to allow (temporary) environments without a main package, but I’d appreciate any insights the @pypa/pipx-committers (or anyone else!) might have on how deeply embedded the assumption is that an environment has a main package. I’ll do the research myself, but any pointers on where to look for potential problems would be very helpful!
Thanks for your comments, they made me think about some issues that I might not have spotted otherwise (validating requirements) so the PR will definitely be better for this conversation!