python自动化脚本安装指定版本python环境详解
作者:Lockeyi 时间:2022-08-14 11:31:46
一般情况下编译安装python环境需要执行以下步骤:
下载源码包
解压源码包
安装配置
编译以及编译安装
TALK IS CHEAP, SHOW YOU MY CODE.
#!/usr/bin/python
#coding:utf-8
'''
date:9/2/17 18:03 PM
author:lockey
email:lockey@123.com
desc:python自动化安装用户指定版本的python环境
'''
#导入Python的系统编程操作模块
import os
#导入用来处理Python运行时配置以及资源,与前当程序之外的系统环境交互的模块
import sys
#判断当前用户是否是root用户
if os.getuid() == 0:
pass
else:
print 'Not under root mode, please switch user!'
sys.exit(1)
#获取用户输入的python安装版本
version = raw_input('Please input wanted python version(2.7/3.6)')
#如果嫌官网下载速度太慢可以指定python3.6.2的链接地址为本人上传资源地址
#根据用户输入的python版本选择源码包下载地址
if version == '2.7':
url = 'https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz'
elif version == '3.6':
url = 'https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz'
else:
print 'Please input given version number(2.7/3.5)'
sys.exit(1)
#拼接源码包下载地址并执行下载命令
cmd = 'wget ' + url
res = os.system(cmd)
if res != 0 :
print 'Failed to download python source package, please inspect your network!'
sys.exit(1)
if version == '2.7':
package_version = 'Python-2.7.13'
else:
package_version = 'Python-3.6.2'
#解压下载的源码包
cmd = 'tar xf ' + package_version + '.tgz'
res = os.system(cmd)
#如果解压失败则删除下载的源码包并且提示用户重新执行脚本
if res != 0:
os.system('rm ' + package_version + '.tgz')
print 'Please reexcute the script to install python'
sys.exit(1)
#解压成功则进入解压后的源码目录中依次执行配置、编译、安装过程
cmd = 'cd ' + package_version + ' && ./configure --prefix=/usr/local/python && make && make install'
res = os.system(cmd)
#安装失败则提示用户安装失败了,让用户检查环境依赖
if res != 0:
print 'Failed to install python, please inspect dependencies for python install!'
sys.exit(1)
程序运行测试截图:
1. 测试普通用户模式下运行安装脚本:
2. 切换到root用户然后运行自动安装脚本:
3. 脚本自动配置(./configure)完成,进入编译(make)阶段
4. 脚本编译安装完成:
5. 测试安装的python环境:
来源:http://blog.csdn.net/lockey23/article/details/77802967
标签:python,环境
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python模块导入的方法
2023-08-23 06:54:38
分享Python 加速运行技巧
2022-07-07 12:48:04
Pygame实战之实现经典外星人游戏
2021-05-10 00:31:44
![](https://img.aspxhome.com/file/2023/3/90593_0s.gif)
解决pyinstaller打包exe文件出现命令窗口一闪而过的问题
2021-09-28 01:03:45
![](https://img.aspxhome.com/file/2023/6/107476_0s.jpg)
js全选/全不选/反选 checkbox代码
2008-03-18 13:00:00
Python中选择结构实例讲解
2023-06-26 14:56:09
Python如何使用print()函数输出格式化字符串
2021-03-09 22:43:41
![](https://img.aspxhome.com/file/2023/5/86345_0s.png)
python中str内置函数用法总结
2022-06-23 10:22:45
php防止sql注入示例分析和几种常见攻击正则表达式
2023-11-23 17:01:02
Python脚本暴力破解栅栏密码
2022-02-09 18:31:11
基于Python编写微信清理工具的示例代码
2022-03-04 09:02:54
![](https://img.aspxhome.com/file/2023/1/112821_0s.jpg)
thinkPHP中配置的读取与C方法详解
2023-11-14 17:12:35
网页设计中的对比原则
2010-03-20 21:50:00
![](https://img.aspxhome.com/file/UploadPic/20103/20/01-91s.jpg)
酷! 程序员用Python带你玩转冲顶大会
2022-02-20 11:56:08
![](https://img.aspxhome.com/file/2023/1/91941_0s.jpg)
JavaScript缓动库
2009-05-25 12:50:00
django 基于中间件实现限制ip频繁访问过程详解
2022-06-24 08:38:43
http通过StreamingHttpResponse完成连续的数据传输长链接方式
2023-06-04 03:33:45
Python中那些 Pythonic的写法详解
2023-09-14 20:01:59
matplotlib绘制折线图的基本配置(万能模板案例)
2022-09-06 18:06:17
![](https://img.aspxhome.com/file/2023/3/97663_0s.png)
Python爬取国外天气预报网站的方法
2022-02-22 00:39:07