1、源码安装
1、安装依赖软件包
dnf -y install gcc gcc-c++ zlib-devel bzip2-devel openssl-devel sqlite-devel readline-devel libffi-devel # python3.7版本安装
前提条件是:你的系统已经安装了开发工具包。
2、下载
curl -o python3.8.0.tgz https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz
// 或者
wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz
3、解压安装
tar -xf Python-3.8.0.tgz
cd Python-3.8.0/
开始编译安装
./configure --enable-shared --prefix=/usr/local/python3
make -j 2 && make install
–enable-shared 指定安装共享库,共享库在使用其他需调用python的软件时会用到,比如使用
mod_wgsi
连接Apache与python时需要。
4、配置共享库文件
为所有用户设置共享库目录
用 vim 编辑器打开配置文件 /etc/profile.d/python3.sh
vim /etc/profile.d/python3.sh
在文件末尾写上如下内容:
# python3.6 共享库目录
export PATH=$PATH:/usr/local/python3/bin
编辑文件 /etc/ld.so.conf.d/python3.conf
,并且添加如下内容:
/usr/local/python3/lib
保存退出文件后, 执行如下命令 加载配置信息使其生效
ldconfig
执行如下命令,使环境变量生效
source /etc/profile.d/python3.sh
5、测试python3
$ python3
Python 3.6.3 (default, Dec 17 2017, 04:11:01)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('qf')
qf
>>> exit()
输入
exit()
即可退出 python3
6、测试 pip3
一般情况下你不需要执行下面的安装命令。
[root@newrain ~]# pip3 -V
pip 9.0.3 from /usr/local/lib/python3.6/site-packages (python 3.6)
假如上面显示的含有 python3.6 就没问题了,说名 pip3 安装的模块会安装到上面显示的目录下
特殊问题
假如 Python Shell 中敲击方向键显示「[[C[[D」
可以安装以下包
一般情况下你不需要执行下面的安装命令。
pip3 install gnureadline
2、 配置使用国内源安装第三方模块
1、创建配置文件
配置 pip3 使用国内源
mkdir ~/.pip
vi ~/.pip/pip.conf
# Windows 下使用 pip.ini
(1):在windows文件管理器中,输入 %APPDATA%
(2):会定位到一个新的目录下,在该目录下新建pip文件夹,然后到pip文件夹里面去新建个pip.ini文件
写入如下内容:
[global]
timeout = 6000
index-url=https://mirrors.aliyun.com/pypi/simple
豆瓣源: https://pypi.douban.com/simple/
阿里源: https://mirrors.aliyun.com/pypi/simple
清华: https://pypi.tuna.tsinghua.edu.cn/simple
阿里云: http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学: http://pypi.hustunique.com/
山东理工大学: http://pypi.sdutlinux.org/
示例:
比如安装一个执行远程主机命令的模块
[root@newrain ~]# pip3 install gnureadline
Looking in indexes: https://mirrors.aliyun.com/pypi/simple
Collecting gnureadline
Downloading https://mirrors.aliyun.com/pypi/packages/2b/36/60b53a1793af9a60539b5ee6fed4f3702280bd5a88ab41600a51510002a1/gnureadline-8.0.0-cp37-cp37m-manylinux1_x86_64.whl (283kB)
100% |████████████████████████████████| 286kB 387kB/s
Installing collected packages: gnureadline
Successfully installed gnureadline-8.0.0
You are using pip version 19.0.3, however version 19.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command. # 最后两行为警告内容,无需理会