如何使用用户';是通过systemd进行的吗?Python是通过SCL安装的

如何使用用户';是通过systemd进行的吗?Python是通过SCL安装的,python,systemd,pipenv,software-collections,Python,Systemd,Pipenv,Software Collections,在CentOS 7服务器上,我通过SCL安装了Python 3.6。() 我在.bashrc中有这一行代码来启用SCL的Python3.6 source scl_source enable rh-python36 我已经安装了pipenv: pip install --user pipenv 我通过命令行运行Python程序: pipenv run python myprogram.py 所有这些都很有效。我有一个使用用户pipenv的Flask应用程序。我正在尝试创建一个systemd单

在CentOS 7服务器上,我通过SCL安装了Python 3.6。()

我在.bashrc中有这一行代码来启用SCL的Python3.6

source scl_source enable rh-python36
我已经安装了pipenv:

pip install --user pipenv
我通过命令行运行Python程序:

pipenv run python myprogram.py
所有这些都很有效。我有一个使用用户pipenv的Flask应用程序。我正在尝试创建一个systemd单元文件来启动/停止/重新加载Flask web应用程序。如何获取系统单元文件以使用通过SCL的Python和pip安装的用户pipenv

我试图从根目录执行命令行,但出现以下错误:

[root@localhost ~]# source scl_source enable rh-python36
[root@localhost ~]# /home/user/.local/bin/pipenv run python /home/user/hello.py 
Traceback (most recent call last):
  File "/home/user/.local/bin/pipenv", line 7, in <module>
    from pipenv import cli
ModuleNotFoundError: No module named 'pipenv'

但这句话似乎很尴尬。我可以在systemd单元文件的ExecStart行中使用的正确行是什么?为了使用用户的pipenv,应包括哪些环境变量

这是我的工作系统单位文件:

[Unit]
Description=Python app
# Requirements
Requires=network.target
# Dependency ordering
After=network.target

[Service]
# Let processes take awhile to start up
TimeoutStartSec=0
RestartSec=10
Restart=always
Environment="APP_SITE_SETTINGS=/home/app/.config/settings.cfg"
Environment="PYTHONPATH=/home/app/.local/lib/python3.6/site-packages"
WorkingDirectory=/home/app/app-site

User=app
Group=app
PermissionsStartOnly=true

KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

# Main process
ExecStartPre=/bin/mkdir -p /run/app
ExecStartPre=/bin/chown app:app /run/app
#ExecStartPre=source scl_source enable rh-python36
ExecStart=/usr/bin/scl enable rh-python36 -- /home/app/.local/bin/pipenv run uwsgi \
  --socket 127.0.0.1:6003 \
  --buffer-size 65535 \
  --enable-threads \
  --single-interpreter \
  --threads 1 \
  -L \
  --stats /run/app/uwsgi_stats.socket \
  --lazy-apps \
  --master-fifo /run/stocks/uwsgimasterfifo \
  --processes 1 \
  --harakiri 960 \
  --max-worker-lifetime=21600 \
  --ignore-sigpipe \
  --ignore-write-errors \
  --disable-write-exception \
  --mount /=run:app \
  --manage-script-name

[Install]
WantedBy=multi-user.target

如果您使用
--user
安装
pipenv
,python在默认情况下不会找到它,除非您是该用户。有没有理由不将
pipenv
安装到系统软件包中?或者,您必须显式地将
/home/user/.local/lib/python3.7/site包添加到
PYTHONPATH
中。此处的
--
非常关键,它是bash中选项分隔符的结尾
[Unit]
Description=Python app
# Requirements
Requires=network.target
# Dependency ordering
After=network.target

[Service]
# Let processes take awhile to start up
TimeoutStartSec=0
RestartSec=10
Restart=always
Environment="APP_SITE_SETTINGS=/home/app/.config/settings.cfg"
Environment="PYTHONPATH=/home/app/.local/lib/python3.6/site-packages"
WorkingDirectory=/home/app/app-site

User=app
Group=app
PermissionsStartOnly=true

KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

# Main process
ExecStartPre=/bin/mkdir -p /run/app
ExecStartPre=/bin/chown app:app /run/app
#ExecStartPre=source scl_source enable rh-python36
ExecStart=/usr/bin/scl enable rh-python36 -- /home/app/.local/bin/pipenv run uwsgi \
  --socket 127.0.0.1:6003 \
  --buffer-size 65535 \
  --enable-threads \
  --single-interpreter \
  --threads 1 \
  -L \
  --stats /run/app/uwsgi_stats.socket \
  --lazy-apps \
  --master-fifo /run/stocks/uwsgimasterfifo \
  --processes 1 \
  --harakiri 960 \
  --max-worker-lifetime=21600 \
  --ignore-sigpipe \
  --ignore-write-errors \
  --disable-write-exception \
  --mount /=run:app \
  --manage-script-name

[Install]
WantedBy=multi-user.target