2010-03-15 178 views
1

在Python 3.1.1使用代碼print('{0} is not'.format('That that is not')),我得到以下錯誤:字符串格式錯誤

AttributeError: 'str' object has no attribute 'format' 

當我刪除該行Netbeans的開頭自動插入:

from distutils.command.bdist_dumb import format 

本身導致錯誤

ImportError: cannot import name format 

我在做什麼錯在這裏?

回答

6

您必須運行較舊版本的Python。這確實在Python 3.1.1+工作:

$ python3 
Python 3.1.1+ (r311:74480, Nov 2 2009, 14:49:22) 
[GCC 4.4.1] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> '{0} is not'.format('That that is not') 
'That that is not is not' 

你會的,但是,在Python 2.5.4得到這個錯誤:

$ python2.5 
Python 2.5.4 (r254:67916, Jan 20 2010, 21:44:03) 
[GCC 4.4.1] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> '{0} is not'.format('That that is not') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: 'str' object has no attribute 'format' 

這個功能似乎已經被移植到Python 2.6,所以你不會在那裏得到這個錯誤。您必須運行Python < 2.6。

+0

奇怪......我在Netbeans中運行<2.6,但在我的機器上安裝了Python 3。 Netbeans必須已經安裝了Python本身?編輯:哦,我現在明白了! Netbeans自己安裝了Jython,它只有2.5:( – wrongusername 2010-03-15 20:40:22

+0

btw,我想要upvote你,但達到我的投票限制:( – wrongusername 2010-03-15 20:41:07

+0

)你應該能夠配置你的Netbeans項目使用的Python版本:http:/ /wiki.netbeans.org/NetBeansPythonTutorial#Setting_Your_Platform_Runtime – allyourcode 2010-03-15 20:54:02