评论

收藏

[python] python pip之python模块自动化安装脚本

编程语言 编程语言 发布于:2021-06-24 10:22 | 阅读数:574 | 评论:0

此脚本集成pip 所有功能,可在远程环境中执行,为你解决还在忧愁记住pip命令烦恼,此脚本预留了接口,可根据自己的实际环境自定义开发。
def pip_python_file():
    win = (sys.platform == 'win32')
    linux = (sys.platform == 'linux2')
    if win:
        version = subprocess.Popen('pip -V', stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                   shell=True).stdout.readlines()
        if len(version) > 0:
            print('******************安装环境信息******************')
            try:
                print('win_version:')
                print('\t' + u'版本:%s' % platform.platform())
            except Exception:
                print()
            pip_version = subprocess.Popen('pip -V',
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.PIPE,
                                           shell=True).stdout.readlines()
            python_version = subprocess.Popen('python -V',
                                              stdout=subprocess.PIPE,
                                              stderr=subprocess.PIPE,
                                              shell=True).stdout.readlines()
            python_path = subprocess.Popen('where python',
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.PIPE,
                                           shell=True).stdout.readlines()
            for python_version1 in python_version:
                print('python_version&path:')
                print('\t' + u'版本:%s' % str(python_version1, encoding="utf8").strip())
            for python_path1 in python_path:
                print('\t' + u'路径:%s' % str(python_path1, encoding="utf8").strip())
            for pip_version1 in pip_version:
                print('pip_version &path:')
                print('\t' + u'版本:%s' % str(pip_version1, encoding="utf8").strip()[:11])
                print('\t' + u'路径:%s' % str(pip_version1, encoding="utf8").strip()[16:])
            def menu():
                print("**************Python pip安装工具 1.0**************")
                print('1.添加py模块')
                print('2.卸载py模块')
                print('3.查询更新py模块')
                print('4.查询单个py模块')
                print('5.查询所有py模块')
                print('6.pip使用方法')
                print('7.更新py模块')
                print('8.退出pip工具')
                print('9.查看本电脑环境变量')
            def input_card():
                """
                1.添加py模块
                :return:
                """
                try:
                    print('*************添加py模块*************')
                    out = input('输入模块名称:')
                    add_pip = "pip install"
                    add_pip1 = subprocess.Popen(add_pip + '\t' + out, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                                shell=True).stdout.readlines()
                    for i in add_pip1:
                        print('\t' + str(i, encoding="utf8").strip())
                except Exception as e:
                    print("错误日志:" + str(e))
                else:
                    print()
                while True:
                    niu = input("请继续您的选择是(y/q):").strip()
                    if niu == 'y':
                        return main()
                    elif niu == 'q':
                        exit('/n你已退出程序...')
                    else:
                        print("输入错误,请重新输入")
                        return main()
            def del_name():
                """
                 2.卸载py模块
                :param card_list:
                :return:
                """
                try:
                    print('************* 卸载py模块*************')
                    out = input('输入卸载模块名称:')
                    add_pip = "pip uninstall"
                    add_pip1 = subprocess.Popen(add_pip + '\t' + out, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                                shell=True).stdout.readlines()
                    for i in add_pip1:
                        print('\t' + str(i, encoding="utf8").strip())
                except Exception as e:
                    print("错误日志:" + str(e))
                else:
                    print()
                while True:
                    niu = input("请继续您的选择是(y/q):").strip()
                    if niu == 'y':
                        return main()
                    elif niu == 'q':
                        exit('/n你已退出程序...')
                    else:
                        print("输入错误,请重新输入")
                        return main()
            def del_card():
                """
               7.更新py模块
               :return:
               """
                try:
                    print('*************更新py模块*************')
                    out = input('输入需要更新模块名:')
                    add_pip = "pip install -U"
                    add_pip1 = subprocess.Popen(add_pip + '\t' + out, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                                shell=True).stdout.readlines()
                    for i in add_pip1:
                        print('\t' + str(i, encoding="utf8").strip())
                except Exception as e:
                    print("错误日志:" + str(e))
                else:
                    print()
                while True:
                    niu = input("请继续您的选择是(y/q):").strip()
                    if niu == 'y':
                        return main()
                    elif niu == 'q':
                        exit('/n你已退出程序...')
                    else:
                        print("输入错误,请重新输入")
                        return main()
            def find_name():
                """
                4.查询已安装模块
                :param card_list:
                :return:
                """
                t = input('输入查询模块名称:')
                import sys
                def dump(module):
                    print("{0}模块安装路径".format(module))
                    if module in sys.builtin_module_names:  # 查找内建模块是否存在
                        print("")
                    else:
                        module = __import__(module)  # 非内建模块输出模块路径
                        print('(非内置模块已安装)安装路径:%s' % module.__file__)
                dump(t)
                while True:
                    niu = input("请继续您的选择是(y/q):").strip()
                    if niu == 'y':
                        return main()
                    elif niu == 'q':
                        exit('/n你已退出程序...')
                    else:
                        print("输入错误,请重新输入")
                        return main()
            def edit_name():
                """
                3.需要升级模块
                :param card_list:
                :return:
                """
                update_pip = "pip list --outdated"
                update_pip1 = subprocess.Popen(update_pip, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                               shell=True).stdout.readlines()
                print(u'需要升级模块:')
                for i in update_pip1:
                    print('\t' + str(i, encoding="utf8").strip())
                while True:
                    niu = input("请继续您的选择是(y/q):").strip()
                    if niu == 'y':
                        return main()
                    elif niu == 'q':
                        exit('/n你已退出程序...')
                    else:
                        print("输入错误,请重新输入")
                        return main()
            def see_card():
                """
                5.查看所有安装py模块
                :param card_list:
                :return:
                """
                cat_pip = "pip list"
                cat_pip1 = subprocess.Popen(cat_pip, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                            shell=True).stdout.readlines()
                print(u'你已安装的模块:')
                for i in cat_pip1:
                    print('\t' + str(i, encoding="utf8").strip())
                while True:
                    niu = input("请继续您的选择是(y/q):").strip()
                    if niu == 'y':
                        return main()
                    elif niu == 'q':
                        exit('/n你已退出程序...')
                    else:
                        print("输入错误,请重新输入")
                        return main()
            def pip_help():
                """
                6.退出系统
                :return:
                """
                sys.exit('感谢使用本工具!!!')
            def sign_out():
                """
                8.pip参数使用方法
                :param card_list:
                :return:
                """
                update_pip = "pip"
                update_pip1 = subprocess.Popen(update_pip, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                               shell=True).stdout.readlines()
                print(u'pip参数使用方法:')
                for i in update_pip1:
                    print('\t' + str(i, encoding="utf8").strip())
                while True:
                    niu = input("请继续您的选择是(y/q):").strip()
                    if niu == 'y':
                        return main()
                    elif niu == 'q':
                        exit('/n你已退出程序...')
                    else:
                        print("输入错误,请重新输入")
                        return main()
            def pip_path():
                import os
                print(u'本地电脑环境变量:')
                print('\t' + 'PATH=' + os.environ['path'])
            def main():
                menu()
                while True:
                    mun = input('请输入功能序号:')
                    if mun.isdigit():
                        mun = int(mun)
                    else:
                        print('请输入正确的功能序号!')
                    if mun == 1:
                        input_card()
                    if mun == 2:
                        del_name()
                    if mun == 3:
                        edit_name()
                    if mun == 4:
                        find_name()
                    if mun == 5:
                        see_card()
                    if mun == 6:
                        sign_out()
                        break
                    if mun == 7:
                        del_card()
                    if mun == 8:
                        pip_help()
                    if mun == 9:
                        pip_path()
            if __name__ == '__main__':
                card_list = []
                main()
        else:
            print('请先安装pip或配置环境变量')
    else:
        print('请先安装pip或配置环境变量')
        from bs4 import BeautifulSoup
        text = """
                                                                            pip安装方法                            """
        soup = BeautifulSoup(text, 'html.parser')
        href = soup.find('a').get('href')
        print('pip安装方式:', href)
    if linux:
        pass
    else:
        print('请先安装pip或配置环境变量')

关注下面的标签,发现更多相似文章