哪種方法對於在特定版本的python中導入模塊最有意義?我的用例是我正在編寫將部署到Python 2.3環境中的代碼,並在幾個月內升級到python 2.5。這:導入特定於版本的python模塊的最佳方法
if sys.version_info[:2] >= (2, 5):
from string import Template
else:
from our.compat.string import Template
或本
try:
from string import Template
except ImportError:
from our.compat.string import Template
我知道,這兩種情況下是同樣正確和工作正常,但哪一個是最好?
你已經錯過了`從xml.etree進口cElementTree作爲ET`爲Python 2.5和高達 – jfs 2008-12-05 00:42:37