简体   繁体   中英

Python AttributeError: 'module' object has no attribute 'suite'

For some reason in some cases this code does not work. I have tried the exact same file (entire thing selected and copy/pasted into a file) in another directory and it was able to parse. It's quite frustrating as there isn't anything different about the file being parsed either.

from compiler.ast import *
import compiler
import sys
import string

debug = False
myfile = sys.argv[1]
print compiler.parseFile(myfile)

Failing output:

Traceback (most recent call last):    
File "src/compile.py", line 17, in <module>    
  print compiler.parseFile(myfile)    
File "/usr/lib/python2.7/compiler/transformer.py", line 47, in parseFile    
  return parse(src)    
File "/usr/lib/python2.7/compiler/transformer.py", line 51, in parse    
  return Transformer().parsesuite(buf)    
File "/usr/lib/python2.7/compiler/transformer.py", line 128, in 
parsesuite    
  return self.transform(parser.suite(text))    
AttributeError: 'module' object has no attribute 'suite'

Successful output:

Module(None, Stmt([Assign([AssName('x', 'OP_ASSIGN')], Add((CallFunc(Name('input'), [], None, None), Const(100)))), Printnl([Name('x')], None)]))

In the failing directory is a file named either parser.py or parser.pyc or a directory named parser . Delete or rename it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM