Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

AttributeError: module 'compiler' has no attribute 'parseFile'

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading