Guidance for modernizing legacy Python 2 scientific computing code to Python 3. This skill should be used when tasks involve converting outdated scientific Python scripts (using deprecated libraries like ConfigParser, cPickle, urllib2, or Python 2 syntax) to modern Python 3 equivalents with contemporary scientific stack (NumPy, pandas, scipy, matplotlib). Applies to data processing, analysis pipelines, and scientific computation modernization tasks.
This skill provides guidance for converting legacy Python 2 scientific computing code to modern Python 3+ with contemporary libraries.
Before writing any code, read all source files completely:
Critical: If a file read is truncated, explicitly request the remaining content. Incomplete analysis leads to missed modernization requirements.
Categorize changes needed:
Python 2 to 3 Syntax:
print statements to print() functionsunicode/str handling to Python 3 strings/ vs )//except Exception, e: to except Exception as e:)Library Replacements:
ConfigParser → configparsercPickle → pickleurllib2 → urllib.request or requestsStringIO → io.StringIOpathlib.PathOnly modernize what is actually used. Avoid listing library replacements for code not present in the source.
Check available tools before assuming availability:
# Check for package managers
which pip python3 uv conda 2>/dev/null
Prefer tools already available. If installing new tools (like uv), verify installation succeeded before proceeding.
For PATH modifications, set once at the beginning:
export PATH="$HOME/.local/bin:$PATH"
Avoid repeating this in every command.
When creating requirements.txt or pyproject.toml:
Write complete files: Ensure Write tool calls contain the entire file content. After writing, verify the file:
# Verify file was written correctly
python -m py_compile script_name.py
wc -l script_name.py
Verify file contents: After writing critical files, read them back to confirm correctness:
# Read back and verify
cat script_name.py | head -50
Preserve functionality: The modernized code must produce identical output to the original. Document expected outputs before implementation.
Always validate Python syntax before execution:
python -m py_compile modernized_script.py
Consolidate verification into a single comprehensive test rather than multiple scattered commands.
Test beyond the happy path:
°C) render correctlyIf specific output format is required:
Problem: Write tool calls may be truncated, resulting in incomplete files that appear successful.
Solution: After writing files:
Problem: Truncated file reads lead to missed requirements.
Solution: If a Read operation shows truncation, explicitly request remaining content with offset parameter.
Problem: Listing modernizations for code that doesn't exist in the actual requirements.
Solution: Focus analysis on what the task actually requires, not every possible Python 2 issue.
Problem: Assuming tools like uv are installed.
Solution: Check tool availability first, use fallbacks (pip is almost always available).
Problem: Running same setup commands (PATH export, tool checks) multiple times.
Solution: Consolidate setup into a single initial step.
Problem: Not verifying that written files contain intended content.
Solution: After every critical file write, validate syntax and optionally read back key sections.
When making implementation choices:
requirements.txt for simple projects, pyproject.toml for packages with build requirementsBefore marking task complete, verify: