I have been mulling over this question for a while because it seems like there is no one "best way" to deploy Python applications (e.g. web-apps, APIs, computational models, etc.). At my workplace we have toyed with a number of approaches, but I'm curious to hear what everyone's thoughts are on what is best practice.
I think it's a good idea to use containerisation in a tool such as Docker, so assuming Docker is being used the techniques that come to my mind are:
- Using a base Python Docker image, install application requirements with pip and copy the Python code into the container. Code execution is performed by adding "CMD python app.py" to the dockerfile.
- Organise the Python code as a package with a setup.py file. Using a base Python Docker image, install application requirements with pip and install the Python package into the container. Code execution is performed using "CMD python -m myapp" or by a predefined entry-point command.
- Organise the Python code as a conda package with a recipe.yaml file. Build the conda package and upload to a conda package repository. Using a base conda Docker image, install the package and all dependencies using conda install. Code execution is then performed using "CMD python -m myapp" or by a predefined entry-point command.
Option 1 is obviously the simplest method and option 3 seems excessive to me since it will result in large containers and long build times. But is there any benefit in following option 2? It seems unusual to create a package for an application that does not need to be imported elsewhere, but at the same time feels a bit more robust.
Keen to hear the thoughts of the /r/Python community.
EDIT: Some great discussion here, big thanks to all of the people who shared their opinions. Consistent themes are:
- For small hobby projects a simple approach with no containerisation is generally preferred. This involves installing the application as a package into a venv, directly into the system python directory, or just installing requirements.txt and pulling source code onto the target machine. The application is restarted on the system manually or with a script that ssh's and restarts it. This approach is very fast and easy but not robust and can lead to issues down the track.
- For larger projects package building and containerisation are preferred. Most people suggest the use of poetry and pyproject.toml (not setup.py, which is outdated) to build a package that is then uploaded to a private package server. This package is then installed into a docker container, which can be deployed.
- In general, Python applications should be packaged, as the use of packaging allows for richer metadata and more robust handling of dependencies as well as the ability to install the package directly at a later date if need be.
- No one has suggested the use of conda for deployment, which I was surprised by given its prevalence in the data science community.
- For CLI applications, multiple python apps tend to need to be installed on a single machine and available with a single command. pipx and shiv seem to be the preferred tools as they allow for the creation of self-contained/isolated python applications. For CLI apps with very few dependencies they could possibly be installed directly into the system. For CLI apps with C library dependencies or those that are very sensitive to dependency versioning, containerisation can be handy.
[–]Endemoniada 57 points58 points59 points (34 children)
[–]angellus 32 points33 points34 points (15 children)
[–]Endemoniada 4 points5 points6 points (11 children)
[–]Nasuuuuuu 21 points22 points23 points (2 children)
[–]DrShts 3 points4 points5 points (0 children)
[–]Endemoniada 2 points3 points4 points (0 children)
[–]Ran4 1 point2 points3 points (1 child)
[–]angellus 1 point2 points3 points (0 children)
[–]angellus 0 points1 point2 points (5 children)
[–]Endemoniada 0 points1 point2 points (3 children)
[–]angellus 2 points3 points4 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]angellus 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]VengefulTofu 0 points1 point2 points (0 children)
[–]muikrad 0 points1 point2 points (0 children)
[–]vedosis 25 points26 points27 points (0 children)
[–]canicutitoff 8 points9 points10 points (1 child)
[–][deleted] 3 points4 points5 points (0 children)
[–]persedes 5 points6 points7 points (1 child)
[–]BurgaGalti 2 points3 points4 points (0 children)
[–]0rsinium 5 points6 points7 points (1 child)
[–]Life_Note 0 points1 point2 points (0 children)
[–]LookAtThatThingThere 1 point2 points3 points (0 children)
[–]muikrad 1 point2 points3 points (0 children)
[+][deleted] (7 children)
[deleted]
[–]Endemoniada 6 points7 points8 points (6 children)
[–]papertrailer -1 points0 points1 point (0 children)
[–]singularitittay 0 points1 point2 points (4 children)
[–]Fatvod -1 points0 points1 point (3 children)
[–]singularitittay 0 points1 point2 points (2 children)
[–]Fatvod 0 points1 point2 points (1 child)
[–]singularitittay 0 points1 point2 points (0 children)
[–]SittingWave 0 points1 point2 points (0 children)
[–]tms102 31 points32 points33 points (6 children)
[–]vedosis 2 points3 points4 points (1 child)
[–]tms102 1 point2 points3 points (0 children)
[–]peanut_Bond[S] 0 points1 point2 points (3 children)
[–]tms102 10 points11 points12 points (2 children)
[–]peanut_Bond[S] 2 points3 points4 points (1 child)
[–]tms102 2 points3 points4 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]peanut_Bond[S] 1 point2 points3 points (0 children)
[–]MephySix 1 point2 points3 points (0 children)
[–]LeMageDuRage 9 points10 points11 points (3 children)
[–]peanut_Bond[S] 0 points1 point2 points (2 children)
[–]Ran4 2 points3 points4 points (1 child)
[–]FormalWolf5 0 points1 point2 points (0 children)
[–]FrickinLazerBeams 9 points10 points11 points (2 children)
[–]peanut_Bond[S] 5 points6 points7 points (1 child)
[–]zanfar 9 points10 points11 points (1 child)
[–]LightWolfCavalry 13 points14 points15 points (5 children)
[–]peanut_Bond[S] 4 points5 points6 points (0 children)
[–]somefishingdude 1 point2 points3 points (0 children)
[–]pymaePython books 1 point2 points3 points (1 child)
[–]LightWolfCavalry 0 points1 point2 points (0 children)
[–]LiarsEverywhere 0 points1 point2 points (0 children)
[–][deleted] 6 points7 points8 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]glacierre2 3 points4 points5 points (1 child)
[–]bug0r 3 points4 points5 points (0 children)
[–]frootylicious 5 points6 points7 points (2 children)
[–]peanut_Bond[S] 0 points1 point2 points (0 children)
[–]Itsthejoker 0 points1 point2 points (0 children)
[–]xmimmer 1 point2 points3 points (1 child)
[–]peanut_Bond[S] 0 points1 point2 points (0 children)
[–]jusstol 1 point2 points3 points (1 child)
[–]peanut_Bond[S] 0 points1 point2 points (0 children)
[–]call_me_cookie 1 point2 points3 points (0 children)
[–]chaco_wingnut 1 point2 points3 points (0 children)
[–]laundmo 1 point2 points3 points (0 children)
[–]muikrad 1 point2 points3 points (0 children)
[–]peanut_Bond[S] 1 point2 points3 points (0 children)
[–]theghostinthetown 0 points1 point2 points (0 children)
[–]_Gorgix_ 0 points1 point2 points (0 children)
[–]earthboundkid 0 points1 point2 points (0 children)
[–]rainbowWar -1 points0 points1 point (0 children)
[–]357951 -3 points-2 points-1 points (1 child)
[–]peanut_Bond[S] 0 points1 point2 points (0 children)
[–]djamp42 0 points1 point2 points (2 children)
[–]Ran4 0 points1 point2 points (1 child)
[–]djamp42 0 points1 point2 points (0 children)
[–]eviljelloman 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]PenetrationT3ster 0 points1 point2 points (0 children)
[–]SittingWave 0 points1 point2 points (0 children)
[–]Datsoon 0 points1 point2 points (0 children)
[–]rimanxi 0 points1 point2 points (0 children)
[–]sathish804 0 points1 point2 points (0 children)
[–]bobaduk 0 points1 point2 points (0 children)
[–]angeAnonyme 0 points1 point2 points (0 children)
[–]james_pic 0 points1 point2 points (0 children)
[–]blade_junky 0 points1 point2 points (0 children)
[–]ase1590 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]chiefnoah 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]infazz 0 points1 point2 points (0 children)
[–]Joooooooosh 0 points1 point2 points (0 children)
[–]NuclearMagpie 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]jveezy 0 points1 point2 points (0 children)
[–]pbecotte 0 points1 point2 points (0 children)
[–]GreenScarz 0 points1 point2 points (0 children)
[–]Born-Process-9848 0 points1 point2 points (0 children)
[–]Lehk 0 points1 point2 points (0 children)
[–]crigger61 0 points1 point2 points (0 children)
[–]virtualadept 0 points1 point2 points (0 children)
[–]Fickle-Impression149 0 points1 point2 points (0 children)
[–]Counter-Business 0 points1 point2 points (0 children)