简体   繁体   中英

How to use user's pipenv via systemd? Python is installed via SCL

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

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

source scl_source enable rh-python36

I have installed pipenv:

pip install --user pipenv

I run Python programs via the command line:

pipenv run python myprogram.py

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?

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'

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM