How to enable a virtualenv in a systemd service unit?

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

Leave a Comment