Upsun User Documentation

Python

Try Upsun for 15 days
After that, enjoy the same, game-changing Upsun features for less with the First Project Incentive!¹ A monthly $19 perk!
¹Terms and conditions apply
Activate your 15-day trial

Python is a general purpose scripting language often used in web development. You can deploy Python apps on Upsun using a server or a project such as uWSGI.

Supported versions Anchor to this heading

You can select the major and minor version.

Patch versions are applied periodically for bug fixes and the like. When you deploy your app, you always get the latest available patches.

  • 3.12
  • 3.11
  • 3.10
  • 3.9
  • 3.8

Specify the language Anchor to this heading

To use Node.js, specify python as your app’s type:

.upsun/config.yaml
applications:
    # The app's name, which must be unique within the project.
    <APP_NAME>:
        type: 'python:<VERSION_NUMBER>'

For example:

.upsun/config.yaml
applications:
    # The app's name, which must be unique within the project.
    app:
        type: 'python:3.9'

Deprecated versions Anchor to this heading

The following versions are deprecated. They’re available, but they aren’t receiving security updates from upstream and aren’t guaranteed to work. They’ll be removed in the future, so migrate to one of the supported versions.

  • 3.7
  • 3.6
  • 3.5
  • 2.7*

* This version doesn’t receive any updates at all. You are strongly recommended to upgrade to a supported version.

Usage example Anchor to this heading

Run your own server Anchor to this heading

You can define any server to handle requests. Once you have it configured, add the following configuration to get it running on Upsun:

  1. Specify one of the supported versions:
.upsun/config.yaml
applications:
    # The app's name, which must be unique within the project.
    app:
        type: 'python:3.9'
  1. Install the requirements for your app.
.upsun/config.yaml
applications:
    # The app's name, which must be unique within the project.
    app:
        type: 'python:3.9'
        dependencies:
            python3:
                pipenv: "2022.12.19"
        hooks:
            build: |
                set -eu
                pipenv install --system --deploy                
  1. Define the command to start your web server:
.upsun/config.yaml
applications:
    # The app's name, which must be unique within the project.
    app:
        type: 'python:3.9'
        web:
            # Start your app with the configuration you define
            # You can replace the file location with your location
            commands:
                start: python server.py

You can choose from many web servers such as Daphne, Gunicorn, Hypercorn, and Uvicorn. See more about running Python web servers.

Use uWSGI Anchor to this heading

You can also use uWSGI to manage your server. Follow these steps to get your server started.

  1. Specify one of the supported versions:
.upsun/config.yaml
applications:
    # The app's name, which must be unique within the project.
    app:
        type: 'python:3.9'
  1. Define the conditions for your web server:
.upsun/config.yaml
applications:
    # The app's name, which must be unique within the project.
    app:
        type: 'python:3.9'
        web:
            upstream:
                # Send requests to the app server through a unix socket
                # Its location is defined in the SOCKET environment variable
                socket_family: "unix"

            # Start your app with the configuration you define
            # You can replace the file location with your location
            commands:
                start: "uwsgi --ini conf/uwsgi.ini"

            locations:
                # The folder from which to serve static assets
                "/":
                    root: "public"
                    passthru: true
                    expires: 1h
  1. Create configuration for uWSGI such as the following:

    config/uwsgi.ini
    [uwsgi]
    # Unix socket to use to talk with the web server
    # Uses the variable defined in the configuration in step 2
    socket = $(SOCKET)
    protocol = http
    
    # the entry point to your app
    wsgi-file = app.py

    Replace app.py with whatever your file is.

  2. Install the requirements for your app.

.upsun/config.yaml
applications:
    # The app's name, which must be unique within the project.
    app:
        type: 'python:3.9'
        dependencies:
            python3:
                pipenv: "2022.12.19"
        hooks:
            build: |
                set -eu
                pipenv install --system --deploy                
  1. Define the entry point in your app:

    # You can name the function differently and pass the new name as a flag
    # start: "uwsgi --ini conf/uwsgi.ini --callable <NAME>"
    def application(env, start_response):
    
        start_response('200 OK', [('Content-Type', 'text/html')])
        return [b"Hello world from Upsun"]

Package management Anchor to this heading

Your app container comes with pip pre-installed. For more about managing packages with pip, Pipenv, and Poetry, see how to manage dependencies.

To add global dependencies (packages available as commands), add them to the dependencies in your app configuration:

.upsun/config.yaml
applications:
    # The app's name, which must be unique within the project.
    app:
        type: 'python:3.9'
        dependencies:
            python3:
                PACKAGE_NAME: PACKAGE_VERSION

For example, to use pipenv to manage requirements and a virtual environment, add the following:

.upsun/config.yaml
applications:
    # The app's name, which must be unique within the project.
    app:
        type: 'python:3.9'
        dependencies:
            python3:
                pipenv: "2022.12.19"
        hooks:
            build: |
                set -eu
                pipenv install --system --deploy                

Connect to services Anchor to this heading

The following examples show how to access various services with Python. For more information on configuring a given service, see the page for that service.

You can access service credentials to connect to managed services from environment variables present in the application container. Consult each of the individual service documentation to see how to retrieve and surface credentials into your application.

Sanitizing data Anchor to this heading

By default, data is inherited automatically by each child environment from its parent. If you need to sanitize data in preview environments for compliance, see how to sanitize databases.

Frameworks Anchor to this heading

All major Python web frameworks can be deployed on Upsun. See dedicated guides for deploying and working with them:

Is this page helpful?