-
Notifications
You must be signed in to change notification settings - Fork 10
510 error: externally‐managed‐environment ‐‐ venv
if you get the above message when trying to install serial or phao-mqtt, just switch to a virtual environment as proposed in the message details:
change to your directory /optolink-splitter
cd optolink-splitter
and enter
python -m venv myvenv
Python will now create a subfolder /myvenv in your /optolink-splitter folder where everything for your virtual environment will be located.
source myvenv/bin/activate
now your prompt starts with '(myvenv)' what clearly indcates that you are operating in your virtual environment.
etc without getting annoying warnings:
pip install pyserial
pip install paho-mqtt
That should be all. now you can start your optolink splitter by
python optolinkvs2_switch.py
almost everything required by the script is now located in the /myvenv folder. If you update/change anything outside your ve that would break the compatibility with the script, the ve is not affected. Everything (python, time, importlib, threading, serial, phao, ...) is used from /myvenv.
deactivate
and your prompt returns to normal (and your optolink-splitter will not work)
you need to reference the ve:
sudo nano /etc/systemd/system/optolink-splitter.service
then paste the following content (adjust user and folder in case):
[Unit]
Description=Optolink-Splitter Service
After=network-online.target
[Service]
Type=simple
User=pi
Group=pi
WorkingDirectory=/home/pi/optolink-splitter/
Environment="PATH=/home/pi/optolink-splitter/myvenv/bin"
ExecStart=/home/pi/optolink-splitter/myvenv/bin/python /home/pi/optolink-splitter/optolinkvs2_switch.py
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
(if you require an additional delay eg. for waiting for a WiFi connction, try adding ExecStartPre=/bin/sleep 30
in the Service section.)
save the file and afterwards update the deamon
sudo systemctl daemon-reload
and run the service
sudo systemctl start optolink-splitter.service
if it starts without trouble, activate the auto-starting when booting:
sudo systemctl enable optolink-splitter.service
as usual use the commands in case
sudo systemctl status optolink-splitter.service
to show the status
sudo systemctl stop optolink-splitter.service
to stop execution
sudo systemctl restart optolink-splitter.service
to restart the script/service e.g. after some code change
sudo systemctl disable optolink-splitter.service
to disable auto-start on boot
ps. with later Python versions (3.12+ ?) you might have to use like
ExecStart=/bin/bash -c 'source /home/pi/optolink/myvenv/bin/activate && exec /home/pi/optolink/myvenv/bin/python /home/pi/optolink/optolinkvs2_switch.py'