Whether you're entertaining or working on Linux, this is a great opportunity for you to program with Python. Back to college I want them to teach me python instead of Java, which is interesting to learn and useful in real-world applications like the Yum Package Manager.
In this tutorial I'll take you through the use of Python and a mini-framework called flask to build a simple application that shows useful information such as memory usage per process, CPU percentage, and so on.
Front-facing requirements
Python basics, lists, classes, functions, modules. Html/css (Basic).
Learn this tutorial you don't have to be a Python advanced developer
Installing Python 3 on Linux
Python is installed by default on most Linux distributions. The following command allows you to see the installed version.
[Root@linux-vps ~]# Python-vpython 2.7.5
We will use the 3.x version to build our app. According to Python.org, this version is now only improved and is not backwards compatible with Python 2.
Note : Before you start, I strongly recommend that you try this tutorial in a virtual machine, because Python is the core component of many Linux distributions, and any accident can damage your system.
The following steps are based on Red Hat versions such as CentOS (6 and 7), Debian-based versions such as Ubuntumint and Resbian can skip this step, Pythonn 3 should already be installed by default. If not, install the appropriate package using Apt-get instead of yum.
[Leo@linux-vps] Yum Groupinstall ' development Tools ' [Leo@linux-vps] yum install-y zlib-dev openssl-devel sqlite-devel BZ Ip2-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# recommended make Altinstall To overwrite the current Python library [Leo@linux-vps] make altinstall
After successful installation, you should be able to enter the Python3.4 shell with the following command.
[leo@linux-vps]# Python3.4python 3.4.2 (default, Dec, 08:01:15) [GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on Linuxtyp E "Help", "copyright", "credits" or "license" for more information.>>> exit ()
Using 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 of dependent environments where your project is placed. This is a good way to isolate projects with different dependent environments. It allows you to install packages without 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 creates a directory called Lib inside the Venv folder, where the packages that the project depends on are installed. A bin folder will also be created to accommodate Pip and Python executables in that environment.
Activating a 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
Installing Flask with PIP
Let's continue to install the first module flask framework, which can handle access to routes and render templates that display our app.
[Leo@linux-vps python3.4-flask]pip3.4 Install flask