Skip to content

510 error: externally‐managed‐environment ‐‐ venv

Phil edited this page Dec 26, 2024 · 10 revisions

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:

create the virtual environment:

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.

then start the virtual environment by

source myvenv/bin/activate

now your prompt starts with '(myvenv)' what clearly indcates that you are operating in your virtual environment.

now you can install all the requirements

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.

to exit the virtual environment enter

deactivate

and your prompt returns to normal (and your optolink-splitter will not work)

if you like to start optolink-splitter as a service

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'