Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Python 2 to Python 3 Migration Example

Documentation

This example demonstrates how to use Graph-sitter to automatically migrate Python 2 code to Python 3. For a complete walkthrough, check out our tutorial.

What This Example Does

The migration script handles five key transformations:

  1. Convert Print Statements

    # From:
    print "Hello, world!"
    print x, y, z
    
    # To:
    print("Hello, world!")
    print(x, y, z)
  2. Update Unicode to str

    # From:
    from __future__ import unicode_literals
    
    text = unicode("Hello")
    prefix = "prefix"
    
    # To:
    text = str("Hello")
    prefix = "prefix"
  3. Convert raw_input to input

    # From:
    name = raw_input("Enter your name: ")
    
    # To:
    name = input("Enter your name: ")
  4. Update Exception Handling

    # From:
    try:
        process_data()
    except ValueError, e:
        print(e)
    
    # To:
    try:
        process_data()
    except ValueError as e:
        print(e)
  5. Modernize Iterator Methods

    # From:
    class MyIterator:
        def next(self):
            return self.value
    
    
    # To:
    class MyIterator:
        def __next__(self):
            return self.value

Running the Example

# Install Graph-sitter
pip install graph-sitter

# Run the migration
python run.py

The script will process all Python files in the repo-before directory and apply the transformations in the correct order.

Understanding the Code

  • run.py - The migration script
  • input_repo/ - Sample Python 2 code to migrate

Learn More