Step-by-Step Guide
- Update the Package Index
Open a terminal and update the package index to ensure you have the latest information about available packages:
sudo yum update -y
- Install Python
RHEL typically comes with Python pre-installed. However, you can install the latest versions of Python using theyum
package manager:
sudo yum install python3 -y
This command installs Python 3.
- Verify Python Installation
Check the Python version to ensure it is installed correctly:
python3 --version
You should see the Python version information displayed.
- Verify pip Installation
Check ifpip
, the Python package installer, is installed:
pip3 --version
If pip
is not installed, you can install it using:
sudo yum install python3-pip -y
- Install Virtualenv (Optional)
It’s a good practice to use virtual environments for your Python projects. Installvirtualenv
usingpip
:
sudo pip3 install virtualenv
- Create a Virtual Environment (Optional)
Create a new virtual environment for your project:
virtualenv venv
Activate the virtual environment:
source venv/bin/activate
Your command prompt should change to indicate that the virtual environment is active.
- Install Packages with pip
You can now install Python packages within your virtual environment usingpip
. For example, to install Flask, a popular web framework, use:
pip install flask
- Deactivate the Virtual Environment
When you are done working in the virtual environment, deactivate it:
deactivate
- Create a Simple Python Script
To test your Python installation, create a simple Python script:
nano hello.py
Add the following code to the file:
print("Hello, World!")
Save and close the file.
- Run the Python Script
Execute the script to ensure Python is working correctly:bash python3 hello.py