hhatto / autopep8

A tool that automatically formats Python code to conform to the PEP 8 style guide.
https://pypi.org/project/autopep8/
MIT License
4.54k stars 291 forks source link

ModuleNotFoundError: No module named 'lib2to3' (python3.12) #720

Closed tandav closed 3 months ago

tandav commented 7 months ago

Command Line

$ pip install --upgrade autopep8
$ autopep8 -i .
Traceback (most recent call last):
  File "/home/tandav/.cache/virtualenvs/pipe21/bin/autopep8", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/tandav/.cache/virtualenvs/pipe21/lib/python3.12/site-packages/autopep8.py", line 4536, in main
    results = fix_multiple_files(args.files, args, sys.stdout)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tandav/.cache/virtualenvs/pipe21/lib/python3.12/site-packages/autopep8.py", line 4430, in fix_multiple_files
    for name in filenames:
  File "/home/tandav/.cache/virtualenvs/pipe21/lib/python3.12/site-packages/autopep8.py", line 4381, in find_files
    if match_file(os.path.join(root, f),
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tandav/.cache/virtualenvs/pipe21/lib/python3.12/site-packages/autopep8.py", line 4368, in match_file
    if not os.path.isdir(filename) and not is_python_file(filename):
                                           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tandav/.cache/virtualenvs/pipe21/lib/python3.12/site-packages/autopep8.py", line 4452, in is_python_file
    with open_with_encoding(
         ^^^^^^^^^^^^^^^^^^^
  File "/home/tandav/.cache/virtualenvs/pipe21/lib/python3.12/site-packages/autopep8.py", line 172, in open_with_encoding
    encoding = detect_encoding(filename, limit_byte_check=limit_byte_check)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tandav/.cache/virtualenvs/pipe21/lib/python3.12/site-packages/autopep8.py", line 182, in detect_encoding
    from lib2to3.pgen2 import tokenize as lib2to3_tokenize
ModuleNotFoundError: No module named 'lib2to3'

Your Environment

Jonnhis commented 5 months ago

I have the same problem and the same environment as @tandav. The workaround I found is the following (https://stackoverflow.com/questions/31228927/how-to-use-install-python-2to3):

sudo apt install 2to3
sudo apt install python3.12-lib2to3

Still, it shows a deprecation warning: DeprecationWarning: lib2to3 package is deprecated and may not be able to parse Python 3.10+

hhatto commented 3 months ago

fixed in autopep8 v2.1.0.