00 实验环境

  1. # 发行版信息
  2. [root@localhost ~]# cat /etc/redhat-release
  3. CentOS Linux release 7.6.1810 (Core)
  4. # 内核
  5. [root@localhost ~]# uname -r
  6. 3.10.0-957.el7.x86_64
  7. # 时间
  8. chery@cherysc ~ date
  9. 2019年 1月 9日 星期三 10时47分07秒 CST

01 实验目的

在本机上通过编译安装的方式安装并配置python3

02 实验软件选择

020 python3选择版本

源码编译方式:https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz

小助手🐖 如果发现官方源太慢可以使用 淘宝源

https://npm.taobao.org/mirrors/python/

03 准备安装

030 准备安装软件()

  1. # 使用官方源
  2. [root@chery tools]# wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
  3. # 也可以使用淘宝源
  4. [root@chery tools]# wget https://npm.taobao.org/mirrors/python/3.6.8/Python-3.6.8.tgz

031 准备依赖包

  1. yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y

如果不安装zlib,就报错:
zipimport.ZipImportError: can’t decompress data; zlib not available
make: * [install] Error 1

04 开始安装

040 解压安装包

  1. [root@chery tools]# tar -xvf Python-3.6.8.tgz
  2. [root@chery tools]# ls
  3. mysql-5.6.41 mysql-5.6.41.tar.gz Python-3.6.8 Python-3.6.8.tgz
  4. [root@chery tools]# cd Python-3.6.8
  5. [root@chery Python-3.6.8]# ls
  6. aclocal.m4 configure Grammar Lib Makefile.pre.in Objects PCbuild Python Tools
  7. config.guess configure.ac Include LICENSE Misc Parser Programs README.rst
  8. config.sub Doc install-sh Mac Modules PC pyconfig.h.in setup.py

041 编辑配置

  1. [root@chery Python-3.6.8]# ./configure --prefix=/opt/python3
  2. checking build system type... x86_64-pc-linux-gnu
  3. checking host system type... x86_64-pc-linux-gnu
  4. .......

—prefix=/opt/python3

If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure —enable-optimizations
如果您希望发布版本并且所有稳定优化都处于活动状态(PGO等),

042 开始编译

  1. [root@chery Python-3.6.8]# make
  2. ## 如果没有问题就执行下面的
  3. [root@chery Python-3.6.8]# make install

05 配置python3

050添加全局变量(推荐)

当然也可以用软连接(但是需要连接python3 和pip3),不推荐

  1. [root@chery Python-3.6.8]# ls
  2. aclocal.m4 config.sub Include Mac Modules Programs Python setup.py
  3. build configure install-sh Makefile Objects pybuilddir.txt python-config Tools
  4. config.guess configure.ac Lib Makefile.pre Parser pyconfig.h python-config.py
  5. config.log Doc libpython3.6m.a Makefile.pre.in PC pyconfig.h.in python-gdb.py
  6. config.status Grammar LICENSE Misc PCbuild python README.rst
  7. [root@chery Python-3.6.8]# cd /opt/python3/
  8. [root@chery python3]# ls
  9. bin include lib share
  10. [root@chery python3]# cd bin/
  11. [root@chery bin]# ls
  12. 2to3 easy_install-3.6 idle3.6 pip3.6 pydoc3.6 python3.6 python3.6m python3-config pyvenv-3.6
  13. 2to3-3.6 idle3 pip3 pydoc3 python3 python3.6-config python3.6m-config pyvenv
  1. [root@chery bin]# echo 'export PATH=/opt/python3/bin:$PATH' >> /etc/profile
  2. [root@chery bin]# tail -1 /etc/profile
  3. export PATH=/opt/python3/bin:$PATH
  4. [root@chery bin]# source /etc/profile
  5. [root@chery bin]# echo $PATH
  6. /opt/python3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
  1. [root@chery bin]# python3
  2. Python 3.6.8 (default, Jan 9 2019, 10:20:25)
  3. [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>>
  6. >>> exit();
  7. [root@chery bin]# ls
  8. 2to3 easy_install-3.6 idle3.6 pip3.6 pydoc3.6 python3.6 python3.6m python3-config pyvenv-3.6
  9. 2to3-3.6 idle3 pip3 pydoc3 python3 python3.6-config python3.6m-config pyvenv
  10. [root@chery bin]# pip3 -V
  11. pip 18.1 from /opt/python3/lib/python3.6/site-packages/pip (python 3.6)

懒人复制区(中途出现问题自己检查!)

首先要安装依赖

yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-develncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y

开启复制模式

  1. mkdir -p /root/tools
  2. cd /root/tools
  3. wget https://npm.taobao.org/mirrors/python/3.6.8/Python-3.6.8.tgz
  4. tar -xf Python-3.6.8.tgz
  5. cd Python-3.6.8
  6. ./configure --prefix=/opt/python3
  7. make && make install
  8. cd /opt/python3/bin/
  9. echo 'export PATH=/opt/python3/bin:$PATH' >> /etc/profile
  10. tail -1 /etc/profile
  11. source /etc/profile
  12. echo $PATH
  13. python3

国内pip源

https://mirrors.tuna.tsinghua.edu.cn/help/pypi/

临时使用

  1. pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

注意,simple 不能少, 是 https 而不是 http

设为默认

升级 pip 到最新的版本 (>=10.0.0) 后进行配置:

  1. pip install pip -U
  2. pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple