Python3向后兼容性(shlex.quote与pipes.quote)

Python3向后兼容性(shlex.quote与pipes.quote),python,compatibility,shlex,Python,Compatibility,Shlex,我的一个项目使用了自python 3.3以来就可用的shlex.quote。但是shlex.quote与移动到shlex后被弃用的pipes.quote相同 现在为了兼容性,我使用以下代码: def cmd_quote(string): import sys if int(sys.version[2]) < 3: import pipes return pipes.quote(string) else: import

我的一个项目使用了自python 3.3以来就可用的
shlex.quote
。但是
shlex.quote
与移动到shlex后被弃用的
pipes.quote
相同

现在为了兼容性,我使用以下代码:

def cmd_quote(string):
    import sys
    if int(sys.version[2]) < 3:
        import pipes
        return pipes.quote(string)
    else:
        import shlex
        return shlex.quote(string)
def cmd_quote(字符串):
导入系统
如果int(系统版本[2])<3:
导入管道
返回管道。引号(字符串)
其他:
导入shlex
返回shlex.quote(字符串)

是否存在更好的做法?

首先,如果要对版本进行数字比较,请使用,不要尝试在
版本中解析字符串*

这也意味着您可以利用元组比较的方式编写如下代码:

if sys.version_info < (3, 3):
try:
    from shlex import quote as cmd_quote
except ImportError:
    from pipes import quote as cmd_quote