📜  在Python中制作自动模块安装程序

📅  最后修改于: 2022-05-13 01:55:22.444000             🧑  作者: Mango

在Python中制作自动模块安装程序

先决条件:

  • 网址库
  • 子流程

很多时候,安装内置Python中尚不可用的模块的任务似乎令人沮丧。本文重点去除打开命令行界面的任务,输入pip install模块名称下载一些Python模块。在本文中,我们将尝试自动化此过程。

方法:

  • 使用Python导入子进程模块以模拟命令行
  • 导入 urllib.request 以实现互联网检查工具。
  • 使用 input()函数输入模块名称并初始化 module_name 变量。
  • 将模块名称发送到主函数。
  • 在 main函数中,我们首先直接通过Python更新我们的 pip 版本,以便我们的应用程序能够顺利运行。
  • 在下一行 p= subprocess.run('pip3 install '+module_name) 中,我们将 pip3 install module_name 虚拟地写入命令行。
  • 基于上述语句的返回码(p)和connect()函数的返回值的组合,我们可以假设下面提到的事情。
Return Code(P)Connect FunctionResult
1FalseInternet Off
1TrueInternet is On but some other problem occurred
0TrueModule Installed Successfully
  • 根据上表,我们可以给出所需的输出。

使用的函数:

connect()函数用于以下目的:

  1. 检查互联网是否打开或关闭。
  2. 使用 urllib.request.urlopen(host) 命令访问特定 URL。
    • 如果成功到达,则返回 True
    • 否则,如果未到达即互联网关闭,则返回 false。

下面给出了使用上述方法实现我们的功能的实现。

例子:

Python3
# importing subprocess module
import subprocess
 
# importing urllib.requests for internet checking functions
import urllib.request
 
# initializing host to gfg.
def connect(host='https://www.geeksforgeeks.org/'):
 
    # trying to open gfg
    try:
        urllib.request.urlopen(host)
        return True
 
    # trying to catch exception when internet is not ON.
    except:
        return False
 
 
def main(module_name):
 
    # updating pip to latest version
    subprocess.run('python -m pip install --upgrade pip')
 
    # commanding terminal to pip install
    p = subprocess.run('pip3 install '+module_name)
 
    # internet off
    if(p.returncode == 1 and connect() == False):
        print("error!! occurred check\nInternet connection")
 
    # Every thing worked fine
    elif(p.returncode == 0):
        print("It worked", module_name, " is installed")
 
    # Name of module wrong
    elif(p.returncode == 1 and connect() == True):
        print("error!! occured check\nName of module")
 
 
module_name = input("Enter the module name: ")
main(module_name)


Python3
# importing subprocess module
import subprocess
 
# importing urllib.requests for internet checking functions
import urllib.request
 
# initializing host to gfg.
def connect(host='https://www.geeksforgeeks.org/'):
 
    # trying to open gfg
    try:
        urllib.request.urlopen(host)
        return True
 
    # trying to catch exception when internet is not ON.
    except:
        return False
 
 
def main(module_name):
 
    # updating pip to latest version
    subprocess.run('python -m pip install --upgrade pip')
 
    # commanding terminal to pip install
    p = subprocess.run('pip3 install '+module_name)
 
    # internet off
    if(p.returncode == 1 and connect() == False):
        print("error!! occurred check\nInternet connection")
 
    # Every thing worked fine
    elif(p.returncode == 0):
        print("It worked", module_name, " is installed")
 
    # Name of module wrong
    elif(p.returncode == 1 and connect() == True):
        print("error!! occurred check\nName of module")
 
 
module_name = input("Enter the module name: ")
main(module_name)


输出:

蟒蛇3

# importing subprocess module
import subprocess
 
# importing urllib.requests for internet checking functions
import urllib.request
 
# initializing host to gfg.
def connect(host='https://www.geeksforgeeks.org/'):
 
    # trying to open gfg
    try:
        urllib.request.urlopen(host)
        return True
 
    # trying to catch exception when internet is not ON.
    except:
        return False
 
 
def main(module_name):
 
    # updating pip to latest version
    subprocess.run('python -m pip install --upgrade pip')
 
    # commanding terminal to pip install
    p = subprocess.run('pip3 install '+module_name)
 
    # internet off
    if(p.returncode == 1 and connect() == False):
        print("error!! occurred check\nInternet connection")
 
    # Every thing worked fine
    elif(p.returncode == 0):
        print("It worked", module_name, " is installed")
 
    # Name of module wrong
    elif(p.returncode == 1 and connect() == True):
        print("error!! occurred check\nName of module")
 
 
module_name = input("Enter the module name: ")
main(module_name)