Python application runtime
Overview
Python is a programming language that lets you work more quickly and integrate your systems more efficiently.
Create your Python application
To create a new Python application, use the Clever Cloud Console or Clever Tools:
clever create --type pythonConfigure your Python application
Mandatory needs
Python apps can be launched in a variety of ways. To select which module you want to start, use the CC_PYTHON_MODULE environment variable.
CC_PYTHON_MODULE="mymodule:app"The module (without .py) must be importable, that is, be in PYTHONPATH. You should point to a WSGI-capable object.
For example, with Flask, this is the name of your main server file, followed by your Flask object: server:app, if you have a server.py file at the root of your project with a Flask app object inside.
You can also use CC_RUN_COMMAND to launch a Python application with a custom command. In such case, it must listen on port 9000.
Python version
The default version of Python on Clever Cloud is the latest supported from branch 3.x. If you want to use Python 2.x, set CC_PYTHON_VERSION to 2, it will default to Python 2.7. Other supported values are:
| Default Version | Accepted Versions |
|---|---|
3 | 2 |
3 | |
3.9 (EOL) | |
3.1 | |
3.11 | |
3.12 | |
3.13 | |
3.14 |
The end-of-life (EOL) versions are marked as such.
Build phase
Dependencies
If you do not have a requirements.txt file to commit you can obtain it via the command pip freeze > requirements.txt (or pip3 freeze > requirements.txt if you use Python 3.x) at the root of your project folder in your terminal.
For example, if you want to install PostgreSQL without using the pip freeze command above, create a requirements.txt file at the root of your application folder:
psycopg2>=2.7 --no-binary psycopg2Note
Using psycopg2>=2.7 --no-binary psycopg2 is recommended to avoid WSGI issues.
You can define a custom requirements.txt file with the environment variable CC_PIP_REQUIREMENTS_FILE for example: CC_PIP_REQUIREMENTS_FILE=config/production.txt.
Cached dependencies
Enabling dependencies caching
You can enable dependencies caching by adding the CC_CACHE_DEPENDENCIES=true environment variable in your application. It is enabled by default only for rust and haskell applications.
Disabling dependencies caching
You can disable dependencies caching completely by removing the CC_CACHE_DEPENDENCIES environment variable from the Clever Cloud console, in the Environment variables menu of your application.
Or by setting it to CC_CACHE_DEPENDENCIES=false
To fully remove cached dependencies, you have to rebuild your application from scratch.
You can select “rebuild and restart” from the Clever Cloud console or launch clever restart --without-cache with the Clever Tools CLI.
Use setup.py
Execution of a single setup.py goal is supported. Usually, this would be to execute custom tasks after the installation of dependencies.
The goal will be launched after the dependencies from requirements.txt have been installed.
To execute a goal, define the environment variable PYTHON_SETUP_PY_GOAL="<your goal>".
Select the Python backend
Currently, daphne, gunicorn, uvicorn and uwsgi are supported for Python backends. If not specified, the default backend is uwsgi.
To select one, set the CC_PYTHON_BACKEND environment variable with either daphne, gunicorn, uvicorn or uwsgi.
Contact the support if you need another backend.
Note
Backend selection only applies to the legacy deployment mode (without uv.lock). Native uv deployments manage their own HTTP server.
Using the Gevent loop engine
Whether you use uwsgi or gunicorn, you can enable the Gevent loop engine.
To do so, add the CC_PYTHON_USE_GEVENT environment variable to your application, with the true value.
Celery apps
Celery apps are supported out of the box. To deploy a celery app, use the CC_PYTHON_CELERY_MODULE environment variable:
CC_PYTHON_CELERY_MODULE="mymodule"Warning
Celery needs to be defined as a dependency in your requirements.txt. Otherwise the deployment will be aborted if Celery support is enabled.
You can also activate beat with CC_PYTHON_CELERY_USE_BEAT=true and provide a given log dir for celery with CC_PYTHON_CELERY_LOGFILE="/path/to/logdir".
The CC_PYTHON_CELERY_LOGFILE path is relative to the application’s path.
Warning
There is a bug in versions <4.2 of Celery. You need to add the CELERY_TIMEZONE = 'UTC' environment variable. The bug is documented here: https://github.com/celery/celery/issues/4184.
Monitor your application with New Relic
You can use New Relic to monitor your application on Clever Cloud.
Please refer to our New Relic documentation to configure it for your application.
Enable health check during deployment
The healthcheck allows you to limit downtimes. Indeed, you can provide Clever Cloud with paths to check. If these paths return something other than 200, the deployment will fail.
Add one (or several) environment variable as such:
CC_HEALTH_CHECK_PATH=/my/awesome/pathOr
CC_HEALTH_CHECK_PATH_0=/my/awesome/path
CC_HEALTH_CHECK_PATH_1=/my/other/pathThe deployment process checks all paths. All of them must reply with a 200 OK response code.
By default, when no environment variable (for ex: APP_HOME) is defined, the monitoring checks your repository root path /.
Example
Using the path listed above, below are the expected logs:
Response from GET /my/awesome/path is 200
Response from GET /my/other/path is 500
Health check failed:
- GET /my/other/path returned 500.
If the deployment fails after this message, please update your configuration and redeploy.In this example, the first path is OK, but the second one failed. This gives you a hint on what failed in your application.
Best practice for healthcheck endpoints
To make the most of a healthcheck endpoint, have it check your critical dependencies. For example:
- execute
SELECT 1 + 1;on your database - retrieve a specific Cellar file
- ping a specific IP through a VPN
Request Flow: Varnish, Redirection.io, custom proxy
Request Flow automatically chains reverse proxies between port 8080 (public) and your application, managing port allocation with no manual configuration. Supported services are activated by their presence in your project:
- Otoroshi Challenge: set
OTOROSHI_CHALLENGE_SECRET - Varnish: add a
clevercloud/varnish.vclfile or setCC_VARNISH_FILE - Redirection.io: set
CC_REDIRECTIONIO_PROJECT_KEY
All three can be active simultaneously. To control the order, set CC_REQUEST_FLOW (e.g. redirectionio,varnish). To add a custom middleware, include custom in the chain and define CC_REQUEST_FLOW_CUSTOM with @@LISTEN_PORT@@ and @@FORWARD_PORT@@ placeholders. To block public access, set CC_REQUEST_FLOW=block.
When at least one middleware is active, your application must listen on port 9000 instead of 8080.