Using Pipenv with Systemd

By · Published · python, systemd, pipenv

So I've been doing a bit of Python recently for a project I'm working on on a Raspberry Pi. There will be a longer blog post about that in the next few weeks. But one thing I ran up against was that I wanted to start my daemon, written in Python, using a systemd service on Raspbian.

Normally, you would just shove a script invocation into a systemd unit and call it good, but in my case I had made use of Pipenv, which is a bit like Bundler in the Ruby world and Composer in the PHP world, to manage my project's dependencies.

And the way you invoke a program using Pipenv is different from just invoking the script:

$ pipenv run python3 src/

So you might try something like this in a systemd unit:

ExecStart=/usr/local/bin/pipenv run python3 /path/to/src/

But that doesn't work. This is because Pipenv relies on the current working directory to look for the Pipfile. Fortunately, with systemd, you can specify the current working directory in the unit file using the WorkingDirectory directive. So your systemd unit file might look something like this:

Description=My Python Service

ExecStart=/usr/local/bin/pipenv run python3 /path/to/src/

( Comments )

Did something I wrote help you out?

That's great! I don't earn any money from this site - I run no ads, sell no products and participate in no affiliate programs. I do this solely because it's fun; I enjoy writing and sharing what I learn.

All the same, if you found this article helpful and want to show your appreciation, here's my wishlist.

Related Posts

petfeedd 0.2 released, with Docker support!

Rob's Raspberry Pi Powered Pet Feeders

comments powered by Disqus