Skip to contentSkip to navigationSkip to topbar
Rate this Page:

How to Set up a Local Python Development Environment


Our quickstarts are designed to get you working with Twilio in record time. We've used the same Python ingredients time and time again so you only need to go through environmental setup once.

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


Install Python

install-python page anchor

If you are on a Mac or Linux machine, you most likely already have Python installed. Windows users can follow this excellent tutorial(link takes you to an external page). There are more advanced configuration instructions at the official Python website(link takes you to an external page).


Install Flask and twilio-python

install-flask-and-twilio-python page anchor

Flask(link takes you to an external page) is a super web server written in Python. We're going to use it for this tutorial. To install it, we are first going to install two tools: pip(link takes you to an external page) and virtualenv(link takes you to an external page).

A brief introduction to Python packages

a-brief-introduction-to-python-packages page anchor

Many third-party Python libraries, such as the math library numpy, the MySQL connector library MySQL-python, and Flash, are made available as "packages". They are installed using a "package manager". Most Python developers prefer(link takes you to an external page) to use a package manager called pip, but easy_install is another.

A brief introduction to Python virtual environments

a-brief-introduction-to-python-virtual-environments page anchor

virtualenv is a tool that lets you create a special sandbox for each of your Python projects, to ensure that you have exactly the right version of every tool you need for that particular project. Furthermore, virtualenv makes sure that you don't accidentally break your app if someone updates your Python packages.

Install virtualenv and pip

install-virtualenv-and-pip page anchor

We first need to make sure that we install pip and virtualenv for the correct version of Python on your computer. Open a terminal and run the following command:


_10
python --version

It should say something like the following:


_10
python --version
_10
Python 3.9.1

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

Install with Python 2.4

install-with-python-24 page anchor

Run the following command:


_10
easy_install virtualenv

Install with Python 2.5-2.7

install-with-python-25-27 page anchor

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


_10
easy_install-2.7 virtualenv

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

Install with Python 3.4 or above

install-with-python-34-or-above page anchor

pip is included in Python 3.4+ installations, so just install virtualenv with:


_10
pip install virtualenv

How to deal with installation errors

how-to-deal-with-installation-errors page anchor

If you get any permission denied errors, try running sudo python instead of python.

If you get an easy_install-2.5: command not found error, you may need to check 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:


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

Then you need to run the install script as the admin user(link takes you to an external page), like this:


_10
sudo easy_install-2.6 virtualenv

Create and activate your virtual environment

create-and-activate-your-virtual-environment page anchor

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


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

Now activate the virtual environment.


_10
source bin/activate

On Windows, use .\bin\activate.bat.

(warning)

Warning

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

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


_10
(quickstart)USER:~ user$

Install the packages we need

install-the-packages-we-need page anchor

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


_10
Flask>=0.12
_10
twilio~=6.0.0

Install these packages with pip:


_10
bin/pip install -r requirements.txt


Start Flask's web server

start-flasks-web-server page anchor

Create a file called run.py and add these lines to it:


_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(debug=True)

Now run it. In your terminal, type:


_10
python run.py

You should see the output:


_10
* Running on http://127.0.0.1:5000/

Navigate to http://localhost:5000(link takes you to an external page) in a browser. You should see a "Hello World" message.


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


Rate this Page: