若本机已安装python2,尽量不要动现有的python2,额外安装python3即可。
1.安装python3.6(centos下安装python3自带pip和setuptools)
# 安装依赖环境
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
[root@ipha-dev71-1 python3]# cd /usr/local/python3 # 我的python3.6安装路径,如果没有python3目录应事先创建
# 依次执行如下命令
[root@ipha-dev71-1 python3]# wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz
[root@ipha-dev71-1 python3]# tar -zxvf Python-3.6.1.tgz
[root@ipha-dev71-1 python3]# /configure --prefix=/usr/local/python3 # 指定安装路径
[root@ipha-dev71-1 python3]# make
[root@ipha-dev71-1 python3]# make install
[root@ipha-dev71-1 python3]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3 # 建立软链接,同windows下的桌面快捷方式,可在/usr/bin 下可查看软连接
[root@ipha-dev71-1 bin]# vim ~/.bash_profile # 配置环境变量,~/.bash_profile指用户变量,/etc/profile指系统变量
添加如下内容
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
export PATH
[root@ipha-dev71-1 bin]# source ~/.bash_profile # 使环境变量生效
[root@ipha-dev71-1 bin]# # ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 # 为pip3也创建软连接
# 检测是否安装成功
[root@ipha-dev71-1 bin]# python3 -V
Python 3.6.1
[root@ipha-dev71-1 bin]# pip3 -V
pip 19.2.1 from /usr/local/python3/lib/python3.6/site-packages/pip (python 3.6)
2.安装第三方库(这里仅举个例子,其他第三方库可在用到时再进行安装)
[root@ipha-dev71-1 bin]# pip3 install configparser Collecting configparser Downloading https://files.pythonhosted.org/packages/09/4c/5e3ddf3cf1b457a25d48dc7253d0887c1037d79881942df5edd99cec4aca/configparser-3.7.5-py2.py3-none-any.whl Installing collected packages: configparser Successfully installed configparser-3.7.5
[root@ipha-dev71-1 report]# pip3 install pymysql
Collecting pymysql
Downloading https://files.pythonhosted.org/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl (47kB)
|████████████████████████████████| 51kB 48kB/s
Installing collected packages: pymysql
Successfully installed pymysql-0.9.3
pip list #查看已安装模块
pip search 模块名 #搜索模块
3.linux查看python的几种方式
[root@ipha-dev71-1 python3]# whereis python3 python3: /usr/bin/python3 /usr/local/python3 [root@ipha-dev71-1 python3]# which python3 /bin/python3 [root@ipha-dev71-1 python3]# find / -name python3 /usr/bin/python3 /usr/local/python3 /usr/local/python3/Python-3.6.1/build/temp.linux-x86_64-3.6/usr/local/python3 /usr/local/python3/bin/python3
4.进入或退出python终端
[root@ipha-dev71-1 site-packages]# python3 Python 3.6.1 (default, Jan 25 2019, 14:39:36) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> exit() # ctrl+d 也可以推出
5.升级pip3
[root@ipha-dev71-1 yysoft]# pip3 install --upgrade pip
6.执行python文件
[root@ipha-dev71-1 site-packages]# cd /root/.jenkins/workspace/python_proj [root@ipha-dev71-1 python_proj]# ll total 12 drwxr-xr-x 3 root root 84 Aug 10 13:32 config -rw-r--r-- 1 root root 1293 Aug 10 13:23 login.py -rw-r--r-- 1 root root 1007 Aug 10 13:23 request.py -rw-r--r-- 1 root root 920 Aug 10 13:23 run_method.py [root@ipha-dev71-1 python_proj]# python3 run_method.py {'code': '200', 'message': '登陆成功', 'data': {'uid': 13644, 'username': 'wangmm', 'password': None, 'userdesc': None, 'realname': '王萌萌', 'email': None, 'sex': None, 'age': None, 'officeQualification': None, 'title': None, 'zoneId': 331, 'zoneName': '南京鼓楼医院', 'state': None, 'createTime': None, 'createUserId': None, 'updateTime': None, 'updateUserId': None, 'endTime': None, 'lastLoginTime': None, 'remark': None, 'phone': None, 'py': None, 'datareportUser': None, 'company': None, 'userType': None, 'worknum': None, 'source': None}, 'token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZWFsTmFtZSI6IueOi-iQjOiQjCIsImlzcyI6ImlwaGFybWFjYXJlX2lzc3VlciIsInpvbmVJZCI6MzMxLCJleHAiOjE1NjU4MTc3MDksInVzZXJJZCI6MTM2NDQsImp0aSI6IndhbmdtbSJ9.msG4Spkmz0hk97vopJUJxSiad3qkvTdE2zHB8P1I6k8', 'refreshToken': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZWFsTmFtZSI6IueOi-iQjOiQjCIsImlzcyI6ImlwaGFybWFjYXJlX2lzc3VlciIsInpvbmVJZCI6MzMxLCJleHAiOjE1NjYxMTc3MDksInVzZXJJZCI6MTM2NDQsImp0aSI6IndhbmdtbSJ9.y3FNyspEgbXonY1FaCewdIAATYEwnWGYJu4-h2XataU', 'systemIsActive': 0, 'isXtss': None}