Some tips for organizing a Python application on a

Some tips for organizing a Python application on a
In this article, I want to share several convenient ways to organize your project on a production (even production) server.
I'm working mostly with the Python /Django stack, so all the examples will be, first of all, applied to this set. Also key technologies: Ubuntu (???), Python3 (3.6).
Demons (systemd)
local settings
script for mass management of the entire economy on the server.
bash migrate
bash start


For remote debugging via the console (run shell_plus from django-extensions):

    bash debug    


If you have more than one server, then, of course, you are already using your own scripts to deploy, this is just an example.


Local settings


This is a topic for holivar and I want to note that this paragraph is purely my point of view, which is based on my experience. Do whatever you want, I just advise.


The problem is that no matter how you write the application, it will most likely have to deal with data that is stored somewhere. List of users, the path to the keys, passwords, the path to the database, and so on All these settings can not be stored in the repository. Someone stores, but this is a bad practice. Unsafe and inflexible.


What are the most common ways of storing such settings and what are the problems with them:

  • file, which is stored in the project folder next to the default
    The problem: you can accidentally zakommitit file, you can wipe it when copying /updating the folder (rsync or from the archive)  
  • environment variables. Also not very safe, not very convenient (with soft-reload for example)  
  • A separate file outside the project folder  


I recommend this method. Usually I create a yaml file for the project in the "/usr /local /etc /" folder. I have written small module , which is using magic khaki loads variables from the file into locals () or globals () importing module.


It is used very simply. Somewhere in the depths of for Django (better near the end) it's enough to call:

    import_settings ("/usr /local /etc /myservice.yaml")  
And all the content will be implicated in the global settings. I use merge for lists and dictionaries, it may not be all convenient. It's important to remember that Django imports only UPPERCASE constants, that is, in the file of the first level configuration, you should immediately be in uppercase.
That's all folks!
The rest will be discussed in the comments.
+ +5 -

Add comment