Guide for modernizing legacy Python 2 scientific computing code to Python 3 with modern libraries. This skill should be used when migrating scientific scripts involving data processing, numerical computation, or analysis from Python 2 to Python 3, or when updating deprecated scientific computing patterns to modern equivalents (pandas, numpy, pathlib).
This skill provides guidance for migrating legacy Python 2 scientific computing code to modern Python 3 with contemporary libraries and best practices.
Apply this skill when:
Before making any changes, ensure complete understanding of the existing codebase:
Read all source files completely - If a file read is truncated, request the full content before proceeding. Never assume file contents based on partial reads.
Identify all dependencies - Check for:
Map the data flow - Understand:
Common Python 2 to Python 3 migration patterns in scientific code:
| Legacy Pattern | Modern Replacement |
|---|---|
print "text" | print("text") |
unicode() / str() | str() with explicit encoding |
open(file) | open(file, encoding='utf-8') |
os.path.join() | pathlib.Path() |
csv module | pandas.read_csv() |
for key in dict.keys() | for key in dict |
dict.has_key(x) | x in dict |
| Manual file iteration | Context managers (with statements) |
xrange() | range() |
Integer division / | Explicit // or float division |
Create the modernized script with these priorities:
Handle configuration files - Check for file existence before reading:
config_path = Path("config.json")
if config_path.exists():
config = json.loads(config_path.read_text(encoding='utf-8'))
Create requirements.txt - Include all dependencies with version constraints
Critical: Always verify file operations
After writing any file, read it back to confirm:
Testing sequence:
Syntax validation - Run Python syntax check:
python -m py_compile script.py
Import verification - Test all imports resolve:
python -c "from script import *"
Functional test - Run the script and compare output to expected results
Output validation - Verify output format matches requirements exactly
Truncated file content - Never proceed with partial file reads. If a response shows ... [truncated] or incomplete content, request the full file before continuing.
Unverified writes - After using a write operation, always read the file back to confirm the complete content was written correctly.
Encoding issues - Always specify encoding='utf-8' explicitly in file operations. Legacy scripts often have implicit ASCII assumptions.
Path string concatenation - Replace all os.path.join() and string concatenation for paths with pathlib.Path operations.
Missing edge case handling:
Environment setup repetition - When setting up environments (venv, PATH), verify the setup persists rather than repeating in each command.
Before marking the task complete, confirm:
When the task specifies an expected output format, verify the output matches exactly: