"Cannot import name x" 使用命名元组时
"Cannot import name x" when using namedtuples
所以,我在一个单独的文件中定义了几个命名元组 "types.py":
import collections
TestWindow = collections.namedtuple('TestWindow', 'HWND rect x y w h refPoint')
Point = collections.namedtuple('Point', 'x y')
并想将这些类型导入到另一个文件中:
from types import TestWindow, Point
def main():
pass
添加第一行导入后,我得到:
Traceback (most recent call last):
File "main.py", line 1, in <module>
from types import TestWindow, Point
ImportError: cannot import name 'TestWindow'
这里有什么问题?
有一个 module in stdlib named types
,它被导入了。
要么重命名您的模块,要么切换到绝对导入。
所以,我在一个单独的文件中定义了几个命名元组 "types.py":
import collections
TestWindow = collections.namedtuple('TestWindow', 'HWND rect x y w h refPoint')
Point = collections.namedtuple('Point', 'x y')
并想将这些类型导入到另一个文件中:
from types import TestWindow, Point
def main():
pass
添加第一行导入后,我得到:
Traceback (most recent call last):
File "main.py", line 1, in <module>
from types import TestWindow, Point
ImportError: cannot import name 'TestWindow'
这里有什么问题?
有一个 module in stdlib named types
,它被导入了。
要么重命名您的模块,要么切换到绝对导入。