python 安装包下载的页面: https://www.python.org/downloads/source/
选择stable版本, Gzipped source tarball 格式的下载包(也就是tar.gz格式),在选好的安装包,鼠标右键可以看到复制链接
笔者看到的最新的版本是https://www.python.org/ftp/python/3.7.7/Python-3.7.7.tgz
我们去服务器中运行这个安装脚本
touch install_python3.sh
复制下面的代码到install_python3.sh中
该安装脚本做了一定的兼容,可以反复安装
安装python3, python默认是python3
#!/bin/bash version=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)..*/1/'` if [[ "$version" == 7 ]]; then echo "this is centos 7"; yum install python-devel mysql-devel zlib-devel openssl-devel mysql-python libffi-devel -y cd /usr/local/src wget -O Python-3.7.7.tgz https://www.python.org/ftp/python/3.7.7/Python-3.7.7.tgz tar zxf Python-3.7.7.tgz cd Python-3.7.7 ./configure --prefix=/usr/local/python3 make make install mv /usr/bin/python /usr/bin/python2 ln -sf /usr/local/python3/bin/python3.7 /usr/bin/python ln -sf /usr/local/python3/bin/python3.7 /usr/bin/python3 ln -sf /usr/local/python3/bin/pip3.7 /usr/bin/pip ln -sf /usr/local/python3/bin/pip3.7 /usr/bin/pip3 sed -i '1d' /usr/bin/yum sed -i '1i#!/usr/bin/python2.7' /usr/bin/yum sed -i '1d' /usr/libexec/urlgrabber-ext-down sed -i '1i#!/usr/bin/python2.7' /usr/libexec/urlgrabber-ext-down fi python -V
安装python3, python默认是python2
#!/bin/bash version=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)..*/1/'` if [[ "$version" == 7 ]]; then echo "this is centos 7"; yum install python-devel mysql-devel zlib-devel openssl-devel mysql-python libffi-devel -y cd /usr/local/src wget -O Python-3.7.7.tgz https://www.python.org/ftp/python/3.7.7/Python-3.7.7.tgz tar zxf Python-3.7.7.tgz cd Python-3.7.7 ./configure --prefix=/usr/local/python3 make make install # mv /usr/bin/python /usr/bin/python2 # ln -sf /usr/local/python3/bin/python3.7 /usr/bin/python ln -sf /usr/local/python3/bin/python3.7 /usr/bin/python3 # ln -sf /usr/local/python3/bin/pip3.7 /usr/bin/pip ln -sf /usr/local/python3/bin/pip3.7 /usr/bin/pip3 # sed -i '1d' /usr/bin/yum # sed -i '1i#!/usr/bin/python2.7' /usr/bin/yum # sed -i '1d' /usr/libexec/urlgrabber-ext-down # sed -i '1i#!/usr/bin/python2.7' /usr/libexec/urlgrabber-ext-down fi python -V
chmod 755 install_python3.sh
./install_python3.sh
安装结束
创建python虚拟环境
https://www.cnblogs.com/liuyansheng/p/6141197.html
1、安装virtualenv
yum install python-virtualenv
2、创建python虚拟环境
virtualenv env # 执行后,在本地会生成一个叫env的文件夹,env 可以换成其他的阿毛或者阿狗,如virtualenv myblog
**如果你的系统里安装有不同版本的python,可以使用--python参数指定虚拟环境的python版本:**
virtualenv --python=/usr/bin/python3 env
3、启动虚拟环境
source env/bin/activate # 启动虚拟环境
deactivate # 退出虚拟环境
4. 在脚本顶部添加以下命令让Python脚本可以像SHELL脚本一样可直接执行
#! /usr/bin/env python3