检查Python中的脚本/程序版本

时间:2017-04-27 08:28:07

标签: python python-2.7

我正在使用Python 2.7开发一个脚本。我想通过github确保脚本始终是最新的。 我试过的是在github上创建一个文件,我在其中放入最新版本号,然后在python脚本中我下载该文件并将其与程序的版本号进行比较。这是我目前的脚本,但它确实不起作用:

import sys
import os
import urllib
current_version = """1.4"""
print ("\a")
print ("Checking for latest verion..")
urllib.urlretrieve ("https://raw.githubusercontent.com/[myname]/[myproject]/master/version_latest", "version_latest")
version = open('version_latest', 'r').read()
if version == current_version:
    vlatest = "1"
else:
    vlatest = "2"

if vlatest == 2:
    print ("Please get the latest version of this program here:")
    print ("https://github.com/[myname]/[myproject]")
    print ("You can't use the program without it!")
    print ("\a")
    os.system("start https://github.com/[myname]/[myproject]")
    raw_input("Press enter to exit")
    sys.exit()
else:
    print ("You have the latest verion! Program will start automaticly!")

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

即将发生什么错误。 但是,我可以看到你将版本号指定为字符串并在if条件中将其作为整数检查。 因此,要么通过 int()方法将其转换为int,要么将整数转换为字符串 str(2)#2是您的版本号。

替代方式:

但是,您可以简单地做一件事,只需命名python脚本,使其也包含版本号,如果版本号为1.4,则类似于parsing_1_4.py。这样,你不需要打开你的脚本,只需检查python文件的名称,解析版本号和&将其与存储在文件中的版本进行比较。如果它们相同,那么好,否则更新脚本并注意更新脚本名称(新版本号)。

答案 1 :(得分:0)

更改     if vlatest == 2 至     if vlatest == "2"

您使用的是不同的类型,因此它们并不相同。 使用相同的类型,你会没事的