Today I Learned (April 05, 2024)

How to package a Python application using Pyinstaller utilising virtual environments

Let’s say you have an application in a virtual environment like so

python -m venv /home/myapp/.venv
source /home/myapp/.venv/bin/activate
# ... install dependencies

then you may point Pyinstaller to use the dependencies installed in the virtual environment .venv by adding it’s lib/site-packages/python<the-python-version>/site-packages to the dependencies included in the packaged executable via the --paths option even when you’re outside the virtual environment

pyinstaller --paths /home/myapp/.venv/lib/site-packages/python<the-python-version>/site-packages /home/myapp/

:information_source: Replace <the-python-version> with the Python version you are using in your virtual environment.