Score:0

When running a python script as a service, I get module not found

na flag

I have a small script that uses flask to listen to API requests and forwards them to Home Assistant and InfluxDB. It currently has a dummy name, "script.py" and is placed in /usr/local/bin. When I run it standalone, by using python3 script.py, it works fine.

Since I want this script to be always running, I'd like it as a service. I created the following service in /usr/local/etc/rc.d/

#!/bin/sh

# PROVIDE: rest
# REQUIRE: DAEMON NETWORKING
# BEFORE: LOGIN
# KEYWORD: shutdown

. "/etc/rc.subr"
location="/usr/local/bin"
name="script"
rcvar=rest_enable

: ${rest_enable:="YES"}
logfile="/var/log/rest/${name}.log"
command="$location/$name.py"
command_args="$1"
command_interpreter="/usr/local/bin/python3"

load_rc_config $name
run_rc_command "$1"

The output is

Starting script.
Traceback (most recent call last):
 File /usr/local/bin/script.py", line 4, in <module>
  from flask import Flask, request, Response
ModuleNotFoundError: No module named 'flask'
/usr/local/etc/rc.d/rest: WARNING: failed to start script

Running a whereis on python3 produces the following:

root@grafana:/usr/local/etc/rc.d # whereis python3
python3: /usr/local/bin/python3

So from my understanding this should be fine. What am I missing?

Score:0
ar flag

There's broadly speaking three alternatives:

  1. You have installed flask as a user by running pip3 install flask
  2. You have a virtual env as user.

Install the flask module system wide, specify an absolute path to flask in your script, or set up a proper venv.

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.