Skip to contentSkip to navigationSkip to topbar
On this page

How to set up your Python and Flask development environment


In this guide, we'll cover how to set up your Python development environment for a Flask(link takes you to an external page) project. We'll use virtual environments to isolate our dependencies, and pip for package management. Also, we'll talk about a couple of helpful tools that we recommend for prototyping Python applications that use Twilio: ngrok and the Twilio Python SDK.

Let's get started!


Choose a Python version

choose-a-python-version page anchor

There are two different versions of Python in widespread use today: Python 2 and Python 3. If you're new to Python, choosing the right version for your project can be confusing.

Python 3 was first released in 2008, but some parts of the Python community continue to prefer Python 2 for various reasons. You can read more about the history of Python 2 vs. Python 3 here.(link takes you to an external page)

Twilio's Python server-side SDK supports Python versions 3.6+.

1
# Check your Python version
2
$ python --version
3
Python 3.7.0

If Python is already installed on your system, you can check its version by running python --version.


How you install Python varies depending on your operating system.

Operating SystemInstructions
OS XOS X already has Python installed, but we recommend configuring your own Python installation. Homebrew(link takes you to an external page) is our tool of choice for getting Python 2 or 3 installed on OS X. You can find detailed instructions here(link takes you to an external page).
WindowsThe best way to install Python on Windows is using the official installer from the Python Software Foundation(link takes you to an external page). The x86 (32-bit) version will tend to have better compatibility with third-party packages.
LinuxThe exact instructions to install Python vary by distribution. Find instructions for Ubuntu(link takes you to an external page), CentOS(link takes you to an external page), and other distributions(link takes you to an external page) here.

Install a text editor or IDE

install-a-text-editor-or-ide page anchor

Before we can start our Python project, we'll need something to write it with.

If you already have a code-writing tool of choice, you can stick with it for developing your Python application. If you're looking for something new, we recommend trying out a few options:

If you're new to programming, we recommend giving Sublime Text and PyCharm each a try before you settle on your favorite.


Start a new project with virtualenv

start-a-new-project-with-virtualenv page anchor

Before starting any new Python project, we should create a virtual environment(link takes you to an external page) for it.

Virtual environments (shortened as "virtualenv") separate our new project's Python dependencies from our other projects and from the Python libraries our operating system uses. If you don't use a virtualenv, there's a good chance you might break part of your OS.

If you have Python 3.3 or above you don't need to install anything - the standard library provides virtualenv under the module "venv".

If you have an older version, you'll need to install this Python utility(link takes you to an external page) which allows you to create and manage Python virtual environments. Use the virtualenv command to create a new virtual environment, using its sole argument to name your new environment. The instructions to activate your new virtualenv vary by operating system:

1
# Create a new virtualenv named "myproject"
2
3
# Python 3.3+
4
$ python -m venv myproject
5
6
# Python pre 3.3
7
$ virtualenv myproject
8
New python executable in myproject/bin/python
9
Installing setuptools, pip, wheel...done.
10
11
# Activate the virtualenv (OS X & Linux)
12
$ source myproject/bin/activate
13
14
# Activate the virtualenv (Windows)
15
$ myproject\Scripts\activate

You'll need to activate your virtual environment every time you work on your Python project. In the rare cases when you want to deactivate your virtualenv without closing your terminal session, just use the deactivate command.


Install Flask and the Twilio Python SDK

install-flask-and-the-twilio-python-sdk page anchor

We're almost ready to start writing our Flask web application, but first, we need to install the Flask library in our virtual environment.

