I'm trying to run flask when my raspberry pi turns on. I created a service for my flask API, this is the service file:
[Unit]
Description= Flask API
After=network.target
[Service]
WorkingDirectory=/home/pi/bucketlist/
ExecStart=flask run --host=0.0.0.0
Restart=Always
[Install]
WantedBy=multi-user.target
But when I try sudo systemctl start flask
then sudo systemctl status flask
I get this error.
Jun 10 09:56:40 raspberrypi flask[4769]: Usage: flask run [OPTIONS]
Jun 10 09:56:40 raspberrypi flask[4769]: Error: While importing "app", an ImportError was raised:
Jun 10 09:56:40 raspberrypi flask[4769]: Traceback (most recent call last):
Jun 10 09:56:40 raspberrypi flask[4769]: File "/usr/lib/python3/dist-packages/flask/cli.py", line 235, in locate_app
Jun 10 09:56:40 raspberrypi flask[4769]: __import__(module_name)
Jun 10 09:56:40 raspberrypi flask[4769]: File "/home/pi/bucketlist/app/__init__.py", line 5, in <module>
Jun 10 09:56:40 raspberrypi flask[4769]: from flask_api import FlaskAPI
Jun 10 09:56:40 raspberrypi flask[4769]: ModuleNotFoundError: No module named 'flask_api'
Jun 10 09:56:40 raspberrypi systemd[1]: flask_api.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Jun 10 09:56:40 raspberrypi systemd[1]: flask_api.service: Failed with result 'exit-code'.
When I type flask run --host=0.0.0.0
manually it works fine, I don't know why the service gives this error.
More info:
I also tried python /home/pi/bucketlist/run.py
instead of flask run --host=0.0.0.0
in ExecStart.
I have a virtual environment set-up in the bucketlist directory.
Python 2.7.16
Flask 1.1.4
Werkzeug 1.0.1