Home > OS >  AttributeError: module 'compiler' has no attribute 'parseFile'
AttributeError: module 'compiler' has no attribute 'parseFile'

Time:12-03

It´s the first time I´m using python script. I´m trying to run a script in python 3.11. But I got this error :

Traceback (most recent call last):
  File "D:\professionnel\projets\PHP-py2php\py2php.py", line 2013, in <module>
    translated_code = "<?php "   translate("tmp.py", module_name),
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "XXXX\PHP-py2php\py2php.py", line 1741, in translate
    mod = compiler.parseFile(file_name)
          ^^^^^^^^^^^^^^^^^^
AttributeError: module 'compiler' has no attribute 'parseFile'

how can modify compiler.parseFile(file_name) to be compatible ?

CodePudding user response:

It looks like you're trying to run a script called py2php.py that uses a module called compiler to parse a Python file. In Python 3.11, the compiler module was removed. You will need to use a different method to parse the file.

One option is to use the ast module, which provides functions for working with Abstract Syntax Trees (ASTs) of Python code. You can use the ast.parse() function to parse a string containing Python code and get an AST representation of that code.

Here's an example of how you might modify your code to use the ast module:

import ast

# Replace this line:
# mod = compiler.parseFile(file_name)

# With this:
with open(file_name, "r") as f:
    code = f.read()
mod = ast.parse(code)

This code reads the contents of the file at the specified file_name and passes it to the ast.parse() function to get an AST representation of the code. You can then use this AST to perform any transformations or operations that you need to do.

I hope this helps! Let me know if you have any other questions.

  • Related