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. Nginx then stays in front of your command, so it must listen on port 9000, even without Request Flow. If your command reads PORT, set 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.10 | |
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 CC_PYTHON_SETUP_PY_GOAL="<your goal>".
Django manage.py tasks
Clever Cloud runs manage.py tasks during the build, after pip installs the dependencies from requirements.txt and before the web server starts. Declare them with the CC_PYTHON_MANAGE_TASKS environment variable, as a comma-separated list. Each entry becomes a python manage.py call, so it can carry its own options:
CC_PYTHON_MANAGE_TASKS="migrate, collectstatic --noinput"A task exiting with an error stops the build.
Note
These tasks belong to the legacy deployment mode. Native uv deployments don’t run them.
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.