Python Instance

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

At the time of this article's writing, Gandi's Python instance supports Python versions 3.6 and 2.7. To choose, 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: http://www.python.org/dev/peps/pep-0333/#the-application-framework-side

The Vhosts

Unlike PHP instances, adding a site to a Python instance via the Gandi interface does not involve creating a new directory on the instance to store the application source code. There is instead only one “default” vhost, where you can upload and store application code. Also in contrast to other instance types, Python instances support only one application per instance. This application can be used to control the allocation of virtual hosts, allowing you the same scaling of sites and vhosts as the other instance types.

It will still be necessary to add the various sites to the instance in DNS, using the procedure described here.

A simple example:

Directory layout
.
└── wsgi.py

wsgi.py:

    def application(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])
        return ['<!DOCTYPE html><html><meta charset="utf-8"><title>It works',
                '</title><h1>It works!</h1>']

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. This file should then be placed on the Simple Hosting instance, at the root of the 'default' directory to ensure dependencies are installed automatically when you deploy the application. You must use Git to commit and push this file to the instance for it to be used.

For more information, see:

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

For more information on deploying your application with Git, see: your Git access

Python version selection

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 the “gandi.yml” file that is located at the root of your project, via the “python” field:

gandi.yml

python:
  version: 3.6

The above example will force the usage of Python 3.6. 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 instance.

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

Static Files

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

Logs

The standard error output from your application will be sent to the following files on your instance'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 'default' root to do so.

For more information, see:

Examples

An example Flask application

https://github.com/mitsuhiko/flask/tree/master/examples/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"
Last modified: 10/16/2017 at 17:32 by Alexandre L. (Gandi)