简体   繁体   English

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

[英]Check a script/program version in Python

I'm developing a script in Python 2.7. 我正在用Python 2.7开发脚本。 I want to make sure that the script is always up to date via github. 我想确保脚本通过github始终是最新的。 What I've tried is to create a file on github where I put the latest version number in and then in the python script I download that file and compare it with the version number of the program. 我试图在github上创建一个文件,在其中放置最新版本号,然后在python脚本中下载该文件并将其与程序的版本号进行比较。 This is my current script but it doesn't really work: 这是我当前的脚本,但实际上不起作用:

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!")

Thanks for the help! 谢谢您的帮助!

What error is coming. 出现什么错误。 However, what i can see is that you are assigning version no as string and checking it as integer in your if condition. 但是,我可以看到的是,您将版本号分配为字符串,并在if条件中将其检查为整数。 So, either convert it in to int by int() method or convert your integer to string by str(2) # 2 is your version no. 因此,通过int()方法将其转换为int或通过str(2)将整数转换为字符串#2是您的版本号。

Alternative Way: 替代方式:

However, you can simply do one thing, just name the python script in such a way that it also contains version no too, something like parsing_1_4.py if version no is 1.4. 但是,您可以简单地做一件事,只需以同样不包含版本的方式命名python脚本即可,例如,如果版本号为1.4,则类似于parsing_1_4.py。 By this, you don't need to open your script, just check the name of python file, parse the version no & compare it with the version no stored in your file. 这样,您无需打开脚本,只需检查python文件的名称,解析版本号并将其与文件中存储的版本号进行比较。 if they are equal, good, else update the script and note to update the script name too(New Version No.). 如果它们相等,则很好,否则更新脚本并注意也更新脚本名称(新版本号)。

Change if vlatest == 2 To if vlatest == "2" if vlatest == 2更改为if vlatest == "2"

You are using different types, so they are not equal. 您使用的是不同类型,所以它们不相等。 Use the same type and you'll be fine 使用相同的类型,你会没事的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM