question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow running scripts with dependencies using pipx

See original GitHub issue

How 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:open
  • Created 10 months ago
  • Reactions:4
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
pfmoorecommented, Nov 23, 2022

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!

1reaction
pfmoorecommented, Nov 22, 2022

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Support running & installing scripts with dependencies ...
So pipx can run scripts specified as URLs on the command line, but it seems that ... Allow running scripts with dependencies using...
Read more >
pipx — Install and Run Python Applications in Isolated ...
Expose CLI entrypoints of packages ("apps") installed to isolated environments with the install command. This guarantees no dependency conflicts and clean ...
Read more >
Improving Python Dependency Management With pipx and ...
My current dev setup with python, poetry and pipx. ... Historically, the way to do dependency management was through requirements.txt .
Read more >
Manage Python Dependencies - JWillikers
Install the dependencies needed for asdf. · Pull down the asdf repository in to your home directory. · Checkout the latest version of...
Read more >
Installing stand alone command line tools
Now you can install packages with pipx install and run the package's applications(s) from anywhere. $ pipx install PACKAGE $ PACKAGE_APPLICATION [ARGS].
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found