-
-
Notifications
You must be signed in to change notification settings - Fork 79
Appendix S Working in a Managed Python Environment
Often, when using python, multiple python programs from multiple supplies may need to be installed on the same system. Sometimes the library requirements of these python programs can conflict, which is not uncommon with open source software. Also, sometimes the maintainer of the system / server may not be the same person who is developing and running python programs on they system. To address these two issues, Python supports virtual environments to be created on a per application basis. This allows each python application to own and managed it's own set of installed libraries. Supporting virtual environments with python has been optional by default with the raspberry pi OS until Bookworm (Debian 12). Beginning with this version of the operating system the default behavior is to require python programs to use virtual libraries.
The Raspberry Pi operating system added a file that enables the requirement of virtual environments for python programs. This file location is dependant on the python version, for example for Python V3.11 the file named /usr/lib/python3.11/EXTERNALLY-MANAGED. You have one of two options:
- You can rename this file (for example to /usr/lib/python3.11/EXTERNALLY-MANAGED.bak and remove the original filename) to disable this virtual environment requirement.
- Leave the virtual environment requirement enabled and genmon will detect this and use a virtual environment.
In normal option when using genmon, the installation, activation and deactivation of the virtual environment is handled by the startgenmon.sh or the genmonmaint.sh scripts.
If you want to run programs directly from the command line or if you want to run a python program in the ./genmon/OtherApps folder that requires serial or modbus libraries, then you must manually activate and deactivate the virtual environment.
Once the virtual environment is installed and initialized by the setup script genmonmaint.sh, you can activate the virtual environment manually from the genmon folder with this command:
source ./genenv/bin/activate
Once this command is executed your prompt on the command line will show genenv. Now you can execute python commands in the virtual environment, however you must use the python3 or pip3 binary in ./genmon/genenv/bin. For example:
cd genmon
source ./genenv/bin/activate
sudo ./genenv/bin/python3 ./OtherApps/serialtest.py
deactivate
or
cd genmon
source ./genenv/bin/activate
sudo ./genenv/bin/python3 ./OtherApps/mopeka_utility.py
deactivate
To deactivate the python virtual environment type this command:
deactivate