Use Python and Flask on Linux to create your first application

Source: Internet
Author: User
Tags virtual environment
Whether you are entertaining or working on linux, this is a great opportunity for you to program using python. Back to college I want them to teach me Python instead of Java, which is interesting to learn and useful in practical applications such as yum package manager. In this tutorial, I will show you how to use python and a micro-framework called flask to build a simple application to display useful information such as memory usage and CPU percentage of each process. Prerequisites: Python basics, lists, classes, functions, and modules. HTMLCSS (basic ). Learning this tutorial... whether you are entertaining or working on linux, this is a great opportunity for you to use python for programming. Back to college I want them to teach me Python instead of Java, which is interesting to learn and useful in practical applications such as yum package manager.

In this tutorial, I will show you how to use python and a micro-framework called flask to build a simple application to display useful information such as memory usage and CPU percentage of each process.

Prerequisites

Python basics, lists, classes, functions, and modules. HTML/CSS (basic ).

To learn this tutorial, you do not have to be a python advanced developer.

Install Python 3 on Linux

Python is installed by default on most Linux distributions. The following command shows the installed version.

[root@linux-vps ~]# python -VPython 2.7.5

We will use version 3.x to build our app. According to Python.org, this version is only improved and is not backward compatible with Python 2.

Note:: Before you start, I strongly recommend that you try this tutorial on a virtual machine, because Python is the core component of many Linux distributions and may damage your system in any way.

The following steps are Red Hat-based versions such as CentOS (6 and 7). Debian-based versions such as tumint and Resbian can skip this step. Pythonn 3 should have been installed by default. If not, use apt-get instead of yum to install the corresponding package.

[Leo @ linux-vps] yum groupinstall 'development tool' [leo @ linux-vps] yum install-y zlib-dev openssl-devel sqlite-devel bzip2-devel [leo @ linux-vps] wget https://www.python.org/ftp/python/3.4.2/Python-3.4.2.tgz[leo@linux-vps] tar-xvzf Python-3.4.2.tgz [leo @ linux-vps] cd Python-3.4.2 [leo @ linux-vps]. /configure [leo @ linux-vps] make # make altinstall is recommended to overwrite the current python library [leo @ linux-vps] make altinstall

After successful installation, you should be able to use the following command to enter the shell of Python3.4.

[leo@linux-vps]# python3.4Python 3.4.2 (default, Dec 12 2014, 08:01:15)[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> exit ()
Use pip to install packages

Python has its own package management, similar to yum and apt-get. You will need it to download, install, and uninstall the package.

[leo@linux-vps] pip3.4 install "packagename"    [leo@linux-vps] pip3.4 list[leo@linux-vps] pip3.4 uninstall "packagename"
Python virtual environment

In Python, a virtual environment is a directory where your project depends on the environment. This is a good way to isolate projects with different dependent environments. It allows you to install the installation package without using the sudo command.

[leo@linux-vps] mkdir python3.4-flask[leo@linux-vps] cd python3.4-flask [leo@linux-vps python3.4-flask] pyvenv-3.4 venv

To create a virtual environment, you need to use the pyvenv-3.4 command. The above Command will create a directory named lib inside the venv folder, where the package on which the project depends will be installed. A bin folder will also be created to hold the pip and python executable files in the environment.

Activate the virtual environment for our Linux system Information Project
[leo@linux-vps python3.4-flask] source venv/bin/activate [leo@linux-vps python3.4-flask] which pip3.4~/python3.4-flask/venv/bin/pip3.4[leo@linux-vps python3.4-flask] which python3.4~/python3.4-flask/venv/bin/python3.4
Install flask using pip

Let's continue to install the flask framework of the first module, which can process access routing and render and display the template of our app.

[leo@linux-vps python3.4-flask]pip3.4 install flask

The preceding section describes how to use Python and Flask to create your first application on Linux. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.