Executing a Python Script Within a Virtual Environment

I have a python script that I want to use with a KM trigger. Given a book's ISBN in the clipboard, the script download the book metadata from Google with the ISBN and generates an APA citation in markdown format.

I use pyenv so I can have a python environment separate from the built-in macOS version of python3. Following these instructions for setting up a separate python environment on macOS, I created a shim in my .zshrc file so my pyenv version of python gets launched instead of the built-in one.

That allows me to use #!/bin/env python3 in my scripts, but it doesn't work from KM because, as I understand it, the shell KM launches doesn't know anything about the shim. As a result, none of the python libraries I have installed inside my virtual python environment are available. I can make it work if I specify the full path to my virtualized python instance using


at the top of my python script, but this affects maintainability of the script since I could upgrade my python version at any time.

What's the best way to give my KM macro the additional context it needs to execute the shimmed version of python when running the script?

You can try source command .
For example,
whatever environment setup is done in init_test.sh/.bash_profile and can then be run by 'source' command to load them into current process and then execute script file

1 Like

Thanks, @macdevign_mac. I tried adding a source ~/.zshrc, but it still didn't make it possible to run the python3 that's part of my pyenv environment. Your suggestion did give me the idea that has solved my problem though!