-
Notifications
You must be signed in to change notification settings - Fork 108
docs/how-to: Shipping Dependency Sets as a Single File #134
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
base: main
Are you sure you want to change the base?
Changes from all commits
d12518f
cedc716
15fdc87
cd27a36
176d0a7
be7e2af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| ********************** | ||
| How-Tos, Tips & Tricks | ||
| ********************** | ||
|
|
||
| This chapter contains some practical examples of using ``shiv`` in the real world, | ||
| to give you a better idea how it can fit into your projects | ||
| and their deployment workflows. | ||
|
|
||
|
|
||
| Shipping Dependency Sets as a Single File | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about referring to these as "frozen environments" rather than "dependency sets". This is usually how we refer to them internally, but I want to know if it makes sense in the broader Python community. |
||
| ========================================= | ||
|
|
||
| If you have a bunch of Python scripts that are all using the same set of base libraries, | ||
| you can distribute those dependencies in the form of a zipapp, | ||
| i.e. in a single executable file. | ||
| This way, you can have a project for the base libraries | ||
| and other projects for the scripts, | ||
| including scripts written by your end users. | ||
|
|
||
| The following example uses ``shiv`` itself for demonstration purposes, | ||
| you'd use a setuptools project defining the needed dependencies | ||
| when applying this ‘for real’. | ||
|
|
||
| So, to create your base library release artifact, call | ||
| ``shiv`` like this in your project's workdir:: | ||
|
|
||
| PROJECT=shiv | ||
| shiv -p '/usr/bin/python3.6 -IS' -o ~/bin/_lib-$PROJECT . | ||
|
|
||
| Note that we do not provide an entry point here, which means this zipapp | ||
| drops into the given Python interpreter and is thus usable *as* an | ||
| interpeter, but with the dependencies of the project added. | ||
|
|
||
| The ``-IS`` options in ``-p`` ensure this zipapp runs isolated (for increased security), | ||
| with neither the current working directory | ||
| nor the host's site packages in the Python path. | ||
|
Comment on lines
+34
to
+36
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's omit this (and the flags) in the example, to not confuse users and discourage copy-pasted boilerplate. |
||
|
|
||
| .. tip:: | ||
|
|
||
| Replace the ``.`` at the end of the ``shiv`` call | ||
| by a literal list of PyPI packages, | ||
| or load them from a requirements file (``-r lib-requirements.txt``), | ||
| and you don't need an extra project for getting your base set defined. | ||
|
Comment on lines
+38
to
+43
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good tip, can you call out the |
||
|
|
||
| Now we can exploit this to write a script using the zipapp as its interpreter:: | ||
|
|
||
| cat >script <<'.' | ||
| #! /usr/bin/env _lib-shiv | ||
| import sys | ||
| from pathlib import Path | ||
| import shiv | ||
|
|
||
| print('Imported shiv from', | ||
| Path(shiv.__file__).parent, | ||
| '\nPython path:', | ||
| '\n'.join(sys.path), | ||
| sep='\n') | ||
| . | ||
| chmod +x script | ||
| ./script | ||
|
|
||
| Calling the script produces the following output:: | ||
|
|
||
| Imported shiv from | ||
| /home/user/.shiv/_lib-shiv_8a…e9/site-packages/shiv | ||
|
|
||
| Python path: | ||
| /home/user/bin/_lib-shiv | ||
| /usr/lib/python36.zip | ||
| /usr/lib/python3.6 | ||
| /usr/lib/python3.6 | ||
| /usr/lib/python3.6/lib-dynload | ||
| /home/user/.shiv/_lib-shiv_8a…e9/site-packages | ||
|
|
||
| The underscore prefix in the zipapp name indicates this is not a command | ||
| humans would normally use – alternatively you can deploy into e.g. | ||
| ``/usr/local/lib/python3.6/`` and then use an absolute path instead of | ||
| an ``env`` call in the script's shebang. | ||
|
Comment on lines
+75
to
+78
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can omit this, don't want to overload the reader with info that's not critical. |
||
|
|
||
|
|
||
| External Resources | ||
| ================== | ||
|
|
||
| Here are some links to related articles on the web. | ||
|
|
||
| * `Python Alternative to Docker`_ – Comparing shiv to Docker as the core tool of a deployment workflow, what are the differences between and advantages of both options. | ||
| * `Enabling Easy Zipapp Installs on Windows`_ – Describes the required steps to make zipapps really shine on Windows (i.e. make them work pretty much like on POSIX systems). | ||
|
|
||
|
|
||
| .. _`Python alternative to Docker`: https://www.mattlayman.com/blog/2019/python-alternative-docker/ | ||
| .. _`Enabling Easy Zipapp Installs on Windows`: https://py-generic-project.readthedocs.io/en/latest/installing.html#enabling-easy-zipapp-installs-on-windows | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,6 +116,7 @@ Table of Contents | |
| :maxdepth: 2 | ||
|
|
||
| cli-reference | ||
| how-tos | ||
| history | ||
| api | ||
| django | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we omit this entry for this change? I appreciate that we want to educate users about what is possible with shebangs, but given that the tool in question (shiv) has no opinion about the shebang, I'd rather not introduce this concept in our documentation.