Python
Back to home
On this page
Note
You can now use composable image (BETA) to install runtimes and tools in your application container. To find out more, see the dedicated documentation page.
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
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
To use Node.js, specify python
as your app’s type:
applications:
# The app's name, which must be unique within the project.
<APP_NAME>:
type: 'python:<VERSION_NUMBER>'
For example:
applications:
# The app's name, which must be unique within the project.
app:
type: 'python:3.9'
Deprecated versions
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
Run your own server
You can define any server to handle requests. Once you have it configured, add the following configuration to get it running on Upsun:
- Specify one of the supported versions:
applications:
# The app's name, which must be unique within the project.
app:
type: 'python:3.9'
- Install the requirements for your app.
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
- Define the command to start your web server:
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
You can also use uWSGI to manage your server. Follow these steps to get your server started.
- Specify one of the supported versions:
applications:
# The app's name, which must be unique within the project.
app:
type: 'python:3.9'
- Define the conditions for your web server:
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
-
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. -
Install the requirements for your app.
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
-
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
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:
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:
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
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
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
All major Python web frameworks can be deployed on Upsun. See dedicated guides for deploying and working with them: