In this post you can learn how to install and configure Python and web development tools like Django, Tornado, Flask and Pyramid, to implement web applications.
Python is one of the most used programming languages. Above all, there are a lot of reasons to learn and use Python as a multi-purpose development tool:
- Easy to Learn and Use: It is developer-oriented and high level.
- Expressive: The syntax is more understandable and readable.
- Interpreted: Easy to run and debug, suitable for beginners.
- Cross-platform: Python is portable. You can install and run it in Windows, Linux, Unix and Mac. Also, there are several other ports.
- Free and Open Source:The source-code is also available.
- Object-Oriented: Python supports OOP with classes and objects.
- Multi-paradigm: You can use procedural, object-oriented and functional programming paradigms.
- Extensible: You can compile and integrate new libraries and tools using languages like C/C++.
- Large Standard Library: Extensive functionality through a huge collection of modules and libraries through a package/module manager (pip) .
- GUI Programming: You can implement Graphical user interfaces using Python, for desktop and web applications.
- Integrated: You can easily integrate Python with languages like C, C++, JAVA etc.
Installing Python
MacOS Installation
Python is already installed along with other development tools in latest MacOS releases. However, you need to install additional packages and tools to get a lot of new functions available to start a new python development process. First of all, check your current python version:
python -V
Normally, Python 2.7 is installed. However, it’s a basic installation and you probably need more than the core Python packages. First of all, you need to install the core development tools for MacOS, including C/C++ compilers:
xcode-select --install
This command will install and configure basic development tools to compile some packages that will be needed in the next sections. You also may need to configure additional development tools like Homebrew and MacPorts before, to continue the installation process.
Install Python using HomeBrew
brew install python@2
This will install Python, setup tools(like easy_install
) and pip
. easy_install
is an installation utility to setup and install remote packages. pip
is a complete package manager and installer for Python, recommended over easy_install
with several additional features. Also, the installation will create links like python2
, to allow installation of additional python version 3 (default current version), running as python3
. You can install this version using HomeBrew.
brew install python
Now you have 3 commands available: python
for the latest version available (v3.7), python2
for version 2.7 and python3
for version 3.7. This could help when you need a specific version to run a specific tool or application. Then, to make sure your default python command points to the latest version installed, you need to add the following path to your ~/.bash_profile
:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
Actually, this path /usr/local/opt/python/libexec/bin
contains all the current version links for python.
Linux Installation
On Debian-based systems (Ubuntu), use APT:
sudo apt-get install python3
For Red Hat-based systems, use yum:
sudo yum install python
In SUSE-based distributions, use zypper:
sudo zypper install python3
Install Django
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. The simplest way to install Django is using the pip
command line:
pip install Django
After installing Django, you can use the Django command django-admin
to create a new Django project structure:
django-admin startproject [projectname]
And then, start your web development server with the command:
cd projectname python manage.py runserver
Install Tornado
Tornado is another Python web framework and asynchronous networking library. By using non-blocking network I/O, Tornado can scale to tens of thousands of open connections, making it ideal for long polling, WebSockets, and other applications that require a long-lived connection to each user. You can install Tornado using the pip command:
pip install tornado
This is a basic “hello world” example Web application using tornado:
import tornado.ioloop import tornado.web class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world") def make_app(): return tornado.web.Application([ (r"/", MainHandler), ]) if __name__ == "__main__": app = make_app() app.listen(8888) tornado.ioloop.IOLoop.current().start()
Also, there is an asynchronious websockets example in the Github project page.
Install Pyramid
Pyramid is another Web framework for Python. You can start small with this “hello world” minimal request/response web app. This may take you far, especially while learning. As your application grows, Pyramid offers many features that make writing complex software take less effort.
from wsgiref.simple_server import make_server from pyramid.config import Configurator from pyramid.response import Response def hello_world(request): return Response('Hello World!') if __name__ == '__main__': with Configurator() as config: config.add_route('hello', '/') config.add_view(hello_world, route_name='hello') app = config.make_wsgi_app() server = make_server('0.0.0.0', 6543, app) server.serve_forever()
You can install pyramid using the pip
command:
pip install "pyramid==1.10.1"
Install Flask
Flask is a Web microframework for Python based on Werkzeug and Jinja 2. Its features are basically: built-in development server and debugger, integrated unit testing, RESTful request dispatching, Jinja2 templating, and secure cookies (client side sessions). This is a simple “Hello World” to start a project:
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!"
To setup and run a local server:
pip install Flask FLASK_APP=hello.py flask run
Note: This article was originally posted on https://developerhowto.com/2018/10/31/install-python-and-web-development-tools/