How to compare Debian package versions?

后端 未结 3 1159
鱼传尺愫
鱼传尺愫 2020-12-24 07:26

I looked at python-apt and python-debian, and they don\'t seem to have functionality to compare package versions. Do I have to write my own, or is

3条回答
  •  自闭症患者
    2020-12-24 07:52

    You could use apt_pkg.version_compare:

    import apt_pkg
    apt_pkg.init_system()
    
    a = '1:1.3.10-0.3'
    b = '1.3.4-1'
    vc = apt_pkg.version_compare(a,b)
    if vc > 0:
        print('version a > version b')
    elif vc == 0:
        print('version a == version b')
    elif vc < 0:
        print('version a < version b')        
    

    yields

    version a > version b
    

    Thanks to Tshepang for noting in the comments that for newer versions: apt.VersionCompare is now apt_pkg.version_compare.

提交回复
热议问题