pythonpython-3.xraspberry-pipyinstaller

How to include system-wide pakages in PyInstaller?


I am running PyInstaller on a Raspberry Pi device to compile a standalone Python script. My script is intended to always run with sudo. It requires smbus, psutil, pyudev, netifaces libraries installed system wide with sudo apt install python3-....

Now to use the PyInstaller I created a virtual environment, activated it, and used pyinstaller --onefile nas_script.py to compile. But when I try to execute it /home/marus/NAS_script/build/nas_script I get an exception saying that it cannot find smbus library.

The smbus is not found neither with pip show smbus nor sudo pip show smbus. It is only found by dpkg -l | grep smbus as python3-smbus:arm64.

When I run PyInstaller it says:

314 INFO: Module search paths (PYTHONPATH):
['/usr/lib/python311.zip',
 '/usr/lib/python3.11',
 '/usr/lib/python3.11/lib-dynload',
 '/home/marus/.env/lib/python3.11/site-packages',
 '/home/marus/NAS_script']

So how can I tell PyInstaller where to find the smbus and how to include it in my standalone executable ?


Solution

  • First locate the smbus Library then copy smbus Module to Your venv Now that the smbus module is available in your virtual environment, you can rebuild the executable:

    Alternatively you can modify the PyInstaller spec file to include specific paths where these libraries reside:

    a = Analysis(['nas_script.py'],
                 pathex=['/usr/lib/python3/dist-packages'],
                 ...
                 )
    

    If PyInstaller doesn't automatically detect these modules, try the --hidden-import option:

    pyinstaller --onefile --hidden-import=smbus nas_script.py
    

    I suggest you use Auto PY to EXE : It is more or less a pyinstaller UI