In order to install Python 3 and set up its programming environment on CentOS 7, a step-by-step process can be followed to ensure a seamless and effective configuration. Please note that CentOS 7 comes with Python 2.7 installed by default, and this guide will focus on installing Python 3 alongside it.
Firstly, before initiating the installation process, it is prudent to ensure that the system’s package manager, YUM (Yellowdog Updater Modified), is up-to-date. This can be achieved by executing the following command in the terminal:

bashsudo yum update
Once the system packages are updated, Python 3 can be installed. However, CentOS’s default repositories might not have the latest version of Python 3. To address this, the Extra Packages for Enterprise Linux (EPEL) repository can be added. This repository provides additional software packages for CentOS.
bashsudo yum install epel-release
After successfully adding the EPEL repository, Python 3 and the accompanying package manager, pip, can be installed. The following command accomplishes this:
bashsudo yum install python3
To verify the installation and ascertain the Python version, the following command can be executed:
bashpython3 --version
This should display the installed Python 3 version, indicating a successful installation.
As Python alone might not be sufficient for all development purposes, the installation of the development tools group is recommended. This group includes essential tools like compilers and libraries necessary for building and compiling software. The following command installs the development tools group:
bashsudo yum groupinstall "Development Tools"
With Python 3 and the development tools in place, the next step involves setting up a virtual environment. A virtual environment is an isolated Python environment that allows for the installation of packages without affecting the system’s global Python installation. This is particularly beneficial for managing dependencies in different projects.
To create a virtual environment, the venv
module, which is included in the Python standard library, can be employed. Navigate to the desired directory where the virtual environment should be created and execute the following command:
bashpython3 -m venv myenv
In this command, “myenv” represents the chosen name for the virtual environment. This name can be customized based on the project’s requirements.
Following the creation of the virtual environment, it needs to be activated. Activation is accomplished with the following command:
bashsource myenv/bin/activate
Upon activation, the terminal prompt should reflect the virtual environment’s name, signifying that it is now the active Python environment.
With the virtual environment active, the installation of Python packages using pip ensures that the dependencies for a particular project are managed within the virtual environment. For instance, to install a package named “example_package,” the following command can be used:
bashpip install example_package
It is important to note that any packages installed while the virtual environment is active are confined to that environment, preventing interference with the global Python installation.
To deactivate the virtual environment once the work is complete, the command is simply:
bashdeactivate
Additionally, it is advisable to keep the system and installed packages up-to-date. This involves regularly updating the virtual environment’s package list and upgrading the installed packages to their latest versions. The following commands achieve this:
bashpip install --upgrade pip
pip list --outdated
pip install --upgrade $(pip list --outdated | awk '{print $1}')
This sequence of commands first updates pip to the latest version, then lists the outdated packages, and finally upgrades them to their latest versions.
In conclusion, the installation and configuration of Python 3 on CentOS 7 involve updating the system packages, adding the EPEL repository, installing Python 3 and development tools, and creating and activating a virtual environment for project-specific development. The use of virtual environments facilitates a clean and organized approach to managing dependencies, ensuring a robust and efficient development environment on CentOS 7.
More Informations
Certainly, delving further into the details, it’s important to highlight the significance of virtual environments in Python development, particularly on CentOS 7. A virtual environment offers a controlled and isolated environment for Python projects, mitigating potential conflicts between different projects and ensuring the integrity of dependencies.
When a virtual environment is created using the python3 -m venv
command, it not only establishes a dedicated space for the project but also encapsulates a copy of the Python interpreter, the standard library, and the pip package manager. This encapsulation ensures that the project can operate independently, unaffected by the global Python environment or other projects.
Activating a virtual environment involves modifying the system’s PATH variable, directing it to the virtual environment’s binaries. This ensures that when Python-related commands are executed, they refer to the binaries within the virtual environment. This activation mechanism allows developers to seamlessly switch between different virtual environments based on their project requirements.
Moreover, the utilization of the requirements.txt
file becomes integral in managing project dependencies within a virtual environment. This file typically contains a list of required packages and their versions. It enables developers to recreate the exact environment by installing the specified packages and versions, ensuring consistency across different development stages and environments.
Creating a requirements.txt
file can be accomplished by freezing the current state of installed packages in the virtual environment using the following command:
bashpip freeze > requirements.txt
Conversely, to install the packages listed in a requirements.txt
file, the following command proves useful:
bashpip install -r requirements.txt
This approach fosters collaboration and facilitates the deployment process, as it guarantees that all collaborators or deployment environments have the same set of dependencies.
Furthermore, CentOS 7 offers a robust firewall solution called firewalld, and when setting up a Python web application or any service that requires external access, it is essential to configure the firewall appropriately. This involves opening the necessary ports and allowing traffic to and from the desired services.
For instance, to allow traffic on port 5000 for a Flask web application, the following commands can be employed:
bashsudo firewall-cmd --zone=public --add-port=5000/tcp --permanent sudo firewall-cmd --reload
These commands add a rule to the firewall configuration, ensuring that traffic on port 5000 is permitted. The --permanent
flag makes the rule persistent across system reboots, and --reload
applies the changes without restarting the firewall service.
Moreover, when developing web applications with Python, the usage of web frameworks such as Flask or Django is prevalent. Flask, for example, provides a lightweight and flexible platform for building web applications. To install Flask within a virtual environment, the command is:
bashpip install Flask
Subsequently, a basic Flask application can be created, and with the built-in development server, the application becomes accessible on a specified port, usually 5000. However, for production deployments, it is recommended to use a production-ready server like Gunicorn or uWSGI in conjunction with a reverse proxy server like Nginx or Apache.
Additionally, the adoption of version control systems, such as Git, enhances collaboration and code management. Initiating a Git repository for a project involves executing the following commands:
bashgit init
git add .
git commit -m "Initial commit"
This establishes a versioned codebase, allowing developers to track changes, collaborate efficiently, and roll back to previous states if needed.
In conclusion, the comprehensive setup of Python 3 on CentOS 7 involves not only the installation of the interpreter and essential tools but also emphasizes the importance of virtual environments, dependency management, firewall configuration, and the utilization of version control systems. This holistic approach ensures a robust, scalable, and maintainable Python development environment on the CentOS 7 operating system.
Keywords
The key words in the article are:
-
CentOS 7:
- Explanation: CentOS 7 is a Linux distribution that serves as the basis for the article’s instructions. It is widely used in server environments and provides a stable and reliable platform for various applications.
-
YUM (Yellowdog Updater Modified):
- Explanation: YUM is the default package manager for CentOS 7. It facilitates the installation, updating, and removal of software packages on the system.
-
EPEL (Extra Packages for Enterprise Linux) repository:
- Explanation: The EPEL repository is an additional repository for CentOS that provides extra software packages not included in the default repositories. It enhances the range of available software for users.
-
Python 3:
- Explanation: Python 3 is the latest major version of the Python programming language. The article focuses on installing and configuring Python 3 alongside the default Python 2.7 on CentOS 7.
-
pip:
- Explanation: Pip is the package installer for Python. It is used to install and manage Python packages, ensuring a smooth and standardized process for handling dependencies.
-
Development Tools Group:
- Explanation: This group includes essential tools such as compilers and libraries that are necessary for building and compiling software. Installing this group ensures that development-related tools are available on the system.
-
Virtual Environment:
- Explanation: A virtual environment is an isolated Python environment that allows developers to manage project-specific dependencies independently of the system’s global Python installation. It helps avoid conflicts between different projects.
-
venv:
- Explanation: The
venv
module is part of the Python standard library and is used to create virtual environments. It encapsulates a copy of the Python interpreter, standard library, and pip for the specific project.
- Explanation: The
-
Activation/Deactivation of Virtual Environment:
- Explanation: Activating a virtual environment modifies the system’s PATH variable to use the virtual environment’s binaries. Deactivating returns to the global environment. This mechanism allows developers to work in project-specific environments.
-
requirements.txt:
- Explanation: This file contains a list of required Python packages and their versions. It is commonly used to specify project dependencies, enabling consistent environment replication across different systems.
-
Firewalld:
- Explanation: Firewalld is a dynamic firewall manager for Linux systems, including CentOS 7. It regulates incoming and outgoing network traffic, and the article mentions its use in configuring firewall rules for specific ports.
-
Flask:
- Explanation: Flask is a lightweight Python web framework used for developing web applications. The article introduces its installation within a virtual environment and briefly discusses its role in web development.
-
Git:
- Explanation: Git is a distributed version control system used for tracking changes in source code during software development. The article suggests initiating a Git repository to manage code versions and facilitate collaboration.
-
Gunicorn and uWSGI:
- Explanation: Gunicorn (Green Unicorn) and uWSGI are production-ready WSGI (Web Server Gateway Interface) servers for deploying Python web applications. They are mentioned as alternatives to the built-in development server for production deployments.
-
Nginx and Apache:
- Explanation: Nginx and Apache are popular web servers and reverse proxy servers. In the context of the article, they are suggested for use in conjunction with production-ready WSGI servers (Gunicorn or uWSGI) to enhance the performance and security of Python web applications.
-
Git Repository:
- Explanation: A Git repository is a storage location for a Git project. It tracks changes in files, facilitates collaboration among developers, and enables versioning. The article suggests initializing a Git repository for effective code management.
These keywords collectively form the foundation for setting up a comprehensive and efficient Python development environment on CentOS 7, covering aspects of package management, virtualization, web development, firewall configuration, and version control.