linux安装python3
--linux安装python3
centos系统本身默认安装有python2.x,可通过python --version 查看系统自带的python版本,有一些系统命令需要用到python2,安装python3时不能卸载python2。
1、安装依赖包
1)首先安装gcc编译器,gcc有些系统版本已经默认安装,通过 gcc --version 查看,没安装的先安装gcc,yum -y install gcc
2)安装其它依赖包,(注:不要缺少,否则有可能安装python出错,python3.7.0以下的版本可不装 libffi-devel )
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
2)下载
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
3)、解压Python-3.7.0.tgz
tar -zxvf Python-3.7.0.tgz
4、建立一个空文件夹,用于存放python3程序
mkdir /usr/local/python3
5、执行配置文件,编译,编译安装
cd Python-3.7.0 ./configure --prefix=/usr/local/python3 make && make install
安装完成没有提示错误便安装成功了
7、建立软连接
ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3 ln -s /usr/local/python3/bin/pip3.7 /usr/bin/pip3
8、测试一下python3是否可以用
[root@mini Python-3.7.0]# python3 Python 3.7.0 (default, Jul 28 2018, 22:47:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> print("hello world!") hello world! >>> exit() [root@mini Python-3.7.0]# pip3 --version pip 10.0.1 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)
可以看到python3.7.0可以正常使用
--##导入模块时提示ModuleNotFoundError: No module named '_bz2'
需在python3中添加对zlib和bz2的支持
yum install zlib zlib-devel
yum install bzip2 bzip2-devel
重新编译安装
make install
--##导入模块时提示 Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
需要安装:
yum install xz-devel
yum install python-backports-lzma
pip install backports.lzma
重新编译安装
make install
--##导入模块时提示:ModuleNotFoundError: No module named ‘_ctypes‘
需要安装 libffi-devel
yum install libffi-devel -y
重新编译安装
make install
--##python命令行按删除键时会出现 ^H 字符,无法正常删除
安装 readline 相关模块
# yum install readline readline-devel
安装完成后,需要再重新编译安装 Python:
# make install