A development version of pipx supports a feature that address this pain point. See pull request #916 [1]<p>In your script <i>example.py</i>, you would specify your dependencies in a special comment like this:<p><pre><code> # Requirements:
# requests
import requests
...
</code></pre>
Then you could run the script like this:<p><pre><code> pipx run file:example.py
</code></pre>
This would install the dependencies in a temporary virtual environment, and run the script in that virtual environment.<p>[1] <a href="https://github.com/pypa/pipx/pull/916">https://github.com/pypa/pipx/pull/916</a>
Why not use something like shiv[0]? You can bundle all your dependencies into one file and distribute that.<p>[0] <a href="https://pypi.org/project/shiv/" rel="nofollow noreferrer">https://pypi.org/project/shiv/</a>
I was going to mention that an official solution to this problem is around the corner, but the corresponding PEP has been rejected. Anyone know what happened there?<p><a href="https://peps.python.org/pep-0722/" rel="nofollow noreferrer">https://peps.python.org/pep-0722/</a>
nix can do something similar via nix shell <a href="https://nixos.org/manual/nix/stable/command-ref/nix-shell.html#use-as-a--interpreter" rel="nofollow noreferrer">https://nixos.org/manual/nix/stable/command-ref/nix-shell.ht...</a>