Best practices for server: virtual environment?

I’m running an application on Flask with WSGI. Previously, I would install packages on my Debian-based servers without a virtual environment.

But now, given externally managed environments and the push to venv, I think that might not be the way to go. Use of --break-system-packages seems like a bad idea in the long run.

I was wondering: are there any established best practices for installing and running Python packages on a server? I’ve done a lot of searching on the topic and have found nothing so far. What first comes to my mind is to run the application server inside some kind of “production” virtual environment.

I would recommend always using virtual environments. I liked Hynek’s guide for deploying with Docker

1 Like

My recommendation is to always use a Python virtual environment, unless there is a reason that prevents you from doing it.

Installing without virtual environment can cause issues. In particular on Debian. On Debian (and derivatives such as Ubuntu) some Python packages are (pre)-installed with apt. By installing Python packages with pip and without virtual environment you run the risk of overwriting those apt-installed packages. Those apt-installed packages might be pre-installed because they are essential to the proper functioning of the system. In short: by installing Python packages without virtual environment you run a high risk of breaking your system.

1 Like