Linux下编译安装python3环境

CentOS7系统 编译安装Python3 环境

编译安装Python之前,需要准备编译环境

[root@localhost bin]# yum install -y gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel ncurses-devel libffi-devel
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
Package gcc-4.8.5-36.el7_6.1.x86_64 already installed and latest version
Package 1:make-3.82-23.el7.x86_64 already installed and latest version
Package patch-2.7.1-10.el7_5.x86_64 already installed and latest version
Package gdbm-devel-1.10-8.el7.x86_64 already installed and latest version
Package 1:openssl-devel-1.0.2k-16.el7_6.1.x86_64 already installed and latest version
Package sqlite-devel-3.7.17-8.el7.x86_64 already installed and latest version
Package readline-devel-6.2-10.el7.x86_64 already installed and latest version
Package zlib-devel-1.2.7-18.el7.x86_64 already installed and latest version
Package bzip2-devel-1.0.6-13.el7.x86_64 already installed and latest version
Package ncurses-devel-5.9-14.20130511.el7_4.x86_64 already installed and latest version
Package libffi-devel-3.0.13-18.el7.x86_64 already installed and latest version
Nothing to do

获取python3源码包
访问 Python 官方网站,下载指定版本的源代码包, 在网站 Download导航栏中选择 Source code ,然后在跳转的页面中选择自己想要的 python版本

[root@localhost ~]# wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
--2019-03-27 16:44:04--  https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
Resolving www.python.org (www.python.org)... 151.101.228.223, 2a04:4e42:1a::223
Connecting to www.python.org (www.python.org)|151.101.228.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 22973527 (22M) [application/octet-stream]
Saving to: ‘Python-3.7.3.tgz’

100%[=========================================================>] 22,973,527   158KB/s   in 2m 42s 

2019-03-26 16:46:49 (139 KB/s) - ‘Python-3.7.3.tgz’ saved [22973527/22973527]

[root@localhost ~]# 
[root@localhost ~]# ll
total 22476
-rw-------. 1 root root     1866 Jan  4 22:24 anaconda-ks.cfg
drwxr-xr-x. 2 root root     4096 Jan  4 22:30 Desktop
drwxr-xr-x. 2 root root     4096 Jan  4 22:30 Documents
drwxr-xr-x. 2 root root     4096 Jan  4 22:30 Downloads
-rw-r--r--. 1 root root     1914 Jan  4 22:27 initial-setup-ks.cfg
drwxr-xr-x. 2 root root     4096 Jan  4 22:30 Music
drwxr-xr-x. 2 root root     4096 Jan  4 22:30 Pictures
drwxr-xr-x. 2 root root     4096 Jan  4 22:30 Public
-rw-r--r--. 1 root root 22973527 Mar 26 04:59 Python-3.7.3.tgz
drwxr-xr-x. 2 root root     4096 Jan  4 22:30 Templates
drwxr-xr-x. 2 root root     4096 Jan  4 22:30 Videos

Linux下编译安装python3环境_第1张图片

[root@localhost ~]# tar xf Python-3.7.3.tgz 
[root@localhost ~]# ll
total 22480
-rw-------.  1 root root     1866 Jan  4 22:24 anaconda-ks.cfg
drwxr-xr-x.  2 root root     4096 Jan  4 22:30 Desktop
drwxr-xr-x.  2 root root     4096 Jan  4 22:30 Documents
drwxr-xr-x.  2 root root     4096 Jan  4 22:30 Downloads
-rw-r--r--.  1 root root     1914 Jan  4 22:27 initial-setup-ks.cfg
drwxr-xr-x.  2 root root     4096 Jan  4 22:30 Music
drwxr-xr-x.  2 root root     4096 Jan  4 22:30 Pictures
drwxr-xr-x.  2 root root     4096 Jan  4 22:30 Public
drwxr-xr-x. 18  501  501     4096 Mar 26 04:59 Python-3.7.3
-rw-r--r--.  1 root root 22973527 Mar 26 04:59 Python-3.7.3.tgz
drwxr-xr-x.  2 root root     4096 Jan  4 22:30 Templates
drwxr-xr-x.  2 root root     4096 Jan  4 22:30 Videos
[root@localhost ~]# cd Python-3.7.3/
[root@localhost Python-3.7.3]# ls
aclocal.m4           config.sub    Doc      install-sh  m4               Misc     Parser   Programs       README.rst
CODE_OF_CONDUCT.rst  configure     Grammar  Lib         Mac              Modules  PC       pyconfig.h.in  setup.py
config.guess         configure.ac  Include  LICENSE     Makefile.pre.in  Objects  PCbuild  Python         Tools
[root@localhost Python-3.7.3]#
[root@localhost Python-3.7.3]# ./configure --prefix=/usr/local/python37
[root@localhost Python-3.7.3]# make
[root@localhost Python-3.7.3]# make install
[root@localhost Python-3.7.3]# cd
[root@localhost ~]# cd /usr/local/python37/bin
[root@localhost bin]# ./python3.7 -V
Python 3.7.3
[root@localhost bin]# cd 
[root@localhost ~]# 

配置环境变量
配置环境变量,在/etc/profile.d目录里创建配置文件

[root@localhost ~]# cd /etc/profile.d/
[root@localhost profile.d]# ls
256term.csh                   bash_completion.sh  colorls.csh  flatpak.sh  less.csh       sh.local  vte.sh
256term.sh                    colorgrep.csh       colorls.sh   lang.csh    less.sh        vim.csh   which2.csh
abrt-console-notification.sh  colorgrep.sh        csh.local    lang.sh     PackageKit.sh  vim.sh    which2.sh
[root@localhost profile.d]# vi python3.7.sh
[root@localhost profile.d]# cat python3.7.sh 
#!/bin/bash

export PATH=$PATH:/usr/local/python37/bin
[root@localhost profile.d]#
[root@localhost profile.d]# source ./python3.7.sh 
[root@localhost profile.d]# python3.7 -V
Python 3.7.3
[root@localhost profile.d]#

你可能感兴趣的:(Linux)