Python was first published in 1991, with its name dubbed from a comedy group : Monty Python, because the developers wanted to make a language that was fun to use. Among other things, they wanted ease of setup, straight forward syntax and easy error debugging.
Python is an all purpose language, meaning it can work at the front end or the back end. At the core of most linux web servers, such as CentOS is is used for the Yum program and shell scripting. On the CentOS server python comes pre-installed but more recent versions would have to be downloaded if needed for development. Python3 is the current version of Python and would be used in this tutorial.
» A linux server plan having CentOS 7
» A non-root user having sudo privileges to gain admin rights.
We will start by firing up a shell prompt and using the open-source package manager tool yum. Yum means Yellowdog Updater Modified, it is a powerful tool for working with software packages in systems such as CentOS and all others based on Red Hat Linux. With it OS programs on your machine can be easily installed, updated and removed.
We start by making sure that Yum is up to date by firing this command:
$ sudo yum update
Following that we install yum-utils, a suit of plugins and utilities that extend and supplement yum :
$ sudo yum -y install yum-utils
The -y flag, prevents the system from prompting us to confirm the action we just took.
Then we install the development tools for CentOS, used to build and compile software from source:
$ sudo yum -y groupinstall development
Next, IUS (In line with Upstream Stable), a community project that ensures all installed programs for the RPM (Red Hat Package manager) follow the stable version philosophy of Red Hat, is installed. This is done with Yum.
$ sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm
Once IUS is in place and finnished installing, the most recent Python version can be installed.
$ sudo yum -y install python36u
That will install Python3.6 and its successful installation can be confirmed with the command:
$ python3.6 -V
A successfull install will output:
With Python in place, Pip the software manager for Python packages is installed
$ sudo yum -y install python36u-pip
With pip, Python, packages can easily be managed. They can also be installed as shown:
$ sudo pip3.6 install package_name
In this instance, package_name refers to a Python package such as scikit-learn for machine learning and Django for web development
So this will install scikit-learn : pip3.6 install scikit-learn install.
Finally we install the IUS package python36u-devel, which comes packed with headers and libraries for Python3 development:
$ sudo sudo yum -y install python36u-devel
This is done with venv after Python has been installed and fully setup. The programming environment is set up with venv. These is space in your computing environment for Python projects, for ensuring that each project can have its own files and dependencies, without affecting any other project.
This is important for versioning and control while working with third party packages. You computer can handle as many Python environments as you possible, they are simply folders and directories with thier own scripts for ensuring they can run code independently.
We start by making the environments directory and entering it with the cd command:
$ mkdir environments
$ cd environments
Within the chosen directory, the environment can be created with the command :
$ python3.6 -m venv new_env
The second directory created, contains the system like files that constitute the new Python environment. These are present within the new_env directory, these are:
$ bin include lib lib64 pyvenv.cfg
With this each project can have its own environment and access to the packages that it needs.
To set up the new environment for use, it needs to be activated by calling the activate script in the bin directory :
$ source new_env/bin/activate
Your shell prompt will then have you new environment name prepended to it, in this example new_env.
(new_env) [lb@localhost] environments]$
The prefix new_env makes it clear that is the active environment for the current programming session. Meaning whatever programs are created will use the settings and packages in new_env.
We can create a simple program to make that new environment work and confirm we had a successfull installation.
To do this we create a file with a py extension using the vi editor within the new_env shell prompt.
(new_env) [lb@localhost] environments]$ vi greeting.py
With the file open, we type the i command to enter the vi insert mode and type the program:
Then press the ESC key to get out of insert mode. Then, type :x then ENTER to save and exit the file.
Next we run the program :
(new_env) [lb@localhost] environments]$ python greeting.py
The greeting.py program that you just created should produce
Output Hello, World!
To leave the environment, simply execute the command deactivate and you’ll return to your original directory.
Great stuff! Now you have a Python 3 environment locally present on your CentOS 7 machine and you can begin a coding project.