How to install Python 3, pip, venv, Virtualenv, and Pipenv on RHEL
This article shows how to install Python 3, pip, venv, virtualenv, and pipenv on Red Hat Enterprise Linux 7. After following the steps in this article, you should be in a good position to follow many Python guides and tutorials using RHEL.
Using Python virtual environments is a best practice to isolate project-specific dependencies and create reproducible environments. Other tips and FAQs for working with Python and software collections on RHEL 7 are also covered.
There are a number of different ways to get Python 3 installed on RHEL. There are many articles and answers online that give incomplete steps or inappropriate answers for people/enterprises that are running RHEL.
This article uses Red Hat Software Collections because these give you a current Python installation that is built and supported by Red Hat. During development, support might not seem that important to you. However, support is important to those who have to deploy and operate the applications you write. To understand why this is important, consider what happens when your application is in production and a critical security vulnerability in a core library (for example SSL/TLS) is discovered. This type of scenario is why many enterprises use Red Hat.
Python 3.6 is used in this article. It was the most recent, stable release when this was written. However, you should be able to use these instructions for any of the versions of Python in Red Hat Software Collections including 2.7, 3.4, 3.5, and future collections such as 3.7.
I hope this answers a number of questions about software collections and working with Python 3 on RHEL. Feedback is welcome.
More on reddit.comInstall Python3.9 and new virtual environment
virtualenv - Unable to create virtual environment on CentOS 7.8 and python3.6 - Stack Overflow
Install python3.9 in python virtual environment Debian 10 Linux
Videos
Hi, I'm running Centos 7.9 and Python 3.6.8. I have all my scripts running a virtual environment using Python 3.6.8.
I need to use some features in Python3.7 so I thought I would just go straight to 3.9. Has anybody got some clear instructions for me to a) install Python3.9 on CentOS and b) setup a new virtual environment using that new version of Python?
I have a lot of scripts running in my existing virtual environment so want to set this up in parallel so I don't affect the old environment. I can then migrate each script one at a time.
Any help appreciated.
Mark
Hi All,
I am looking to install python3.9 in a virtual environment in my Debian 10 machine. How could I do that? I tried to give command python3.9 -m venv ~/py_env/web_app when creating a virtual environment. but it said bad command. When I python3 -m venv ~/py_env/web_app it creates a virtual environment. with the current version of python3 (python 3.7) which is installed in the system. Would anyone be able to help me in this regards.
Thanks & Best Regards
Michael
The recommended way by python.org
The recommended way of managing virtual environments since Python 3.5 is with the venv module within the Python Standard Library itself.
Source: https://docs.python.org/3/library/venv.html#creating-virtual-environments
That is not the same as virtualenv, which is a third party package, outside the Python Standard Library.
Source: https://pypi.org/project/virtualenv/
Dangerous to downgrade (and to upgrade)
Depending on if your system itself uses Python, it could be dangerous for system stability to change the system Python version. Your system might need exactly that version of Python. That is true with Ubuntu.
Instructions for Ubuntu
Tested on Ubuntu 20.04
Install another version of Python
Safer than downgrading or upgrading is installing other versions of Python on the same system.
For example, to install Python 3.9:
## Add the deadsnakes repository
me@mydevice:~$ sudo add-apt-repository ppa:deadsnakes/ppa
## Update package lists
me@mydevice:~$ sudo apt update
## Install Python 3.9
me@mydevice:~$ sudo apt install python3.9
Install the venv package and create a venv virtual environment
## Install the venv package for Python 3.9
me@mydevice:~$ sudo apt install python3.9-venv
## Make a folder for venv virtual environments
me@mydevice:~$ mkdir ~/.venvs
## Create a new venv virtual environment with Python 3.9 in it
me@mydevice:~$ python3.9 -m venv ~/.venvs/my-venv-name
## Activate the new venv
me@mydevice:~$ source ~/.venvs/my-venv-name/bin/activate
(my-venv-name) me@mydevice:~$
Check versions within the venv virtual environment
## Check the Python version inside the venv:
(my-venv-name) me@mydevice:~$ python -V
Python 3.9.9
## Check the Pip version inside the venv:
(my-venv-name) me@mydevice:~$ pip3 --version
pip 21.2.4 from /home/me/.venvs/my-venv-name/lib/python3.9/site-packages/pip (python 3.9)
Deactivate the venv virtual environment
(my-venv-name) me@mydevice:~$ deactivate
me@mydevice:~$
Check versions outside any virtual environments
## Check Python version:
me@mydevice:~$ python -V
Python 3.8.10
## Check the Pip version:
me@mydevice:~$ pip3 --version
pip 20.0.2 from /home/me/.venvs/my-venv-name/lib/python3.8/site-packages/pip (python 3.8)
Install more Python versions
To install more Python versions, just change the version number from 3.9 to which ever version you choose, that is available from the deadsnakes repository.
Simple and recent
Supposed that you have a different version of Python installed in your system. To check use the following command to check:
py --list
Output:
-3.10-64 *
-3.7-64
And you want to create a new virtual environment for python 3.7 on a 'test_env' directory. Run the following command:
py -3.7 -m venv test_env
Then activate the test_env by running the following command in Windows PowerShell:
.\test_env\Scripts\Activate.ps1
Or Linux:
source test_env/bin/activate
Check:
python --version
Output:
Python 3.7.0