Knowledge Base

Create a Python 'Hello World' App with Flask

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/helloworld.py

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 __init__.py file, this is actually going to be an empty file:

touch app/__init__.py

Prepare Your New Flask App for Passenger

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

vi passenger_wsgi.py

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".

Did you find this article helpful?

* Your feedback is too short