linkedin / shiv

shiv is a command line utility for building fully self contained Python zipapps as outlined in PEP 441, but with all their dependencies included.
BSD 2-Clause "Simplified" License
1.75k stars 99 forks source link

How to use single shiv zipapp/pyz for multiple python scripts #230

Closed sudarshanvm closed 1 year ago

sudarshanvm commented 1 year ago

Suppose I have 3 files : requirements.txt, module1.py and module2.py I need to bake a single shiv executable with all the dependencies specified inside requirements.txt and later I want to use the same shiv executable for running module1.py as well as module2.py

can we do something like:

does this work? do we have any alternative to do that?

Thanks, Sudarshan

lorencarvalho commented 1 year ago

Hi @sudarshanvm,

A pyz created with shiv should work exactly as you describe. In fact, internally at LinkedIn we sometimes package multiple CLIs that share a set of dependencies this way. Another option is to use the pyz as the shebang for a script, like so:

$ shiv httpx -o httpx.pyz --quiet
$ cat << EOF > tryme.py
> #!/usr/bin/env httpx.pyz
>
> import httpx
> url = "https://shiv.readthedocs.io"
> response = httpx.get(url)
> print(f"Got {response.status_code} from {url}!")
>
> EOF
$ chmod +x tryme.py
$ ./tryme.py
Got 200 from https://shiv.readthedocs.io!