I have API server using loopback4 framework with nodejs and python file that is called by api.
When I tested in local using just terminal, it works well. So, I tried to make it as a systemd service, the API server works well but the python file has a ModuleNotFoundError about it's python package like opencv.
I moved top directory using cd /
and checked python packages pip list
,but there is the same package and version in python project uses.
How can I map the correct package to python file when running python file with systemd api service?
Asked
Active
Viewed 221 times
0

BigJ
- 21
- 1
- 5
1 Answers
0
Running this via systemd
is going to use the system-installed set of packages. It is quite possible that your OpenCV installation (or whichever package) has gone to $HOME/.local
- and that's not accessible to systemd.
Either use OS packager-derived packages for your dependencies (eg, apt install opencv
) or create and activate a virtualenv (venv), install the packages you need into that directory, then explicitly invoke your systemd-run script with the python interpreter from that venv directory.
For example:
# python3 -mvenv /opt/my-venv
# . /opt/my-venv/bin/activate
(my-venv) # pip install -r requirements.txt
(edit your systemd unit to use /opt/my-venv/bin/python
)
(my-venv) # systemctl daemon-reload
(my-venv) # deactivate
# systemctl enable my-service && systemctl start my-service

James McPherson
- 2,476
- 1
- 12
- 16
-
Thx for giving me an opinion, but I followed the steps that you guided it doesn't works.. I guess the reason is systemd exec runs API server(nodejs), not python...I reinstall packages using requirements.txt and daemon-reload when venv is activating, and deactivate the venv. And do enable and start my service, but the same error is occured... – BigJ Jan 05 '22 at 02:03