Migrating from Python 2 to Python 3 requires careful planning. Here’s a step-by-step guide.

Key Differences

# Python 2
print "Hello"

# Python 3
print("Hello")

Division

# Python 2
5 / 2  # 2

# Python 3
5 / 2  # 2.5
5 // 2  # 2

Unicode

# Python 2
s = u"Hello"

# Python 3
s = "Hello"  # Unicode by default

Migration Tools

# 2to3 tool
2to3 -w script.py

# Modernize
python-modernize script.py

Best Practices

  1. Test thoroughly
  2. Update dependencies
  3. Use type hints
  4. Handle bytes/strings
  5. Update string formatting

Conclusion

Migrate to Python 3 for modern Python development! 🐍