Injecting Packages
Outcome: one tool's world gains a
guest package that no other Python on
the machine can see โ and you
understand why that pattern, called
injecting, is how Python tools take
plugins. Transcript:
pipx-lab/transcripts/t07-inject.txt.
Why a world would need a guest
Some tools are built to be extended.
Their extra abilities live in separate
packages: a plugin that adds a feature,
a library the tool can use if it finds
it. Under the old shared-shelf way,
extending a tool was natural โ just
pip install the plugin next to it.
Under pipx, the shelf is private, so
the question is fair: how does anything
else get in?
Deliberately, through the door marked
inject:
pipx inject <tool-package> <guest-package>
The lab's guest
The lab gave pycowsay a guest: the
cowsay package, a different cow
implementation with its own ideas.
Watch the wording of the receipt โ
injected into, not installed:
pipx inject pycowsay cowsay
installing cowsay...
done! โจ ๐ โจ
injected package cowsay into venv pycowsay
pipx opened pycowsay's world, put
cowsay and anything it needed onto
that world's shelf, recorded the guest
in the world's pipx_metadata.json,
and closed the door.
Proving the guest's address
import is the test, as in chapter 6.
Inside the host's world, the guest
answers โ and performs:
~/.local/share/pipx/venvs/pycowsay/bin/python -c "import cowsay; cowsay.cow('a guest in the pycowsay venv')"
____________________________
| a guest in the pycowsay venv |
============================
\
\
^__^
(oo)\_______
(__)\\ )\/\\
||----w |
|| ||
Everywhere else, the guest does not exist:
~/.local/share/pipx/venvs/httpie/bin/python -c "import cowsay"
python3 -c "import cowsay"
ModuleNotFoundError: No module named 'cowsay'
ModuleNotFoundError: No module named 'cowsay'
The host's neighbour cannot see it. The system cannot see it. The guest lives at exactly one address โ which is what a plugin needs: present where the host looks, absent everywhere it would be clutter.
The honest fine print
Two things the lab found while doing
this. First, plain pipx list in
pipx 1.4.3 does not itemise guests โ
it lists host packages only. The record
of a guest lives in the host's
pipx_metadata.json:
python3 -c "import json; m=json.load(open('$HOME/.local/share/pipx/venvs/pycowsay/pipx_metadata.json')); print(list(m['injected_packages']))"
['cowsay']
(Newer pipx releases surface guests in
pipx list directly; the metadata file
is the record either way.)
Second, the lab's guest was a library,
proven at the import line. Guests can
also bring commands of their own,
and pipx can expose those as shims
too, with --include-apps โ and
--include-deps pulls in the
dependencies' commands as well. Those
two flags are documented here rather
than run; the shape is the same, with
more doors planted in ~/.local/bin.
The rule for guests
A guest shares its host's fate. Upgrade the host and pipx keeps the guest list in mind; remove the host โ chapter 10 โ and the guest leaves with it, unlisted and unmissed, because a guest has no life of its own. Choose guests the way you chose the host: chapter 1's trust rule applies to everything that runs as you, guests included.