1
# Use pip to install the Flask and twilio libraries
2
$ pip install Flask twilio
3
Collecting Flask
4
Using cached Flask-0.11.1-py2.py3-none-any.whl
5
Collecting twilio
6
Collecting click>=2.0 (from Flask)
7
Collecting itsdangerous>=0.21 (from Flask)
8
Collecting Werkzeug>=0.7 (from Flask)
9
Using cached Werkzeug-0.11.11-py2.py3-none-any.whl
10
Collecting Jinja2>=2.4 (from Flask)
11
Using cached Jinja2-2.8-py2.py3-none-any.whl
12
Collecting pytz (from twilio)
13
Using cached pytz-2016.7-py2.py3-none-any.whl
14
Collecting six (from twilio)
15
Using cached six-1.10.0-py2.py3-none-any.whl
16
Collecting httplib2>=0.7 (from twilio)
17
Collecting MarkupSafe (from Jinja2>=2.4->Flask)
18
Installing collected packages: click, itsdangerous, Werkzeug, MarkupSafe, Jinja2, Flask, pytz, six, httplib2, twilio
19
Successfully installed Flask-0.11.1 Jinja2-2.8 MarkupSafe-0.23 Werkzeug-0.11.11 click-6.6 httplib2-0.9.2 itsdangerous-0.24 pytz-2016.7 six-1.10.0 twilio-5.6.0

Python uses pip(link takes you to an external page) to manage dependencies, so the command to pull Flask and the Twilio SDK into our development environment is pip install Flask twilio.

After you get your dependencies installed and confirm they're doing the trick for you, you'll probably want to keep track of and control what versions of the dependencies you're using. Pip allows us to "freeze" our dependencies, and record which versions we are using in a file that (by convention) is called requirements.txt. Create a requirements file with this command:

pip freeze > requirements.txt

If later on, you wish to install this same set of dependencies again, you can install them from this file with the following command:

pip install -r requirements.txt

Create a simple Flask application

create-a-simple-flask-application page anchor

We can test that our development environment is configured correctly by creating a simple Flask application. We'll grab the nine-line example from Flask's homepage and drop it in a new file called app.py.

1
from flask import Flask
2
app = Flask(__name__)
3
4
@app.route("/")
5
def hello():
6
return "Hello World!"
7
8
if __name__ == "__main__":
9
app.run()

We can then try running our new Flask application with the command python app.py. You can then open http://localhost:5000(link takes you to an external page) in your browser, and you should see the "Hello World!" response.

Note: If you're using a virtual machine for your development environment, like Vagrant, you might not be able to see your Flask application at the localhost hostname. Continue to the ngrok section for an easy way to fix this.


While Flask is a micro-framework that can be extended with various libraries, Django is another popular web framework that provides a lot more out of the box. If you're interested in using Django, we recommend following their guides(link takes you to an external page) to get started with it.


Once you see your sample Flask application's "Hello World!" message, your development environment is ready to go. But for most Twilio projects you'll want to install one more helpful tool: ngrok(link takes you to an external page).

Most Twilio services use webhooks to communicate with your application. When Twilio receives an incoming phone call, for example, it reaches out to a URL in your application for instructions on how to handle the call.

When you're working on your Flask application in your development environment, your app is only reachable by other programs on the same computer, so Twilio won't be able to talk to it.

Ngrok is our favorite tool for solving this problem. Once started, it provides a unique URL on the ngrok.io domain which will forward incoming requests to your local development environment.

To start, head over to the Ngrok download page and grab the binary for your operating system: https://ngrok.com/download(link takes you to an external page)

Once downloaded, make sure your Flask application is running and then start Ngrok using this command: "./ngrok http 5000". You should see output similar to this:

ngrok screen.

Look at the "Forwarding" line to see your unique Ngrok domain name (ours is "aaf29606.ngrok.io") and then point your browser at that domain name.

If everything's working correctly, you should see your Flask application's "Hello World!" message displayed at your new Ngrok URL.

Anytime you're working on your Twilio application and need a URL for a webhook you should use Ngrok to get a publicly accessible URL like this one.


Nicely done! You've learned about ngrok, pip and virtual environments, and you're now ready to build out your Flask application.

Ready to build something more substantiate with Twilio? Here are a few other resources we like:

Twilio

twilio page anchor

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.