Python 环境配置——源码安装python3.6 pip3

1.系统

  666@66:/proc$ cat /proc/version
  Linux version 4.4.0-128-generic (buildd@lgw01-amd64-055) (gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.4) )

2.安装

系统默认安装了python2.7,这里演示安装python3.6。

1. 从python官网下载python3.6.5 Source release版本

Python 环境配置——源码安装python3.6 pip3_第1张图片
download.png

2. 解压源码安装

  • 解压得到Python-3.6.5文件夹
  kjs@42:~/下载$ xz -d Python-3.6.5.tar.xz
  kjs@42:~/下载$ tar -xvf Python-3.6.5.tar
  • 进入文件夹,进行编译(指定目标文件夹,pip 的时候可能缺少 ssl,所以这里配置的时候加上)
  kjs@42:~/下载/Python-3.6.5$ ./configure --prefix=/usr/local/python-3.6 --with -ssl
  • 编译及安装
  kjs@42:~/下载/Python-3.6.5$ make
  kjs@42:~/下载/Python-3.6.5$ sudo make install

3.检查python pip版本 并创建软链接到 /usr/bin下

  • 检查版本
  kjs@42:~/下载/Python-3.6.5$ cd /usr/local/python-3.6/bin
  kjs@42:/usr/local/python-3.6/bin$ ./python3
  Python 3.6.5 (default, Jul  5 2018, 14:32:19) 
  [GCC 4.8.4] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  >>> exit()
  kjs@42:/usr/local/python-3.6/bin$ ./pip3 -V
  pip 10.0.1 from /usr/local/python-3.6/lib/python3.6/site-packages/pip (python 3.6)
  kjs@42:/usr/local/python-3.6/bin$ 

  • 创建软链接
  kjs@42:/usr/local/python-3.6/bin$ sudo ln -s python3 /usr/bin/python3
  kjs@42:/usr/local/python-3.6/bin$ sudo ln -s pip3 /usr/bin/pip3
  • 检查
  kjs@42:~$ python3
  Python 3.6.5 (default, Jul  5 2018, 14:32:19) 
  [GCC 4.8.4] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  >>> exit()
  kjs@42:~$ pip3 -V
  pip 10.0.1 from /usr/local/python-3.6/lib/python3.6/site-packages/pip (python 3.6)
  kjs@42:~$ 

版本没有问题,执行pip3 install 试试

Python 环境配置——源码安装python3.6 pip3_第2张图片
error.jpg

问题原因:python3.6.5不支持lsb_release -a命令
解决办法:系统自带的python3.4支持该命令
修改 lsb_release 文件

  kjs@42:~$ vim /usr/bin/lsb_release

将第一行的 python3 改成相应的python版本

  1 #! /usr/bin/python3.4.4.4.4 -Es
  2 
  3 # lsb_release command for Debian
  4 # (C) 2005-10 Chris Lawrence 
  5 
  6 #    This package is free software; you can redistribute it and/or modify
  7 #    it under the terms of the GNU General Public License as published by
  8 #    the Free Software Foundation; version 2 dated June, 1991.
  9 
 10 #    This package is distributed in the hope that it will be useful,
 11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 #    GNU General Public License for more details.
 14 
 15 #    You should have received a copy of the GNU General Public License
 16 #    along with this package; if not, write to the Free Software
 17 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 18 #    02110-1301 USA
 19 
 20 # Python3-compatible print() function
 21 from __future__ import print_function
 22 
 23 from optparse import OptionParser

执行 pip3 install request 即可成功

你可能感兴趣的:(Python 环境配置——源码安装python3.6 pip3)