本文介绍了如何通过systemd使用用户的pipenv?Python 通过 SCL 安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 CentOS 7 服务器上,我通过 SCL 安装了 Python 3.6.( https://www.softwarecollections.org/en/scls/rhscl/rh-python36/)

On a CentOS 7 server, I have installed Python 3.6 via SCL. ( https://www.softwarecollections.org/en/scls/rhscl/rh-python36/)

我在 .bashrc 中有这一行来启用 SCL 的 Python 3.6

I have this line in .bashrc to enable SCL's Python 3.6

source scl_source enable rh-python36

我已经安装了 pipenv:

I have installed pipenv:

pip install --user pipenv

我通过命令行运行 Python 程序:

I run Python programs via the command line:

pipenv run python myprogram.py

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

All these work great. I have a Flask application that uses the user's pipenv. I am trying to create a systemd unit file to start/stop/reload the Flask web application. How can I get the sytemd unit file to use the user's pipenv installed via SCL's Python and pip?

我尝试从 root 执行命令行,但出现此错误:

I tried to execute the command line from root and I get this error:

[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'

但是,我可以通过加载用户的 bash shell 来通过 su -c 执行命令:

However, I am able to execute the command via su -c by loading user's bash shell:

 su -c 'bash -lc /home/user/.local/bin/pipenv run python hello.py' user

但这行看起来很尴尬.我可以在 systemd 单元文件的 ExecStart 行中使用的正确行是什么?为了使用用户的pipenv,应该包含哪些环境变量?

But this line seems awkward. What is a the correct line I could use in systemd unit file's ExecStart line? What environment variables should be included in order to use the user's pipenv?

推荐答案

这是我工作的 systemd 单元文件:

Here's my working systemd unit file:

[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

这篇关于如何通过systemd使用用户的pipenv?Python 通过 SCL 安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-12 00:15