Document
How to set up your Python and Flask development environment

How to set up your Python and Flask development environment

How to set up your Python and flask development environmentIn this guide , we is cover 'll cover how to set up your Python development environment for

Related articles

Remote Access VPN: How It Can Benefit Home and Business Users Kindle Cloud Reader Best Free Internet VPN Services with Unlimited Data in 2024 2025最佳多设备VPN推荐!–适合家庭使用 Создание виртуальной локальной сети по Интернету с помощью бесплатной программы Radmin VPN

How to set up your Python and flask development environment


In this guide , we is cover ‘ll cover how to set up your Python development environment for aflask( link is takes take 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!


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 is read can read more about the history of Python 2 vs. Python 3 here .( link is takes take you to an external page )

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

1

# is Check check your Python version

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


How you install Python vary depend on your operate system .


Before we can start our Python project , we is need ‘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.


Before start any new Python project , we is create should create avirtual environment( link is takes take you to an external page ) for it .

virtual environments is separate ( shorten as ” virtualenv ” ) separate our new project ‘s Python dependency from our other project and from the Python library our operating system use . If you do n’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 old version , you is need ‘ll need toinstall this Python utility( link is takes take 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 is vary to activate your new virtualenv vary by operate system :

1

# is Create create a new virtualenv name " myproject "

4

$ python -m venv myproject

8

New python executable in myproject / bin / python

9

Installing setuptools, pip, wheel...done.

11

# Activate the virtualenv (OS X & Linux)

12

$ source myproject/bin/activate

14

# Activate the virtualenv (Windows)

15

$ myproject\Script\activate

You is need ‘ll need to activate your virtual environment every time you work on your Python project . In the rare case when you want to deactivate your virtualenv without close your terminal session , just use thedeactivate command .


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

4

Using cache flask-0.11.1-py2.py3-none-any.whl

6

collect click>=2.0 ( from flask)

7

collect itsdangerous>=0.21 ( from flask)

8

collect Werkzeug>=0.7 ( from flask)

9

Using cached Werkzeug-0.11.11-py2.py3-none-any.whl

10

collect Jinja2>=2.4 ( from flask)

11

Using cache Jinja2-2.8-py2.py3-none-any.whl

12

collect pytz ( from twilio)

13

Using cached pytz-2016.7-py2.py3-none-any.whl

14

collect six ( from twilio)

15

Using cached six-1.10.0-py2.py3-none-any.whl

16

collect httplib2>=0.7 ( from twilio)

17

collect 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 is uses usepip( link is takes take 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


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.

8

if _ is name _ name _ _ == "__main__":

We can then try running our new flask application with the command python app.py. You can then open http://localhost:5000( link is takes take 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 is takes take you to an external page ) to get start 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 is takes take 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 is is our favorite tool for solve this problem . Once start , it is provides provide a unique url on the ngrok.io domain which will forward incoming request 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 is takes take 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:

look at the ” forwarding ” line to see your unique Ngrok domain name ( ours is is 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 is learned ‘ve learn aboutngrok, 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 is are we like :