Install python3 on centos 7
Python is a general-purpose object-oriented programming language designed to be used as a software solution for almost all kinds of problems. However, the pre-installed version of Python found on CentOS 7 is a much older version. In order to have the latest version of Python, the user will have to install it manually.
By default centos is having python version of 2.7
First we will install python3.6 from repository
[root@centos ~]# yum install https://centos7.iuscommunity.org/ius-release.rpm
Now install python with pip
[root@centos ~]# yum install python36u python36u-libs python36u-devel python36u-pip
[root@centos ~]# python3.6 -V Python 3.6.8
compiling 3.7 from source code
This Python installation required GCC compiler on your system.install prerequisites for Python before installing it
[root@centos ~]# yum install gcc openssl-devel bzip2-devel libffi-devel [root@centos ~]# yum groupinstall "Development Tools"
download python 3.7
[root@centos ~]# cd /usr/src [root@centos src]# https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz [root@centos src]# tar -xvzf Python-3.7.4.tgz
use below commands to compile source code by using altinstall
[root@centos ~]# cd /usr/src/Python-3.7.4/ [root@centos Python-3.7.4]# ./configure --enable-optimizations [root@centos Python-3.7.4]# make altinstall
when you install your custom version of Python. If you use the normal "make install" you will end up with two different versions of Python in the filesystem both named python.so altinstall is used to prevent replacing the default python binary file /usr/bin/python
Installing collected packages: setuptools, pip Successfully installed pip-19.0.3 setuptools-40.8.0
check python version
[root@centos ~]# python3.7 -V Python 3.7.4
Setting up virtual environment
Virtual environments enable you to have an isolated space on your computer for Python projects, ensuring that each of your projects can have its own set of dependencies that won’t disrupt any of your other projects.
You can set up as many Python programming environments as you want. Each environment is basically a directory or folder in your computer that has a few scripts in it to make it act as an environment.
create new directory with mkdir
[root@centos ~]# mkdir environment [root@centos ~]# cd environment
you can create virtual environment with following command
[root@centos environment]# python3.7 -m venv myenv
you can give any name in place of myenv.
[root@centos environment]# cd myenv/ [root@centos myenv]# ls bin include lib lib64 pyvenv.cfg
To use this environment, you need to activate it by typing following command
[root@centos myenv]# source bin/activate
It will promt you with the name of your environment like below
(myenv) [root@centos myenv]#
Here myenv is active that means when we create programs here they will use only this particular environment’s settings and packages.
Create sample program
(myenv) [root@centos myenv]# vi hello.py
Once the text file opens up in our terminal window, we will have to type
i to enter insert mode, and then we can write our first program
Now press escape and save it using :wq
(myenv) [root@centos myenv]# python hello.py Hello, Welcome! (myenv) [root@centos myenv]# python -V Python 3.7.4
To leave the environment, simply type the command deactivate and return to your original directory.
(myenv) [root@centos myenv]# deactivate [root@centos myenv]# python -V Python 2.7.5