156

I want to "activate" a virtualenv in a systemd service file.

I would like to avoid having 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

PATH={{ venv_home }}/bin:/usr/local/bin:/usr/bin:/bin
PYTHONIOENCODING=utf-8
PYTHONPATH={{ venv_home }}/...
VIRTUAL_ENV={{ venv_home }}

But I am having trouble. I get ImportErrors since some entries in sys.path are missing.

2
  • Can you please include the errors you are getting? May 13, 2016 at 13:39
  • @PraveenYalagandula The traceback does not contain any useful information, since the ImportError Exception and all lines above it contain only custom code which does not matter here.
    – guettli
    May 13, 2016 at 18:08

5 Answers 5

186

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

or

ExecStart={{ venv_home }}/bin/python {{ venv_home }}/fooservice.py --serve-in-foreground

and remove the EnvironmentFile entry.

To verify that it is indeed correct you can check sys.path by running

{{ venv_home }}/bin/python -m site

and comparing the output to

python -m site
17
  • 5
    good point Nils. Btw, fooservice.py doesn't make sense to be inside venv_home directory, I suppose its a typo in the question.
    – stelios
    Jan 29, 2017 at 18:06
  • 5
    Note the suggested print commands are not compatible with Python 3. If you are using at least python 2.4, you can alternatively just use: python -m site to get a nicely formatted output of the sys.path variable along with additional information. Mar 6, 2017 at 21:40
  • 3
    Neat, I didn't know about python -m site. I have adjusted my answer. Mar 7, 2017 at 8:56
  • 2
    This approach doesn't work when the python process you're starting expects the virtualenv to be activated. For example, consider {{ venv_home }}/bin/python -m newrelic.admin run-program python -m myapp. That will launch python in the venv, invoke New Relic, and then it will fail to launch the app because python is not in the path because the venv is not activated. Jan 12, 2018 at 18:51
  • 21
    For those wondering if this is ninja2....no, the double curly-braces are just placeholders the OP invented: superuser.com/questions/1209919/…
    – ankostis
    Jul 6, 2018 at 21:03
33

While the path for libraries is indeed baked into the python interpreter of the virtualenv, I've had issues with python tools that were using binaries installed in that virtualenv. For instance, my apache airflow service wouldn't work because it couldn't find the gunicorn binary. To work around this, here's my ExecStart instruction, with an Environment instruction (which sets an environment variable for the service alone).

ExecStart={{ virtualenv }}/bin/python {{ virtualenv }}/bin/airflow webserver
Environment="PATH={{ virtualenv }}/bin:{{ ansible_env.PATH }}"

ExecStartexplicitly uses the python interpreter of the virtualenv. I'm also adding a PATH variable, which adds the binary folder of the virtualenv before the system PATH. That way, I get the desired python libraries as well as binaries.

Note that I'm using ansible to build this service, ergo the curly braces of jinja2.

2
  • 2
    Literally came looking for how to set the environment in a service because airflow couldn't find gunicorn. Was NOT disappointed! Thank you! I'm up and running.
    – pyFiddler
    Jun 17, 2021 at 19:18
  • Exactly , I was in need of Airflow automation through Ansible . Thank you so much :)
    – Manju N
    Jul 7, 2021 at 6:42
6

In my case I just tried to add environment variables required for Flask, for instance

[Service]
Environment="PATH=/xx/yy/zz/venv/bin"
Environment="FLASK_ENV=development"
Environment="APP_SETTINGS=config.DevelopmentConfig"

I was using virtualenv so /xx/yy/zz/venv/bin is the path of virtualenv folder.

3

I'm not using virtualenv but pyenv: here is it just to use the real .pyenv path in the shebang and make sure it is in the PATH

Ex: pyenv activate flask-prod for user mortenb which is running in prod

/home/mortenb/.pyenv/versions/flask-prod/bin/python --version
Python 3.6.2

Then to my flask scripts starting in systemd *.service I add the following shebang:

#!/home/mortenb/.pyenv/versions/flask-prod/bin/python3
0
ExecStart=cd /root/app/working-directory && poetry run python my_app.py
1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 30 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.