问题描述
我想激活" systemd服务文件中的virtualenv.
I want to "activate" a virtualenv in a systemd service file.
我希望避免在systemd进程和python解释器之间使用shell进程.
I would like avoid to have a shell process between the systemd process and the python interpreter.
我当前的解决方案如下:
My current solution looks like this:
[Unit]
Description=fooservice
After=syslog.target network.target
[Service]
Type=simple
User=fooservice
WorkingDirectory={{ venv_home }}
ExecStart={{ venv_home }}/fooservice --serve-in-foreground
Restart=on-abort
EnvironmentFile=/etc/sysconfig/fooservice.env
[Install]
WantedBy=multi-user.target
/etc/sysconfig/fooservice.env
/etc/sysconfig/fooservice.env
PATH={{ venv_home }}/bin:/usr/local/bin:/usr/bin:/bin
PYTHONIOENCODING=utf-8
PYTHONPATH={{ venv_home }}/...
VIRTUAL_ENV={{ venv_home }}
但是我遇到了麻烦.由于缺少sys.path中的某些实体,因此出现ImportErrors.
But I am having trouble. I get ImportErrors since some enties in sys.path are missing.
推荐答案
virtualenv被烘焙到virtualenv中的Python解释器中".这意味着您可以启动python
或 console_scripts
直接在该virtualenv中,不需要先激活virtualenv或自己管理PATH
.
The virtualenv is "baked into the Python interpreter in the virtualenv". This means you can launch python
or console_scripts
directly in that virtualenv and don't need to activate the virtualenv first or manage PATH
yourself.:
ExecStart={{ venv_home }}/bin/fooservice --serve-in-foreground
或
ExecStart={{ venv_home }}/bin/python {{ venv_home }}/fooservice.py --serve-in-foreground
并删除EnvironmentFile
条目.
要确认它确实正确,可以通过运行检查sys.path
To verify that it is indeed correct you can check sys.path
by running
{{ venv_home }}/bin/python -m site
并将输出与
python -m site
这篇关于如何在systemd服务单元中启用virtualenv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!