Skip to contentSkip to navigationSkip to topbar
On this page

Set up your Python and Flask development environment


In this guide, you'll set up your Python and Flask development environment to build Twilio applications. By the end of this tutorial, you'll be ready to create Flask web applications that interact with Twilio services.


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 continued to prefer Python 2 for various reasons. Note that Python 2 reached its End of Life on January 1, 2020, and is no longer maintained. Therefore, it's recommended to use Python 3 for new projects. For more information, see the Python wiki page on Python 2 or Python 3(link takes you to an external page).

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

To check your Python version, open a terminal and run:

1
$ python --version
2
Python 3.9.0

On some systems, you might need to use python3 instead of python:

1
$ python3 --version
2
Python 3.11.5
(information)

Info

On macOS, the python command might refer to Python 2, and python3 refers to Python 3. Use python3 when working with Python 3 on macOS.


How you install Python varies depending on your operating system.

Operating systemInstructions
macOSmacOS comes with Python pre-installed, but it's recommended to install your own Python 3 version. Homebrew(link takes you to an external page) is a popular tool for installing Python 3 on macOS. You can find detailed instructions in the Python Guide for macOS(link takes you to an external page).
WindowsThe best way to install Python on Windows is by using the official installer from the Python Software Foundation(link takes you to an external page). The x86-64 (64-bit) version is generally recommended, but if you need better compatibility with certain third-party packages, the x86 (32-bit) version may be more suitable.
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) on their respective websites.

Install a text editor or IDE

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

Before you can start your Python project, you'll need an editor or integrated development environment (IDE) to write your code.

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

If you're new to programming, consider giving Sublime Text and PyCharm a try before you settle on your favorite. Follow these steps to create and activate a virtual environment:

  1. If you have Python 3.3 or above, you can use the venv module from the standard library. If you have an older version, you'll need to install the virtualenv utility.

  2. Use the appropriate command to create a new virtual environment named myproject. On some systems, you might need to use python3 instead of python:

    1
    # Python 3.3+ (using python or python3 depending on your system)
    2
    $ python -m venv myproject
    3
    4
    # Or
    5
    $ python3 -m venv myproject
  3. Activate the virtual environment using the following commands, depending on your operating system:

    1
    # Activate the virtual environment (macOS and Linux)
    2
    $ source myproject/bin/activate
    3
    4
    # Activate the virtual environment (Windows)
    5
    $ myproject\Scripts\activate
$ pip install Flask twilio

Note: If you're using Python 3 outside of a virtual environment, you might need to use pip3 instead of pip. You're almost ready to start writing your Flask web application. First, you need to install the Flask library and the Twilio Python SDK in your virtual environment. Python uses pip to manage dependencies, so the command to install Flask and the Twilio SDK into your development environment is pip install Flask twilio.

  1. Ensure your virtual environment is active.

  2. Install Flask and the Twilio library using pip:

    $ pip install Flask twilio

You're almost ready to start writing your Flask web application. First, you need to install the Flask library and the Twilio Python SDK in your virtual environment. Note: If you're using Python 3 outside of a virtual environment, you might need to use pip3 instead of pip.

  1. Ensure your virtual environment is active.

  2. Install Flask and the Twilio library using pip:

    $ pip install Flask twilio

    Note: If you're using Python 3 outside of a virtual environment, you might need to use pip3 instead of pip.

  3. After installing your dependencies, you might want to keep track of and control which versions you're using. Pip allows you to "freeze" your dependencies and record the versions in a file called requirements.txt. Create a requirements file with this command:

    $ pip freeze > requirements.txt

if name == "main": app.run() You can test that your development environment is configured correctly by creating a simple Flask application. Follow these steps:

  1. Create a new file named app.py and copy the following code into it:

    1
    from flask import Flask
    2
    app = Flask(__name__)
    3
    4
    @app.route("/")

You can test that your development environment is configured correctly by creating a simple Flask application. Follow these steps:

  1. Create a new file named app.py and copy the following code into it:

    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()
  2. Run your new Flask application with the command:

    1
    cd myproject
    2
    $ python app.py

    Note: On macOS and some Linux systems, you might need to use python3 instead of python:

    1
    cd myproject
    2
    $ python3 app.py
  3. Open http://localhost:5000(link takes you to an external page) in your browser. You should see the "Hello World!" response.

    Note: If you're using a virtual machine for your development environment, such as 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.

    Note: If you're using a virtual machine for your development environment, such as 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. 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 a helpful tool to solve 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 download 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 output showing a sample domain forwarding to localhost port 5000.

You should see a "Forwarding" line in your ngrok terminal output, e.g.:

Forwarding http://aaf29606.ngrok.io -> http://localhost:5000

Look at the "Forwarding" line to see your unique ngrok domain name (for example, aaf29606.ngrok.io), and then open that URL in your browser.

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.


You've learned about ngrok, pip, and virtual environments, and now you're ready to build your Flask application. Learn more with the following resources:

Twilio

twilio page anchor