在 Linux 中安装 Python3

系统环境:CentOS 6.8

在 Python 官网下载源码包:https://www.python.org/downloads/ 选择对应平台的源码包进行下载。

找到源码包后可以拷贝下载链接,在 Linux 中直接使用 wget 命令下载:

wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz

下载后解压:

tar -xJf Python-3.6.0.tar.xz
(或者直接:tar -xf Python-3.6.0.tar.xz)

进入安装包中,查看 README 文件,有安装提示:

Build Instructions

On Unix, Linux, BSD, OSX, and Cygwin:

./configure
make
make test
sudo make install

This will install Python as python3.

查看更多编译选项:

./configure --help

默认安装目录为 PREFIX 为 /usr/local,程序文件会安装到 /usr/local/bin, /usr/local/lib 等位置。

make test 报错:

======================================================================
ERROR: test_idna (test.test_socket.GeneralModuleTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/root/Python-3.6.0/Lib/test/test_socket.py", line 1337, in test_idna
    socket.gethostbyname(domain)
socket.gaierror: [Errno -3] Temporary failure in name resolution

----------------------------------------------------------------------
Ran 539 tests in 148.041s

FAILED (errors=1, skipped=24)
test test_socket failed
2 tests failed again:
    test_dbm test_socket

Total duration: 15 min 17 sec
Tests result: FAILURE
make: *** [test] Error 1

还是 make install,最后显示:

Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-9.0.1 setuptools-28.8.0

python的执行命令安装在了 /usr/local/bin 下面,因为 $PATH 路径已经包含这个路径,所以可以直接在命令行执行 python3 命令。

[root@vm1 etc]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

你可能感兴趣的:(在 Linux 中安装 Python3)