Run a Python App

Getting an application running on is essentially working out how to package it as a deployable image. Once packaged it can be deployed to the global application platform.

In this guide we'll learn how to deploy a Python application on

The Hellofly-python Application

You can get the code for the example from the Fly-Examples GitHub repository. Just git clone to get a local copy.

The Python hellofly application is, as you'd expect for an example, very simple. It's a Python application that uses the Flask web framework. Here's all the code form

from flask import Flask, render_template

app = Flask(__name__)

def hello(name=None):
    return render_template('hello.html', name=name)

Flask is set up to route request to a hello function which in turn passes a name value (taken from the requests path)to a function to render a template. The template resides in the templates directory under the name hello.html. It too is very simple:

<!DOCTYPE html>
<html lang="en">
<h1>Hello from Fly</h1>
{% if name %}
<h2>and hello to {{name}}</h2>
{% endif %}

We're using a template as it makes it easier to show what you should do with assets that aren't the actual application.

You will need to install Flask itself, or at least set up virtual environments as recommended in the Flask Install guide.

Once you have activated the virtual environment, run:

python -m pip install -r requirements.txt

This will load Flask and other required packages. One of those packages will be gunicorn which isn't a Flask dependency, but will be used when we deploy the app to Fly.

Testing the Application

Flask apps are run with the flask run command, but before you do that, you need to set an environment variable FLASK_APP to say which app you want to run.

FLASK_APP=hellofly flask run
 * Serving Flask app "hellofly"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on (Press CTRL+C to quit)

This will run our hellofly app and you should be able to connect to it locally on localhost:5000.

Now, let's move on to deploying this app to Fly.

Install Flyctl and Login

We are ready to start working with Fly and that means we need flyctl, our CLI app for managing apps on Fly. If you've already installed it, carry on. If not, hop over to our installation guide. Once that is installed you'll want to log in to Fly.

Configure the App for Fly

Each Fly application needs a fly.toml file to tell the system how we'd like to deploy it. That file can be automatically generated with the flyctl launch command. We are going to use one of Fly's builtin deployment configurations for Python.

flyctl launch
? Select organization: demo (demo)
Wrote config file fly.toml

Organizations are a way of sharing applications between Fly users. When you are asked to select an organization, there should be one with your account name; this is your personal organization. Select that.

One thing to know about the builtin Python builder is that it will automatically copy over the contents of the directory to the deployable image. This is how you can move static assets such as templates and other files to your application. The other thing to know is that it uses a Procfile to run the application; Procfiles are used on other platforms to deploy Python applications so we keep it simple. The Procfile contains instructions for starting the application. Here's the contents of ours:

web: gunicorn hellofly:app

This says the web component of the application is served by gunicorn (which we mentioned earlier when talking about dependencies) and that should run the hellofly Flask app as we set up for Flask.

Inside fly.toml

The fly.toml file now contains a default configuration for deploying your app. In the process of creating that file, flyctl has also created a Fly-side application slot of the same name, hellofly. If we look at the fly.toml file we can see the name in there:

# fly.toml file generated for hellofly-python on 2021-01-13T15:21:15Z

app = "hellofly-python"

kill_signal = "SIGINT"
kill_timeout = 5

  internal_port = 8080
  protocol = "tcp"

    hard_limit = 25
    soft_limit = 20

    handlers = ["http"]
    port = "80"

    handlers = ["tls", "http"]
    port = "443"

    interval = 10000
    timeout = 2000

The flyctl command will always refer to this file in the current directory if it exists, specifically for the app name/value at the start. That name will be used to identify the application to the Fly service. The rest of the file contains settings to be applied to the application when it deploys.

We'll have more details about these properties as we progress, but for now, it's enough to say that they mostly configure which ports the application will be visible on.

Deploying to Fly

We are now ready to deploy our app to the Fly platform. At the command line, just run:

flyctl deploy

This will lookup our fly.toml file, and get the app name hellofly-python from there. Then flyctl will start the process of deploying our application to the Fly platform. Flyctl will return you to the command line when it's done.

NOTE: When you are prompted to overwrite the Procfile, choose 'no' for this example.

Viewing the Deployed App

Now the application has been deployed, let's find out more about its deployment. The command flyctl status will give you all the essential details.

flyctl status
  Name     = hellofly-python
  Owner    = demo
  Version  = 0
  Status   = running
  Hostname =

Deployment Status
  ID          = 0cdc72fe-3db9-aa52-eb84-5c3552053b1e
  Version     = v0
  Status      = successful
  Description = Deployment completed successfully
  Instances   = 1 desired, 1 placed, 1 healthy, 0 unhealthy

0530d622 0       lhr    run     running 1 total, 1 passing 0        40s ago

As you can see, the application has been assigned with a DNS hostname of, and an instance is running in London. Your deployment's name will, of course, be different.

Connecting to the App

The quickest way to connect to your deployed app is with the flyctl open command. This will open a browser on the HTTP version of the site. That will automatically be upgraded to an HTTPS secured connection (for the domain).

To specify a path, add /name to flyctl open and it'll be appended to the URL as the path and you'll get an extra greeting from the hellofly-python application.

Bonus Points

If you want to know what IP addresses the app is using, try flyctl ips list:

fly ips list
TYPE ADDRESS                             CREATED AT
v4                      2m1s ago
v6   2a09:8280:1:502e:4ce8:6058:7f09:62a 1m58s ago

And you can always run flyctl as fly if you want to save a few keystrokes.

Arrived at Destination

You have successfully built, deployed, and connected to your first Python application on Fly.