How to Install Python 3 on CentOS 7

本教程将指导您如何在CentOS 7系统上使用软件集合(SCL)安装Python 3,同时保留默认的Python 2.7版本。内容包括:启用SCL,安装Python 3,使用Python 3,安装开发工具以及创建虚拟环境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

This tutorial will guide you through installing Python 3 on a CentOS 7 system using the Software Collections (SCL) alongside the distribution default Python version 2.7. We will also show you how to create a Python virtual environment.

Python is one of the most popular programming languages in the world. With its simple and easy to learn syntax Python is a great choice for beginners and experienced developers. Python is a quite versatile programming language. You can use it to do almost anything you want, write small scripts, build games, develop websites, create machine learning algorithms, analyze data, and more.

Many popular applications and websites, including YouTube, DropBox, Reddit, Quora, Instagram, Pinterest have been developed using Python.

 

While Python 2 is well-supported and active, Python 3 is considered to be the present and future of the language.

Enable Software Collections (SCL)

Software Collections, also known as SCL is a community project that allows you to build, install, and use multiple versions of software on the same system, without affecting system default packages. By enabling SCL you will gain access to the newer versions of programming languages and services which are not available in the core repositories.

CentOS 7 ships with Python 2.7.5 which is a critical part of the CentOS base system. SCL allows you to install newer versions of python 3.x alongside the default python v2.7.5 so that system tools such as yum will continue to work properly.

To enable SCL, you need to install the CentOS SCL release file. It is part of the CentOS extras repository and can be installed by running the following command:

 

sudo yum install centos-release-scl

Copy

Installing Python 3 on CentOS 7

Now that you have access to the SCL repository, you can install any Python 3.x version you need. Currently, the following Python 3 collections are available:

  • Python 3.3
  • Python 3.4
  • Python 3.5
  • Python 3.6

In this tutorial, we will install Python 3.6, which is the latest version available at the time of writing. To do so type the following command on your CentOS 7 terminal:

sudo yum install rh-python36

Copy

Using Python 3

Once the package rh-python36 is installed, check the Python version by typing:

python --version

Copy

Python 2.7.5

Copy

You will notice that Python 2.7 is the default Python version in your current shell.

To access Python 3.6 you need to launch a new shell instance using the Software Collection scl tool:

 

scl enable rh-python36 bash

Copy

What the command above does is calling the script /opt/rh/rh-python36/enable, which changes the shell environment variables.

If you check the Python version again, you’ll notice that Python 3.6 is the default version in your current shell now.

python --version

Copy

Python 3.6.3

Copy

It is important to point out that Python 3.6 is set as the default Python version only in this shell session. If you exit the session or open a new session from another terminal Python 2.7 will be the default Python version.

Installing Development Tools

Development tools are required for building Python modules. To install the necessary tools and libraries type:

 

sudo yum groupinstall 'Development Tools'

Copy

Creating a Virtual Environment

Python Virtual Environments allows you to install Python modules in an isolated location for a specific project, rather than being installed globally. This way, you do not have to worry about affecting other Python projects.

The preferred way to create a new virtual environment in Python 3 is by executing the venv command.

Let’s say you want to create a new Python 3 project called my_new_project inside your user home directory and matching virtual environment.

 

First, create the project directory and switch to it:

mkdir ~/my_new_project
cd ~/my_new_project

Copy

Activate Python 3.6 using the scl tool:

scl enable rh-python36 bash

Copy

From inside the project root run the following command to create a virtual environment named my_project_venv:

 

python -m venv my_project_venv

Copy

To start use the virtual environment, activate it by typing:

source my_project_venv/bin/activate

Copy

After activating the environment, the shell prompt will be prefixed with the name of the environment:

(my_project_venv) user@host:~/my_new_project$

Copy

Starting with Python 3.4, when creating virtual environments pip, the package manager for Python is installed by default.

Conclusion

You should now have Python 3 programming environment set up on your CentOS 7 machine, and you can start developing your Python 3 project.

 

You can also read about how to install Odoo 11 on CentOS 7 and see an example of how to use Python 3 virtual environment to run Python software on a CentOS 7 server.

If you have any questions or feedback, feel free to comment below.

link:https://linuxize.com/post/how-to-install-python-3-on-centos-7/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值