Score:0

Ubuntu service file not finding module from environment

la flag

My Linux service runs a script that produces an error that a module is not found.

However, when I run the script from the terminal with the python environment activated the script works fine.

I run the script in the terminal with the exact same environment /home/nick/environments/social_agg/bin/python activated and this is referenced in my .service file.

I believe my problem is the environments (1-terminal and 2-service file) are different somehow. I’ve created many service files similarly -- adding python packages to the venv referenced in ExecStart= /home/nick/environments/social_agg/bin/python of the .service file has never been a problem so I assumed the same environments were being used.

Here is the github repo for goodreads-user-scraper package that is not being recognized. Could there be something about this package since it runs from the terminal that makes it different than other python packages?

Any thoughts at all on how to approach this would be great. At this point I'm not really sure where to look next.

Error

Jan 13 17:30:25 speedypersonal2 python[17585]: /bin/sh: 1: goodreads-user-scraper: not found

socialAggregator.service

[Unit]
Description=Run Social Aggregator --- DEVELOPMENT ---
After=network.target

[Service]
User=nick
ExecStart= /home/nick/environments/social_agg/bin/python /home/nick/applications/socialAggregator/scheduler.py --serve-in-foreground

[Install]
WantedBy=multi-user.target
  • update 2023-01-15 working service *
[Unit]
Description=Run Social Aggregator scheduler - collect data from API's and store in socialAggregator Db --- DEVELOPMENT ---
After=network.target

[Service]
User=nick
ExecStart= /home/nick/environments/social_agg/bin/python /home/nick/applications/socialAggregator/scheduler.py --serve-in-foreground
Environment=PATH=/home/nick/environments/social_agg/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin


[Install]
WantedBy=multi-user.target
guiverc avatar
cn flag
You've not told us what Ubuntu product & release you're using? We're limited to the details you provide us, and not knowing that basic detail we can't currently help much. Your question is unclear due to missing information.
hr flag
Your title mentions a "module", but `goodreads-user-scraper` looks like an executable python *script* that you are perhaps running from your script via a python subprocess (hence the `/bin/sh` in the error message)? If you installed via pip3 as a user, it likely lives somewhere like `~/.local/bin`, which is in the PATH of your interactive shell, but not in the PATH of the service environment.
costa rica avatar
la flag
@steeldriver, thanks! I think that's it and yes, its an executable python script - I'm still learning. This is what I did: 1)copied echo $PATH from the terminal with the active venv. 2) pasted that string in my service file like this `[Service] User=nick ExecStart= /home/nick/environments/social_agg/bin/python /home/nick/applications/socialAggregator/scheduler.py --serve-in-foreground Environment=PATH=<my_terminal_PATH_pasted_here>` It works, but maybe I am completely removing environment variables from the .service environment? Is there a better way to do it?
hr flag
@costarica probably the simplest solution would be to call the program by its full absolute path in your python script
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.