使用请求自动更新python并尝试除外但不起作用

时间:2019-06-20 22:11:05

标签: python python-3.x auto-update

我正在尝试为正在处理的python项目添加一些更新功能。

为此,我首先在项目顶部定义了项目版本。 然后,我得到了一些代码,在try / except块中,它通过请求来访问URL以获取版本号。

该代码应该比较两者,如果它们不匹配,则为用户提供更新并开始下载较新版本的选项。

我的代码跳过了try / except块,并且都没有执行。.我做错了什么?

注意:为了便于阅读,我省略了脚本的某些部分。

    try:
        print("Checking for updates...")
        vercheck = requests.get(
            "https://pastebin.com/fMvQX8Nk").text.rstrip().split("|")
        if not vercheck[0] == selfbotversion:
            print(colored("There is an update available, Would you like to download it?", menucolour))
            verchoice = input("(Y/N): ")
            if verchoice.lower() == "y":
                clear()

                @animation.wait(colored('Downloading update, Please Wait ', menucolour))
                def run_update():
                    update = requests.get('https://github.com/redacted/redacted-redacted/archive/master.zip')
                    print("Update downloaded, Installing...")
                    return update

                update = run_update()
                with open("update.zip", "wb") as handle:
                    handle.write(update.content)
                try:
                    shutil.copy("config.py", "config_old.py")
                except Exception:
                    pass
                try:
                    shutil.unpack_archive("update.zip")
                    copy_tree("Public-master/", ".")
                    os.remove("update.zip")
                    shutil.rmtree("redacted-redacted-master/")
                    print("Update complete, exiting.")
                except Exception as e:
                    print("Error Updating, {}".format(e))
                time.sleep(3)
                sys.exit()
    except Exception as e:
        print("Error Updating")

如果我在try / except块之外执行此代码,则它将运行,因此问题必定是其他原因。

        vercheck = requests.get(
            "https://raw.githubusercontent.com/redacted/redacted-redacted/master/version").text.rstrip().split("|")
            if not vercheck[0] == selfbotversion:
        print(vercheck[0])

感谢任何帮助,因为我仍在学习python! 谢谢

1 个答案:

答案 0 :(得分:0)

我不确定到底要怎么解决这个问题,但我认为问题归结于模块导入不正确。

我最终只是将代码复制并粘贴到块中,然后修复错误,而不是将它们全部加载在一起,而不是将它们全部加载到一起,因为我发现它跳过了很多会首先导致我的问题的错误! >