Deploying Python Poetry project with SystemD

Posted on : 29th Oct 2021

What is Poetry ?

An awesome dependency management tool for python programming language. Which also makes it quite easier to manage the virtual environment for your python project. You can read more about Poetry and its installation here.

How do you deploy a Python project anyway ?

Glad you asked! If you have a python project that you want to run as a background service or daemon. You usually either use a  service manager like systemd or you can use a terminal multiplexer like tmux or screen.

Read more on the topic here : How to run a python program as a background service ?

Deploy Python poetry project ?

Here comes the good stuff. We will use systemd service manager to create background service for our python app. For our example we are trying to deploy a Flask app that uses poetry to manage dependencies and the virtual environment. Follow along, comrade :

  1. Activate the poetry shell, and get the path of the python executable that poetry is using. `which python`. When a virtual environment is being used, you will get an isolated copy of dependencies and python interpreter for your project. To execute your python code via that interpreter, we will need the absolute path for the interpreter in use by poetry.
  2. Once you have the absolute python interpreter path with you. Create a systemd service configuration. As follows.
  3. And that is how you deploy a flask app(or any python project) that uses poetry for its dependency and environment management.

Ending notes

Yes! I know it was simple. The approach is pretty much the same across all the background services you will ever make with systemd. Just find out the absolute executable path that will run your code with all dependencies. And that's pretty much it.

© 2021, All Rights Reserved · Vipin Joshi