Setting up a Local Python Dev Environment

Our quickstarts are designed to get you working with Twilio in record time. To make things more efficient, we have used the same Python ingredients time and time again so you only need to go through environmental setup one time.

To complete the Twilio Python quickstarts, you'll need to have the following tools installed:

  • Python
  • Flask
  • The twilio-python library
  • ngrok or some other way to expose a URL for webhooks.

Install Python

If you are on a Mac or Linux machine, you probably already have Python installed. Windows users can follow this excellent tutorial. There are more advanced configuration instructions at the official Python website.

Installing Flask and twilio-python

Flask is a super simple web server written in Python. We're going to use it for this tutorial. To install it, we are going to install two tools: pip and virtualenv.

A Brief Introduction to Python Packages

Third party Python libraries, such as the math library numpy or the MySQL connector library MySQL-python, are hosted in packages, which are installed by a package manager. The package manager that comes with Python is called easy_install. However, most Python developers prefer to use a package manager called pip (why?).

pip is a package manager that you can use to install new Python libraries with one command. virtualenv is a tool that lets you create a special sandbox for your Python packages, to ensure that you have exactly the right version of every tool you need. Furthermore, virtualenv makes sure that you don't accidentally break your app if someone updates your Python packages.

How to install pip

The good news is that pip comes installed with virtualenv. We need to make sure though that we install pip and virtualenv for the correct version of Python on your computer. Open a terminal and run the following command:

python --version

It should say something like the following:

python --version
Python 2.6.1

Find the instructions below to install virtualenv for the version of Python reported by your terminal.

Installing virtualenv with Python 2.4

Run the following command:

easy_install virtualenv

Installing virtualenv with Python 2.5-2.7

If your Python version reported above is 2.5, 2.6 or 2.7, run easy_install with your Python version number, like this:

easy_install-2.7 virtualenv

Replace the version number 2.7 above with 2.5 or 2.6 if you have that version installed.

Installing virtualenv with Python 3.4+

You can install virtualenv using pip, which is included in Python 3.4+ installations:

# If you get 'permission denied' errors, try running "sudo python" instead of "python"
pip install virtualenv

Debugging errors

If you get the following error:

-bash: easy_install-2.5: command not found

you may need to ensure that Python is installed, or add the folder containing the easy_install program to your $PATH.

If you get an error that looks like this:

Installing pip script to /usr/local/bin
error: /usr/local/bin/pip: Permission denied

Then you need to run the install script as the admin user, like this:

sudo easy_install-2.6 virtualenv

Create and Activate your Virtual Environment

Once you have virtualenv installed, switch to the directory you'll use for your tutorial, and create a virtual environment:

cd Documents/my_tutorial_folder
virtualenv --no-site-packages .

Now activate the virtual environment.

source bin/activate


Note: You will need to activate your environment before every session with your Python server.

You can tell your virtualenv is running, because your terminal will have the name of the enclosing folder listed above it:

(quickstart)USER:~ user$

Install dependencies

Now we're going to install Flask and the twilio-python library. Open a file called requirements.txt and add the following lines to it:

requirements.txt
Flask>=0.12
twilio~=6.0.0

Then install these packages with pip, in your terminal:

bin/pip install -r requirements.txt

Test everything from scratch

First, make sure your virtualenv is up:

cd Documents/my_tutorial_folder
source bin/activate     # On Windows, use .\bin\activate.bat

Then open a file called run.py and add these lines:

run.py
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run(debug=True)

Now try running it. In your terminal, type:

python run.py
* Running on http://127.0.0.1:5000/ 

Navigate to http://localhost:5000 in your browser. You should see a Hello World message.

Congrats, you now have everything you need to start using Twilio and Python! Let's start coding.

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.