{"id":7891,"library":"2to3","title":"2to3 - Automated Python 2 to 3 Code Translation","description":"2to3 is a Python program designed to automatically convert Python 2.x source code into Python 3.x compatible code. It achieves this by applying a series of 'fixers' that address common syntax and API changes between the two major Python versions. The PyPI package `2to3` (version 1.0, last updated September 2018) primarily provides the `2to3` command-line utility, which was historically part of the Python standard library. Its core component, `lib2to3`, has been deprecated since Python 3.11 and is slated for removal in Python 3.13, marking the tool's transition towards obsolescence for active development. As Python 2 reached its End-of-Life in January 2020, the utility's main relevance is for legacy codebase migrations.","status":"deprecated","version":"1.0","language":"python","source_language":"en","source_url":"https://github.com/python/cpython/tree/main/Lib/2to3","tags":["python2","python3","migration","code conversion","deprecated"],"install":[{"cmd":"pip install 2to3","lang":"bash","label":"Install the 2to3 PyPI package"}],"dependencies":[],"imports":[{"note":"The `2to3` tool is typically invoked from the shell, not imported as a Python module for direct code conversion. While its underlying library, `lib2to3`, exists, its API is unstable and deprecated for general use.","symbol":"2to3 (command-line tool)","correct":"This library is primarily used as a command-line tool."}],"quickstart":{"code":"# Save your Python 2 code to a file, e.g., 'my_python2_script.py'\n# Example content for my_python2_script.py:\n# print 'Hello, world!'\n# x = raw_input('Enter your name: ')\n# print 'Your name is', x\n\n# To see the changes without modifying the file:\n# 2to3 my_python2_script.py\n\n# To apply changes directly to the file (creates a .bak backup):\n# 2to3 -w my_python2_script.py\n\n# To convert an entire directory to a new output directory:\n# mkdir python3_version\n# 2to3 --output-dir=python3_version -W -n python2_codebase/","lang":"bash","description":"The `2to3` tool is executed from the command line, pointing it to Python 2.x source files or directories. The `-w` flag writes the changes back to the original files, creating a `.bak` backup. The `--output-dir` option allows writing converted files to a new location, often used with `-n` to prevent backup files and `-W` to write all files, even if unchanged."},"warnings":[{"fix":"For new projects, avoid Python 2 entirely. For existing Python 2 codebases, migrate to Python 3 as soon as possible and manually address any remaining incompatibilities if `2to3` is no longer viable with your target Python 3 version. Consider alternatives like `python-modernize` for dual Python 2/3 compatibility if needed for a transition period, though Python 2 is EOL.","message":"The underlying `lib2to3` library, which powers the `2to3` tool, was officially deprecated in Python 3.11 and is scheduled for removal in Python 3.13 (expected October 2024). This means the `2to3` PyPI package and the standalone tool may cease to function or be actively supported in newer Python environments.","severity":"breaking","affected_versions":"Python 3.11+"},{"fix":"Always thoroughly review the diffs generated by `2to3` and extensively test the converted Python 3 code. Be prepared to manually refactor code, especially for string/bytes handling, exception syntax (`except Exception as e`), and relocated standard library modules.","message":"`2to3` cannot automatically fix all Python 2 to Python 3 incompatibilities. Significant changes, such as those related to Unicode vs. bytes, reorganized standard library modules (e.g., `urllib`, `urllib2`), and certain semantic shifts, often require manual review and modification after conversion.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Run `2to3` using an older Python 3 interpreter (e.g., Python 3.9 or earlier) if encountering parsing issues with newer Python versions, or manually port the code. Given Python 2 is EOL, a full manual port for problematic sections might be more robust.","message":"The `2to3` tool itself, relying on `lib2to3`, is unable to parse Python 3.10+ grammar due to the introduction of the new PEG parser in CPython. This limits its ability to convert Python 2 code if the `2to3` tool itself is being run on a very recent Python 3 interpreter (e.g., 3.10 and above) that it cannot parse.","severity":"gotcha","affected_versions":"Python 3.10+"}],"env_vars":null,"search_vec":"'1.0':56 '2':4,19,108 '2018':60 '2020':117 '2to3':1,9,54,64 '3':6,25 '3.11':87 '3.13':95 'achiev':30 'activ':104 'address':39 'api':43 'appli':33 'autom':2 'automat':16 'chang':44 'code':7,22,28,131 'codebas':126 'command':66 'command-lin':65 'common':40 'compat':27 'compon':80 'convers':132 'convert':17 'core':79 'deprec':84,133 'design':14 'develop':105 'end':112 'end-of-lif':111 'fixer':37 'histor':71 'januari':116 'last':57 'legaci':125 'lib2to3':81 'librari':77 'life':114 'line':67 'main':121 'major':48 'mark':96 'migrat':127,130 'obsolesc':102 'packag':53 'part':72 'primarili':61 'program':13 'provid':62 'pypi':52 'python':3,12,18,24,49,75,86,94,107 'python2':128 'python3':129 'reach':109 'relev':122 'remov':92 'septemb':59 'seri':35 'sinc':85 'slate':90 'sourc':21 'standard':76 'syntax':41 'tool':98 'toward':101 'transit':100 'translat':8 'two':47 'updat':58 'util':68,119 'version':50,55 'x':20,26","created_at":"2026-04-16T16:58:07.960726+00:00","updated_at":"2026-04-16T16:58:07.960726+00:00","problems":{"verify_error":"no import statement found"},"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.0","cli_name":"2to3","cli_version":"Usage: 2to3 [options] file|dir ...","type":"library","homepage":null,"github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/2to3/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["devops"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"skip","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-05","install_tag":null}}