我有一个python程序,其内容如下:
from flask import Flask, abort
我将其作为 systemd 服务(称为test_flask.service
)如下:
[Unit]
Description=My simple flask service
After=syslog.target network.target
[Service]
Type=simple
Environment=export PYTHONPATH=<path_to_the_python_directory>
ExecStart=/usr/bin/python3 <path_to_the_python_directory>/test_flask.py
[Install]
WantedBy=multi-user.target
当我开始使用它时sudo systemctl start test_flask.service
出现错误:
ImportError: No module named 'flask'
有什么线索吗?
答案1
如果你检查服务状态,你会看到类似这样的内容:
Sep 19 17:46:10 muru-1604 systemd[1]: [/etc/systemd/system/foo.service:7] Invalid environment assignment, ignoring: export PYTHONPATH=/some/path
该Environment
设置未使用 shell 语法。它只接受简单的赋值:
Environment=PYTHONPATH=/some/path