This tutorial walks you through setting up the tools you need to begin developing in Python and Flask. Note: I assume you are on a Unix environment (OSX, Linux, Linux VM on Windows).
- Python 2.7.x
- easy_install and pip
- Git 1.7/1.8
- virtualenv
- Flask
- Text editor (Sublime, vim, Komodo, gedit)
- Check your Python version -
python -V
- If you already have a 2.7.x version, move on to the next step.
- If not, download and install the latest 2.7.x version
- Check your Git version -
git --version
- Download the latest version, if necessary
-
Download setuptools for Python 2.7
$ curl -o setuptools-0.6c11-py2.7.egg https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg#md5=fe1f997bc722265116870bc7919059ea
-
Install setup tools
$ sudo sh setuptools-0.6c11-py2.7.egg
-
Clean up and delete egg
$ rm setuptools-0.6c11-py2.7.egg
-
Install pip
$ sudo easy_install pip
virtualenv is used to create a self-contained development environment.
-
Install
$ sudo pip install virtualenv
-
Setup your local environment
$ mkdir new_project $ cd new_project $ virtualenv --no-site-packages flask
-
Activate virtualenv
$ cd flask $ source bin/activate
-
Install
pip install Flask
-
Add a Git repository
$ git init
- Download a text editor. I prefer Sublime.
-
Create a new file named hello.py and add the following code:
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
Save the file to your "flask" directory.
-
Run the file with your Python interpreter
$ python hello.py
-
Point your browser at http://localhost:5000 to view the hello world greeting.
Cheers!