1. 程式人生 > >Python--cannot import name 'namedtuple'

Python--cannot import name 'namedtuple'

Python–cannot import name ‘namedtuple’

表示無法找到該’namedtuple’模組。檢查一下我們的程式碼。


'常用的集合'
__author__ = 'click'
__date__ = '2018/8/2 下午4:37'

from collections import namedtuple

# namedtuple,用來自定義一個tuple,可以規定tuple的個數
# ,並且可以使用定義的屬性代替索引進行取值

Circle = namedtuple('Circle', ['x', 'y', 'r'])
circle = Circle(1
, 1, 4) print('namedtuple橫座標(x)%1s 縱座標(y)%2s 半徑(r)%3s' % (circle.x, circle.y, circle.r))

from collections import namedtuple

匯入的方式沒有出錯啊,由collections模組匯入namedtuple方法。

這種情況很有可能是你自己建立的模組名與系統內建的模組一致,導致import的是你自己建立的,而不是系統的。
自然也就報錯了。

如下:
WX20180802-165743@2x.png

你要問我怎麼改? 肯定是修改你自建的模組名啊,隨便改只要不跟系統的衝突,開心就好。


需要一個Python編碼規範。哪位大佬提供一下。

麻煩發給我一份。

QQ:841740273