Knowledgebase: Python
Create a Python 'Hello World' App with Flask
Posted by ASO Admin on 11 February 2020 08:17 AM

Note: this tutorial is focused on Shared hosting environments only.

Preparing the Python Environment

The first thing we need to do is set up a local Python environment. For this we'll be using VirtualEnv, which is commonly used to isolate Python environments. This tutorial will require SSH access to your account, which is covered <here>.

cd ~

Next we'll initialize our local python environment under the "pyenv" directory, relative to your home directory (/home/USER/pyenv). At this time we're also selecting which python version we're going to be using for our environment, in the below example, we're choosing to use the server's default python.

virtualenv --python=python2.6 pyenv
source pyenv/bin/activate

After that last command, our prompt should now look a little different indicating we've entered our python environment. Now let's install Flask:

pip install flask

Create Your First Flask App

Just in case you're revisiting this tutorial, please ensure we're still in or if not, change to your account's home directory:

cd ~

Now that our environment has been set up to support Flask, we can create our first application using the Flask framework. For this tutorial we're going to create a standard Hello World application. First, let's create our application's directory:

mkdir helloworld

And now change to our application's directory:

cd helloworld

Now we need to create our basic application directory layout, which consists of three sub directories:

mkdir public
mkdir tmp
mkdir app

Now, let's create our main application file:

vi app/

Inside this file, place these contents:

from flask import Flask
app = Flask(__name__)

def hello_world():
    return "Hello World!"

if __name__ == "__main__":

Save the file and close the file. Now we need to create an file, this is actually going to be an empty file:

touch app/

Prepare Your New Flask App for Passenger

Next, we need to create a file named "". This is the entry file to our application:


Inside this file, place the below contents substituting %USER% for your own user on the server:

import sys, os
virt_binary = "/home/%USER%/pyenv/bin/python"
if sys.executable != virt_binary: os.execl(virt_binary, virt_binary, *sys.argv)
from app.helloworld import app as application

Save and close the file. The final step is to create an .htaccess file in the document root we want to serve the application from. Let's assume it's the root of your website, which would be the public_html directory. Let's change to that directory:

cd ~/public_html

Now, create or open the file named .htaccess:

vi .htaccess

Inside this file, place:

PassengerEnabled on
PassengerAppRoot /home/%USER%/helloworld
PassengerPython /home/%USER%/pyenv/bin/python

Save and close the file. Now try visiting your website, you should see "Hello World".

(1 vote(s))
This article was helpful
This article was not helpful

Help Desk Software by Kayako fusion
ERROR: This domain name (, does not match the domain name in the license key file

For assistance with your license, please contact the Kayako support team: