首页 > 编程语言 >自动化脚本:一键安装python自定义版本

自动化脚本:一键安装python自定义版本

时间:2023-04-17 17:58:33浏览次数:48  
标签:自定义 desired python 一键 try Python version file download

 

1:环境:

  • centos 7
  • python2.7

2:脚本内容:

#!/usr/bin/env python
import os
import sys
import requests
import tarfile
import shutil
import subprocess

# Install necessary packages
try:
    subprocess.check_call(["yum", "install", "-y", "wget", "gcc", "make", "openssl-devel", "bzip2-devel", "zlib-devel", "libffi-devel"])
except subprocess.CalledProcessError:
    print("Failed to install necessary packages. Please check your system configuration and try again.")
    sys.exit(1)

# Step 1: Check for root privileges
if os.geteuid() != 0:
    print("This script must be run as root!")
    sys.exit(1)

# Step 2: Check if the desired version is already installed
current_version = sys.version_info
if current_version >= (3, 10):
    print("You already have the latest version of Python ({0}.{1}).".format(current_version.major, current_version.minor))
    sys.exit(0)

# Step 3: Prompt user for desired version and download it
desired_version = raw_input("Please enter the version of Python you want to install (e.g., '3.9.7'): ")
download_url = "https://mirrors.huaweicloud.com/python/" + str(desired_version) + "/Python-" + str(desired_version) + ".tgz"
download_file = "Python-" + str(desired_version) + ".tgz"

try:
    response = requests.get(download_url)
    with open(download_file, "wb") as f:
        f.write(response.content)
except requests.exceptions.RequestException:
    print("Failed to download Python. Please check your network connection and try again.")
    sys.exit(1)

# Step 4: Check if the download was successful
if not os.path.exists(download_file):
    print("Failed to download " + download_file + ". Please check your network connection and try again.")
    sys.exit(1)

# Step 5: Extract the downloaded file
try:
    with tarfile.open(download_file) as tar:
        tar.extractall()
except (OSError, tarfile.TarError):
    print("Failed to extract Python. Please check your download file and try again.")
    sys.exit(1)

# Step 6: Compile and install Python
try:
    subprocess.check_call(["./configure", "--prefix=/usr/local/python3.10"], cwd="Python-" + desired_version)
    subprocess.check_call(["make"], cwd="Python-" + desired_version)
    subprocess.check_call(["make", "install"], cwd="Python-" + desired_version)
except subprocess.CalledProcessError:
    print("Failed to compile or install Python. Please check your installation dependencies and try again.")
    sys.exit(1)

# Step 7: Modify the python path in the yum command
backup_file = "/usr/bin/yum.bak"
if not os.path.exists(backup_file):
    os.rename("/usr/bin/yum", backup_file)

with open(backup_file, "r") as f:
    content = f.read()
if "#!/usr/bin/python" in content:
    content = content.replace("#!/usr/bin/python", "#!/usr/bin/python2")
    with open("/usr/bin/yum", "w") as f:
        f.write(content)

# Step 8: Print a success message
print("Python " + desired_version + " has been installed successfully and set as the default version!")

 

标签:自定义,desired,python,一键,try,Python,version,file,download
From: https://www.cnblogs.com/xlei/p/17326622.html

相关文章

  • [oeasy]python0132_变量含义_meaning_声明_declaration_赋值_assignment
    变量定义回忆上次内容上次回顾了一下历史python是如何从无到有的看到Guido长期的坚持和努力 编程语言的基础都是变量声明python是如何声明变量的呢? 变量想要定义变量首先明确什么是变量变量就是数值能变的量英文名称varia......
  • [oeasy]python0132_变量含义_meaning_声明_declaration_赋值_assignment
    变量定义回忆上次内容上次回顾了一下历史python是如何从无到有的看到Guido长期的坚持和努力编程语言的基础都是变量声明python是如何声明变量的呢?变量想要定义变量首先明确什么是变量变量就是数值能变的量英文名称variable计算机在内存中分配出空间用来存储这些能变的量那......
  • random模块&string模块谈python中随机数
    一、概述随机数在程序设计中的属于比较基础的内容,主要用于验证场景(如验证码,生成账号对应的密码等),今天结合random模块和string模块来谈谈python中随机数那些事儿。二、随机数实现相关模块2.1random模块random.random()返回一个随机浮点数。>>>importrandom>>>print(ran......
  • PYTHON学习路径计划图整理
    PYTHON学习路径计划图Python工作环境及基础语法知识了解对于Python基础语法学习部分,学习周期大概为4周,需要的相关资源在网络上都能找到免费的资源,而且质量都不错。相关中文资源如下:1.python工作集成环境包Python(x,y): 下载地址Pycharm: 下载地址2.python数据分析相关库(Pa......
  • 自定义bpmn 属性面板
    点击查看代码<template><divclass="custom-properties-panelmy-card"><divv-if="selectedElements.length<=0"class="empty">请选择一个节点</div><divv-else-if="selectedElements.length>1&......
  • 功能不够用?使用C++编写通达信插件及接入Python(二)
    参考:https://zhuanlan.zhihu.com/p/613157262一、准备工作(参考上一篇)安装VS2019 安装pycharm下载 http://help.tdx.com.cn/book.asp《通达信DLL函数编程规范.rar》二、下载python3.x的32位版本,http://www.python.org,随便找个32位版就行了。我准备下载Windowsembeddabl......
  • Python数据分析方向第三方库推荐!
    在Python的应用范围中,数据分析是一个非常不错的就业方向,发展前景好、需求市场大,也成为很多人的首选方向,因此Python也提供了许多数据分析的第三方库。那么Python数据分析第三方库有哪些?这篇文章为大家详细介绍一下。1、PandasPandas是Python强大、灵活的数据分析和探索......
  • angular项目国际化yaml自定义配置(ngx-translate)
    angular国际化配置很简单,但是想不用json文件用yaml文件,并且同一语言分label.jp.yaml和message.jp.yaml两个文件分开管理。1、下载ngx-translate的依赖库npminstall@ngx-translate/core--savenpminstall@ngx-translate/http-loader--save2、app.module.ts 中引入TranslateMo......
  • npm自定义模块及发布模块NodeJS
    在模块目录下执行:npminit(可以加--yes一键生成)新建文件index.jsvaryunan='helloyunan';module.exports=yunan;然后将文件夹放到node_modules可以用下面方法使用试试constyunan=require('huyunan');console.log('yunan',yunan);//yunanhelloyunan发布之前......
  • 一键装机win7系统u盘推荐哪个
    重装系统相比大家都接触过把,前几年大家提到重装就会跑去外面店里去重装,其实现在一键装机已经很成熟了不用在麻烦别人了,那么一键装机win7系统u盘推荐哪个好用一点呢?一键装机win7系统u盘推荐哪个好一、浆果一键装机【点击查看】浆果一键装机这可是新手安装的好东西,不但没有绑定,还......