I’m documenting a workflow with a jupyter notebook, and I wanted to embed shell commands when that makes sense. Something like this, where some_command
is a script they’ve installed with this package:
# here's the data
data_path = Path("/where/the/data/is")
# then we run this command
! some_command {data_path}
An issue I’ve hit is that the notebook is running in a different environment from jupyterlab
, so some_command
is not on the path. I can get this to work with a full path, e.g. ! /path/to/env/bin/some_command ...
but I don’t want to hard-code the path because the user’s environment can be set up differently.
With importlib.metadata
I can retrieve the entry point command directly, which is pretty slick:
import importlib.metadata
ep, = importlib.metadata.entry_points(name="some_command")
ep_cmd = ep.load()
ep_cmd(data_path) # look ma, no shell!
Sadly this has a pretty nasty UX interaction with jupyter
if the command calls sys.exit
, and I want to avoid that. In any case I’d prefer to write shell command there because it’s much less esoteric.
One way to achieve this is to infer the location of the entry point from sys.executable
(some_command
should be sitting next to python
, as I understand it). But I was curious if there’s a nice way to use importlib.metadata
to tell me the path to the executable for a given tool.
Any tips?