Python入门教程笔记
Python编程入门
本系列笔记准备从零基础开始记录python的技术细节和感想。
环境配置
操作系统:Linux系统
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.7.1908 (Core)
Release: 7.7.1908
Codename: Core
备注:root权限,所有操作都是在root用户下操作。
安装Python 3
Linux系统下有多种安装方式:Anaconda安装、命令行安装和源码安装。根据自己需要自行选择。
这里选择源码安装,安装Python 3.8版本。
- 安装编译python源码时,需要一些依赖包:
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel
- 下载工具wget,用于下载python源码:
yum install wget
- 下载对应的python版本:
wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
- 解压压缩包并安装:
# 解压压缩包
tar -zxvf Python-3.8.2.tgz
# 进入文件夹
cd Python-3.8.2
# 配置安装位置
./configure prefix=/usr/local/python3
# 安装
make && make install
如果需要确认版本编译成功且是否安装正确,可以通过ll或ls命令查看python3目录是否生成。
[root@iZ2ze9ftqv2b85gk7iyhp1Z ~]# cd /usr/local
[root@iZ2ze9ftqv2b85gk7iyhp1Z local]# ll
total 76
drwxr-xr-x 6 root root 4096 Apr 21 21:41 aegis
drwxr-xr-x. 2 root root 4096 Dec 12 18:30 bin
drwxr-xr-x 6 root root 4096 Dec 12 18:22 curl
drwxr-xr-x. 2 root root 4096 Apr 11 2018 etc
drwxr-xr-x 6 root root 4096 Dec 12 18:22 freetype
drwxr-xr-x. 2 root root 4096 Apr 11 2018 games
drwxr-xr-x. 3 root root 4096 Dec 12 18:21 include
drwxr-xr-x. 3 root root 4096 Dec 12 18:21 lib
drwxr-xr-x. 2 root root 4096 Apr 11 2018 lib64
drwxr-xr-x. 2 root root 4096 Apr 11 2018 libexec
drwxr-xr-x 9 root root 4096 Dec 12 18:18 mysql
drwxr-xr-x 11 root root 4096 Dec 12 18:21 nginx
drwxr-xr-x 9 root root 4096 Dec 12 18:18 openssl
drwxr-xr-x 9 root root 4096 Dec 12 18:29 php
drwxr-xr-x 6 root root 4096 May 7 20:14 python3
drwxr-xr-x 5 root root 4096 Dec 12 18:30 redis
drwxr-xr-x. 2 root root 4096 Apr 11 2018 sbin
drwxr-xr-x. 9 root root 4096 Dec 12 18:21 share
drwxr-xr-x. 2 root root 4096 Apr 11 2018 src
- 添加软连接:
#添加python3的软链接
ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3
#添加 pip3 的软链接
ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip3
测试验证安装结果
当安装过程完成后,在命令行界面下测试python3和pip3是否安装成功:
[root@iZ2ze9ftqv2b85gk7iyhp1Z ~]# python3
Python 3.8.2 (default, May 7 2020, 20:13:40)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@iZ2ze9ftqv2b85gk7iyhp1Z ~]# pip3 -V
pip 19.2.3 from /usr/local/python3/lib/python3.8/site-packages/pip (python 3.8)
如果命令行界面执行以上命令出现上面的提示,证明python3和pip3已经安装成功。