Migrating from Python 2 to Python 3 requires careful planning. Here’s a step-by-step guide.
Key Differences
Print Statement
# 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
- Test thoroughly
- Update dependencies
- Use type hints
- Handle bytes/strings
- Update string formatting
Conclusion
Migrate to Python 3 for modern Python development! π