Skip to contentSkip to navigationSkip to topbar
Rate 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+.


_10
# Check your Python version
_10
$ python --version
_10
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:


_15
# Create a new virtualenv named "myproject"
_15
_15
# Python 3.3+
_15
$ python -m venv myproject
_15
_15
# Python pre 3.3
_15
$ virtualenv myproject
_15
New python executable in myproject/bin/python
_15
Installing setuptools, pip, wheel...done.
_15
_15
# Activate the virtualenv (OS X & Linux)
_15
$ source myproject/bin/activate
_15
_15
# 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.


_19
# Use pip to install the Flask and twilio libraries
_19
$ pip install Flask twilio
_19
Collecting Flask
_19
Using cached Flask-0.11.1-py2.py3-none-any.whl
_19
Collecting twilio
_19
Collecting click>=2.0 (from Flask)
_19
Collecting itsdangerous>=0.21 (from Flask)
_19
Collecting Werkzeug>=0.7 (from Flask)
_19
Using cached Werkzeug-0.11.11-py2.py3-none-any.whl
_19
Collecting Jinja2>=2.4 (from Flask)
_19
Using cached Jinja2-2.8-py2.py3-none-any.whl
_19
Collecting pytz (from twilio)
_19
Using cached pytz-2016.7-py2.py3-none-any.whl
_19
Collecting six (from twilio)
_19
Using cached six-1.10.0-py2.py3-none-any.whl
_19
Collecting httplib2>=0.7 (from twilio)
_19
Collecting MarkupSafe (from Jinja2>=2.4->Flask)
_19
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:


_10
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:


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


_10
from flask import Flask
_10
app = Flask(__name__)
_10
_10
@app.route("/")
_10
def hello():
_10
return "Hello World!"
_10
_10
if __name__ == "__main__":
_10
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

Rate this Page: