目前python的最热的两大版本,python2与python3;
做数据分析,常用的是python3,用python3开发的程序部署到服务器上时,要求服务器上的python环境也是python3。
Linux服务器一般默认自带python2,建议不要做卸载
gcc是一个用于linux系统下编程的编译器
linux下的python3需要使用编译安装,所以我们需要安装gcc
查看一下系统中gcc:
#常用编译安装所需要的依赖包
[root@zeny ~]# 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
#python版本确认
[root@zeny ~]# python --version
Python 2.7
#python3安装包下载
[root@zeny ~]# cd opt/
[root@zeny ~]# wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz
#创建安装目录
[root@zeny ~]# mkdir -p /usr/local/python3
#安装包python3解压
[root@zeny ~]# cd /opt/
[root@zeny opt]# tar -zxvf Python-3.7.1.tgz
[root@zeny opt]# ll |grep Python
drwxr-xr-x 19 501 501 4096 Aug 6 01:43 Python-3.7.1
-rw-r--r-- 1 root root 22802018 Oct 20 2018 Python-3.7.1.tgz
#编译安装Python3
[root@zeny ~]# cd /opt/Python-3.7.1
[root@zeny Python-3.7.1]# ll
total 56660
-rw-r--r-- 1 501 501 10953 Oct 20 2018 aclocal.m4
drwxr-xr-x 6 root root 107 Aug 6 01:39 build
-rw-r--r-- 1 501 501 631 Oct 20 2018 CODE_OF_CONDUCT.rst
-rwxr-xr-x 1 501 501 44166 Oct 20 2018 config.guess
-rw-r--r-- 1 root root 908527 Aug 6 01:35 config.log
-rwxr-xr-x 1 root root 40763 Aug 6 01:35 config.status
-rwxr-xr-x 1 501 501 36251 Oct 20 2018 config.sub
-rwxr-xr-x 1 501 501 495052 Oct 20 2018 configure
-rw-r--r-- 1 501 501 165352 Oct 20 2018 configure.ac
drwxr-xr-x 18 501 501 4096 Oct 20 2018 Doc
drwxr-xr-x 2 501 501 21 Oct 20 2018 Grammar
drwxr-xr-x 3 501 501 4096 Oct 20 2018 Include
-rwxr-xr-x 1 501 501 7122 Oct 20 2018 install-sh
drwxr-xr-x 34 501 501 8192 Aug 6 01:37 Lib
-rw-r--r-- 1 root root 39914012 Aug 6 01:37 libpython3.7m.a
-rw-r--r-- 1 501 501 12763 Oct 20 2018 LICENSE
drwxr-xr-x 2 501 501 33 Oct 20 2018 m4
drwxr-xr-x 8 501 501 160 Oct 20 2018 Mac
-rw-r--r-- 1 root root 71234 Aug 6 01:35 Makefile
-rw-r--r-- 1 root root 62294 Aug 6 01:35 Makefile.pre
-rw-r--r-- 1 501 501 62613 Oct 20 2018 Makefile.pre.in
drwxr-xr-x 2 501 501 4096 Aug 6 01:35 Misc
drwxr-xr-x 13 501 501 8192 Aug 6 01:39 Modules
drwxr-xr-x 4 501 501 4096 Aug 6 01:37 Objects
drwxr-xr-x 3 501 501 4096 Aug 6 01:40 Parser
drwxr-xr-x 6 501 501 4096 Oct 20 2018 PC
drwxr-xr-x 2 501 501 4096 Oct 20 2018 PCbuild
-rw-r--r-- 1 root root 0 Aug 6 01:35 profile-clean-stamp
-rw-r--r-- 1 root root 0 Aug 6 01:39 profile-gen-stamp
drwxr-xr-x 2 501 501 179 Aug 6 01:47 Programs
-rw-r--r-- 1 root root 26 Aug 6 01:37 pybuilddir.txt
-rw-r--r-- 1 root root 45191 Aug 6 01:10 pyconfig.h
-rw-r--r-- 1 501 501 43134 Oct 20 2018 pyconfig.h.in
-rwxr-xr-x 1 root root 15798928 Aug 6 01:37 python
drwxr-xr-x 3 501 501 8192 Aug 6 01:37 Python
-rw-r--r-- 1 root root 3097 Aug 6 01:39 python-config
-rw-r--r-- 1 root root 2042 Aug 6 01:39 python-config.py
-rw-r--r-- 1 root root 64614 Aug 6 01:14 python-gdb.py
-rw-r--r-- 1 501 501 10097 Oct 20 2018 README.rst
-rw-r--r-- 1 501 501 101855 Oct 20 2018 setup.py
drwxr-xr-x 23 501 501 322 Oct 20 2018 Tools
#「指定安装目录,设置启用ssl功能,直接添加参数:--with-ssl」
[root@zeny Python-3.7.1]# ./configure --prefix=/usr/local/python3
[root@zeny Python-3.7.1]# make && make install
#测试Pthon3是否成功
[root@zeny Python-3.7.1]# /usr/local/python3/bin/python3.7
#创建软连接(-f,如创建不成功,可以添加此参数直接强制覆盖)
[root@zeny Python-3.7.1]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@zeny Python-3.7.1]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
配置PATH
#加入环境变量(PATH)
[root@zeny Python-3.7.1]# vim /etc/profile (直接添加如下内容即可)
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
export PATH
#配置快速生效
[root@zeny Python-3.7.1]# source ~/.bash_profile
#安装setuptools
[root@zeny ~]# cd /opt/
[root@zeny opt]# wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz
--no-check-certificate :意味着以不安全的方法连接
##下载文件的md5校验码:c607dd118eae682c44ed146367a17e26
#解压安装
[root@zeny opt]# tar -zxvf setuptools-19.6.tar.gz
[root@zeny opt]# cd /opt/setuptools-19.6/
#编译并安装
[root@zeny setuptools-19.6]# python3 setup.py build
[root@zeny setuptools-19.6]# python3 setup.py install
#软连接创建
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
Python3环境检查
#查看Python版本
[root@zeny ~]# python3 -V
Python 3.7.1
[root@zeny ~]# pip3 -V
pip 10.0.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
Python3模块: requests
#直接使用python安装库pip安装requests(出现以下错需要更新到最新的pip版本即可)
[root@zeny ~]# pip install requests
Collecting requests
Downloading https://files.pythonhosted.org/packages/ca/91/6d9b8ccacd0412c08820f72cebaa4f0c0441b5cda699c90f618b6f8a1b42/requests-2.28.1-py3-none-any.whl (62kB)
100% |████████████████████████████████| 71kB 966kB/s
Collecting urllib3<1.27,>=1.21.1 (from requests)
Downloading https://files.pythonhosted.org/packages/d1/cb/4783c8f1a90f89e260dbf72ebbcf25931f3a28f8f80e2e90f8a589941b19/urllib3-1.26.11-py2.py3-none-any.whl (139kB)
100% |████████████████████████████████| 143kB 1.7MB/s
Collecting charset-normalizer<3,>=2 (from requests)
Downloading https://files.pythonhosted.org/packages/94/69/64b11e8c2fb21f08634468caef885112e682b0ebe2908e74d3616eb1c113/charset_normalizer-2.1.0-py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests)
Downloading https://files.pythonhosted.org/packages/e9/06/d3d367b7af6305b16f0d28ae2aaeb86154fa91f144f036c2d5002a5a202b/certifi-2022.6.15-py3-none-any.whl (160kB)
100% |████████████████████████████████| 163kB 6.0MB/s
Collecting idna<4,>=2.5 (from requests)
Downloading https://files.pythonhosted.org/packages/04/a2/d918dcd22354d8958fe113e1a3630137e0fc8b44859ade3063982eacd2a4/idna-3.3-py3-none-any.whl (61kB)
100% |████████████████████████████████| 61kB 8.6MB/s
Installing collected packages: urllib3, charset-normalizer, certifi, idna, requests
Successfully installed certifi-2022.6.15 charset-normalizer-2.1.0 idna-3.3 requests-2.28.1 urllib3-1.26.11
You are using pip version 10.0.1, however version 22.2.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
#更新pip版本
[root@zeny ~]# pip install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/1f/2c/d9626f045e7b49a6225c6b09257861f24da78f4e5f23af2ddbdf852c99b8/pip-22.2.2-py3-none-any.whl (2.0MB)
100% |████████████████████████████████| 2.0MB 724kB/s
Installing collected packages: pip
Found existing installation: pip 10.0.1
Uninstalling pip-10.0.1:
Successfully uninstalled pip-10.0.1
Successfully installed pip-22.2.2
#继续安装requsts模块
[root@zeny ~]# pip install requests
Requirement already satisfied: requests in /usr/local/lib/python3.7/site-packages (2.28.1)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.7/site-packages (from requests) (3.3)
Requirement already satisfied: charset-normalizer<3,>=2 in /usr/local/lib/python3.7/site-packages (from requests) (2.1.0)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.7/site-packages (from requests) (1.26.11)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/site-packages (from requests) (2022.6.15)
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
Python3模块: xlrd
注:使用python3进行执行脚本,由于xlrd新的模块(2.0.1版本)已不支持.xlsx文件,我们就需要卸载当前版本的xlrd库,安装就版本的1.2.0版本。
#卸载删除新版xlrd模块
[root@zabbix01 zeny]# pip uninstall xlrd
WARNING: Skipping xlrd as it is not installed.
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#安装指定版本的xlrd模块
[root@zabbix01 zeny]# pip install xlrd==1.2.0
Collecting xlrd==1.2.0
Downloading xlrd-1.2.0-py2.py3-none-any.whl (103 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 103.3/103.3 kB 1.1 MB/s eta 0:00:00
Installing collected packages: xlrd
Successfully installed xlrd-1.2.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv