How to Use Python on a Gandi Web Hosting

A Python web hosting enables you to publish web applications written in Python easily and affordably.

Prerequisites

  • Familiarity with the Python coding language and WSGI protocol

  • Your application must have a WSGI entry point

  • You must be familiar with Git

Version Support

When creating a new Python web hosting, you currently have four options :

  • 2.7 (includes 3.2 support)

  • 3.6

  • 3.7

  • 3.8

If you’ve selected a Python 2.7 web hosting, to switch between 2.7 and 3.2 in order to run your application, see “Python Version Selection” below.

Basic Use

Your Python app must contain at least one file named wsgi.py, which itself must contain the application object, in accordance with the PEP agreement specified here: https://www.python.org/dev/peps/pep-0333/#the-application-framework-side

Domains

A domain name is made available for testing purposes when the web hosting is created. You can link a number of domain names and subdomains. All will point to the same application code base.

Example

Directory layout

.
└── wsgi.py

wsgi.py:

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    body = ['<!DOCTYPE html><html><meta charset="utf-8">',
            '<title>It works!</title><h1>It works!</h1>']
    return [line.encode('utf-8') for line in body]

Application Dependencies

Your application’s dependencies have to be installable with pip, and must be specified in a file named requirements.txt.

For the dependencies to be automatically installed, you need to deploy the code with Git.

As you develop your application on a local machine, install the dependencies with pip install. Then, when your application is ready to go into production, run the following command locally to generate a file containing all the dependencies:

$ pip freeze > requirements.txt

This command will populate the requirements.txt file with modules and dependencies based on those you have installed on your development environment. You must use Git to commit and push this file to the web hosting for it to be used. The dependencies will be installed when you deploy the application with SSH.

For more information, see:

https://www.pip-installer.org/en/latest/cookbook.html#requirements-files

For more information on deploying your application with git+SSH, checkout out git guide.

Python Version Selection

Note

This section only applies to Python 2.7 / 3.2 web hostings.

It is possible to choose the version of Python that you want to use to execute your application from the available versions we offer. To do this, you will need to specify the Python version that you want to use in a gandi.yml file at the root of your project, in the python field:

gandi.yml

python:
  version: 3.2

The above example will force the usage of Python 3.2. If the file exists but there is no python field, or in the event that the yaml format is invalid, the default version will be used. If an invalid value is specified, your application will not boot and an error message will be displayed in the deployment logs of your application and in the boot logs of your web hosting.

Note

This file, as every other files composing your project, needs to be tracked by Git and pushed to your Web Hosting remote repository before issuing the deploy command.

Static Files

Place all of your static content in directories named /static/ or /media/, so that the Apache HTTPD server can pick them up.

Logs

The standard error output from your application will be written to the following files on your web hosting’s disk:

  • from the SSH console: /srv/data/var/log/www/uwsgi.log

  • over SFTP: /lamp0/var/log/www/uwsgi.log

You can use these files to monitor the successful start of your application.

You also have the option of configuring the logger to fit your needs. Add a file called logging.ini to the project’s root to do so.

For more information, see:

Examples

An example Flask application

https://github.com/crito/minitwit

Directory layout

.
├── minitwit.py
├── requirements.txt
├── static/
├── templates/
└── wsgi.py

wsgi.py:

from minitwit import app as application

requirements.txt:

flask

A Django Example

Directory layout

.
├── media/
├── cms/
│   ├── manage.py
│   └── cms/
│       ├── init.py
│       ├── settings.py
│       ├── templates/
│       └── urls.py
├── requirements.txt
├── static/
└── wsgi.py

wsgi.py:

import sys
import os
import os.path

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__),
                                                'cms')))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cms.settings')
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

requirements.txt:

django-cms

Character Encoding

To enable special character support in uwsgi, for example within filenames, add the following statements to the uwsgi.py file:

os.environ['LC_ALL']="en_US.UTF-8"
os.environ['LC_LANG']="en_US.UTF-8"