2010-03-15 236 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