How to install the latest version of Python on CentOS

本文介绍如何通过编译源码的方式,在CentOS上安装最新的Python 2和Python 3版本,适用于CentOS 6和CentOS 7。教程强调了在非标准位置安装的重要性,并提供了配置建议。

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

https://danieleriksson.net/2017/02/08/how-to-install-latest-python-on-centos/

How to install the latest version of Python on CentOS

This tutorial will show you how to install the latest version of Python 2 and Python 3 on CentOS by compiling from source. The examples below are for Python 2.7.14 and Python 3.6.3, but the procedure is the same for any modern version of Python.

If you are using CentOS 6 you can use this tutorial to install both Python 2.7.x and Python 3.6.x. For CentOS 7 only the Python 3.6.x instructions below are applicable. Warning! Do not install Python 2.7.14 on CentOS 7 using these instructions. Your system will end up having two different python2.7 binaries, each with its own package directory. This will likely cause difficult-to-diagnose problems.

This tutorial should work for all versions of CentOS 6 and CentOS 7, and it will probably work on the corresponding RHEL distributions as well. I have verified it on CentOS 6.9 64 bit and CentOS 7 (1611) 64 bit.

This tutorial is meant for people that are comfortable with compiling and installing applications from source.

What is the problem?

CentOS ships with Python as a critical part of the base system. Because it is a critical part it is not getting updated, other than to plug security vulnerabilities. The lack of updates means that CentOS 6 users are stuck with Python 2.6.6 released in August 2010, and CentOS 7 users are stuck with Python 2.7.5 released in May 2013.

Solving the problem

Utilities such as yum will break if the default Python interpreter is upgraded or replaced. The trick is to install new versions of Python in /usr/local (or some other non-standard location) so that they can live side-by-side with the system version.

Things to consider

Before you compile and install Python there are a few things you should know and/or consider:

Unicode

Python has a long and complicated history when it comes to Unicode support. Unless you have very specific reasons you should configure Python 2.7 to enable UTF-32 support. This increases memory usage but improves compatibility. In Python 3.3+ the Unicode support has been completely rewritten and strings are automatically stored using the most efficient encoding possible.

You enable UTF-32 in Python 2.7 by passing --enable-unicode=ucs4 to the configure command.

Shared library

You should compile Python as a shared library by passing --enable-shared to the configure command. All modern Linux distros ship with Python compiled as a shared library. It reduces memory usage if more than one Python process is running, and there are third-party tools that might not work properly without it. To make sure the executable can find its shared library you also need to pass some additional flags to the configure command (LDFLAGS="-Wl,-rpath /usr/local/lib").

If you do not have sudo or root access you will probably not be able to compile Python as a shared library. If someone knows how to solve this please leave a comment below and I will update this text with instructions.

Use “make altinstall” to prevent problems

It is critical that you use 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. This can lead to problems that are very hard to diagnose.

Preparations – install prerequisites

In order to compile Python you must first install the development tools and a few extra libs. The extra libs are not strictly needed to compile Python but without them your new Python interpreter will be quite useless.

Execute all the commands below as root either by temporarily logging in as root or by using sudo.

 

Download, compile and install Python

Here are the commands to download, compile and install Python.

After running the commands above your newly installed Python interpreter will be available as /usr/local/bin/python2.7 or /usr/local/bin/python3.6. The system version of Python 2.6.6 will continue to be available as /usr/bin/python/usr/bin/python2 and /usr/bin/python2.6.

You might also want to strip symbols from the shared library to reduce the memory footprint.

 

Install/upgrade pip, setuptools and wheel

Each Python interpreter on your system needs its own install of pip, setuptools and wheel. The easiest way to install or upgrade these packages is by using the get-pip.py script.

The packages will end up in /usr/local/lib/pythonX.Y/site-packages/ (where X.Y is the Python version).

What’s next?

If you are using Python 2.7 I strongly recommend that you install virtualenv and learn how to use it. Virtualenv makes it possible to create isolated Python environments. If you are using Python 3.3+ then you don’t need virtualenv because that functionality is already built in.

Each isolated Python environment (also called sandbox) can have its own Python version and packages. This is very useful when you work on multiple projects or on different versions of the same project.

Create your first isolated Python environment

 

转载于:https://www.cnblogs.com/laozha/articles/9420681.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值