Python 将两个方法作为不同的对象导入

Python 将两个方法作为不同的对象导入,python,import,Python,Import,我如何才能从re模块导入搜索和匹配,作为重新匹配和重新搜索 from re import match, search as re_match, re_match 这不起作用您的订单错误,名称重复;您当前的尝试被解释为 from re import (match), (search as re_match), (re_match) 因此,错误-re\u match不在re中。你想要 from re import match as re_match, search as re_search 虽

我如何才能从re模块导入搜索和匹配,作为重新匹配和重新搜索

from re import match, search as re_match, re_match

这不起作用

您的订单错误,名称重复;您当前的尝试被解释为

from re import (match), (search as re_match), (re_match)
因此,错误-
re\u match
不在
re
中。你想要

from re import match as re_match, search as re_search

虽然这在
re.match
re.search
上没有保存任何内容,但不清楚您为什么要麻烦

这不起作用的原因是,您不能使用
作为
导入这两个东西,并将它们定义为同一别名。你需要不同的别名。以下将起作用:

from re import match as re_match
from re import search as re_search
注意别名的不同

此外,如果将
用作
关键字,则这些命令需要位于单独的行中。这是因为Python将逗号分隔的列表解释为元组,并尝试将其解包。如果要将它们放在同一行上,则需要分别执行,例如:

from re import match as re_match, search as re_search

如果订单正确,您可以将两者导入到同一个别名中,但显然您只能访问其中一个别名!是的,我对我的答案做了忍者编辑,说这是因为python解压元组的方式。但是,同一个别名将来会引起问题,所以我觉得这一点很重要。与其说“这不起作用”,不如说更具体一些——在本例中,提供完整的错误回溯。