メニュー

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:

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

Pythonをインストールする

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

Install Flask and twilio-python

Flask 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 and virtualenv.

A brief introduction to Python packages

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 to use a package manager called pip, but easy_install is another.

A brief introduction to Python virtual environments

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

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:

python --version

下記のような表示が出るはずです。

python --version
Python 3.9.1

下記にvirtualenvのインストール方法が記載されています。

Install with Python 2.4

下記のコマンドを実行します。

easy_install virtualenv

Install with Python 2.5-2.7

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

easy_install-2.7 virtualenv

バージョン番号 2.7 の部分を2.5, 2.6 に置き換えてください。

Install with Python 3.4 or above

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

pip install virtualenv

How to deal with installation errors

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.

もし下記のようなエラーが表示された場合:

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

adminユーザーとして下記のスクリプトを実行します。

sudo easy_install-2.6 virtualenv

仮想環境を作成およびアクティブにする

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 .

仮想環境が作られました。

source bin/activate

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

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:

(quickstart)USER:~ user$

Install the packages we need

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:

Flask>=0.12
twilio~=6.0.0

Install these packages with pip:

bin/pip install -r requirements.txt

Start Flask’s web server

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

from flask import Flask
app = Flask(__name__)

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

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

Now run it. In your terminal, type:

python run.py

You should see the output:

* Running on http://127.0.0.1:5000/ 

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

Where next?

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

ページを評価:

ヘルプが必要ですか?

誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。

フィードバックくださりありがとうございます!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Sending your feedback...
🎉 Thank you for your feedback!
Something went wrong. Please try again.

Thanks for your feedback!

Refer us and get $10 in 3 simple steps!

ステップ1

Get link

Get a free personal referral link here

ステップ2:

Give $10

Your user signs up and upgrade using link

ステップ3

Get $10

1,250 free SMSes
OR 1,000 free voice mins
OR 12,000 chats
OR more