Python

Python Language Recognition Strategy

By default, the platform will recognize it as a Python project based on whether there is a requirements.txt file in the source root directory.

Platform Compilation and Operation Mechanism

  1. The pre-compilation process will detect whether the startup command configuration file Procfile is defined, and if not defined, the default Flask/Django startup configuration file will be generated;
  2. After the pre-compilation process is completed, the Python buildpack will be selected according to the language type to compile the project. The defined Python version and related Python dependencies will be installed during the compilation process;
  3. After the compilation is completed, it will be checked whether the Procfile parameter is set on the platform, and if it is configured, the startup command configuration file Procfile will be overwritten.

Python Project Source Code Specification

In this step, you need to provide a usable Python source code program for deployment on the Kato platform. This application must at least meet the following conditions:

  • The deployed Python program can be run normally locally
  • The project can be hosted in a git repository
  • requirements.txt must exist in the project root directory, which is used to manage the dependencies of the Python project and is also a necessary condition for Kato to recognize the Python language
  • Procfile needs to be defined in the root directory of the project to define the way to start the program
  • There is runtime.txt in the project root directory, which is used to define the Python version of the current project
requirements.txt Specification

If the program has no dependencies, make requirements.txt an empty file.

If there is no requirements.txt, the following command can be used to generate

pip freeze > requirements.txt
Procfile Specification

If the project does not define a Procfile file, the platform will generate a default Procfile to run the War package by default.

web: gunicorn app:app --log-file - --access-logfile - --error-logfile -

The above is the default Procfile, if you need to expand more startup parameters, you can customize the Procfile.

  1. There is a space between web: and gunicorn
  2. The end of the file cannot contain special characters

Compile and Run Environment Settings

Configure Python Version

It is recommended to use runtime.txt to define the Python version. If not defined, Kato will use the python-3.6.6 version by default.

$ cat runtime.txt
python-3.6.6

Recommended python version

  • Python-2.7.15
  • Python-3.6.6

The python version is supported by default

# python 2.7.x
python-2.7.9 python-2.7.10 python-2.7.13 python-2.7.14 python-2.7.15 
# python 3.x
python-3.4.3
python-3.5.3 
python-3.6.0 python-3.6.1 python-3.6.2 python-3.6.3 python-3.6.4 python-3.6.5 python-3.6.6 
python-3.7.0

Advanced Build Options

Enable advanced build features in build advanced settings or build sources

Environment VariablesDefault ValueDescription
BUILD_PIP_INDEX_URLhttps://pypi.tuna.tsinghua.edu.cn/simplePypi source

Django Static File Support

Since the static file support (CSS, images, etc.) of Django is not easy to configure and difficult to debug, here is an example:

settings.py

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

By default, the system will automatically execute the following command when building a Django application to try to check (–dry-run) whether the static file configuration is correct:

$ python manage.py collectstatic --dry-run --noinput

If there is no error in this command, the real command will be executed to copy the static files to the STATIC_ROOT directory:

$ python manage.py collectstatic --noinput

Users can manually disable the above features, only need to configure the value of BUILD_DISABLE_COLLECTSTATIC to 1 in the application environment variables.

Sample Demo Program

Example https://github.com/gridworkz/python-demo