Complete guide for migrating from the original num2words library to num2words2.
- Active maintenance - Regular updates and bug fixes
- AI/LLM optimization - Better support for modern applications
- Enhanced language support - More languages and improved implementations
- Better reliability - Critical bug fixes for production use
- Future-proof - Aligned with evolving AI/ML ecosystem
# 1. Run migration script
curl -O https://raw.githubusercontent.com/jqueguiner/num2words/master/migrate_to_num2words2.py
python migrate_to_num2words2.py --dry-run . # Preview changes
python migrate_to_num2words2.py . # Apply changes
# 2. Install new package
pip install num2words2
pip uninstall num2words # Optional: remove old package
# 3. Update requirements.txt
# Replace "num2words" with "num2words2"First, check what needs to be migrated in your project:
# Find all files with num2words imports
grep -r "num2words" --include="*.py" .
# Check requirements files
grep -r "num2words" requirements*.txt setup.py pyproject.tomlUse our migration script for Python code:
# Download the script
curl -O https://raw.githubusercontent.com/jqueguiner/num2words/master/migrate_to_num2words2.py
# Preview changes (recommended)
python migrate_to_num2words2.py --dry-run /path/to/your/project
# Apply changes
python migrate_to_num2words2.py /path/to/your/projectThe script handles these import patterns:
| Before | After |
|---|---|
from num2words import num2words |
from num2words2 import num2words |
import num2words |
import num2words2 as num2words |
from num2words.lang_en import Num2Word_EN |
from num2words2.lang_en import Num2Word_EN |
Update your dependency declarations:
requirements.txt:
- num2words>=0.5.12
+ num2words2>=0.5.15pyproject.toml:
dependencies = [
- "num2words>=0.5.12",
+ "num2words2>=0.5.15",
]setup.py:
install_requires=[
- "num2words>=0.5.12",
+ "num2words2>=0.5.15",
]Update Dockerfiles:
- RUN pip install num2words
+ RUN pip install num2words2Update CI configuration files:
.github/workflows/test.yml:
- pip install num2words
+ pip install num2words2tox.ini:
deps =
- num2words
+ num2words2Verify your migration works correctly:
# test_migration.py
from num2words2 import num2words
def test_basic_functionality():
"""Test basic num2words2 functionality"""
# Cardinal numbers
assert num2words(42) == "forty-two"
assert num2words(1000) == "one thousand"
# Different languages
assert num2words(42, lang='es') == "cuarenta y dos"
assert num2words(42, lang='fr') == "quarante-deux"
# Ordinal numbers
assert num2words(42, to='ordinal') == "forty-second"
# Currency
result = num2words(42.50, to='currency', lang='en')
assert 'forty' in result and 'fifty' in result
def test_edge_cases():
"""Test edge cases and bug fixes"""
# Test negative numbers (fixed in num2words2)
assert num2words(-1, lang='bn') == 'ঋণাত্মক এক'
# Test float handling (improved in num2words2)
assert num2words(1.5, lang='en') == 'one point five'
if __name__ == "__main__":
test_basic_functionality()
test_edge_cases()
print("✅ All tests passed! Migration successful.")Run the test:
python test_migration.pyBefore:
from num2words import num2words
def convert_number(n):
return num2words(n)After:
from num2words2 import num2words
def convert_number(n):
return num2words(n)Before:
import num2words
def convert_number(n, lang='en'):
return num2words.num2words(n, lang=lang)After:
import num2words2 as num2words
def convert_number(n, lang='en'):
return num2words.num2words(n, lang=lang)Before:
from num2words.lang_en import Num2Word_EN
converter = Num2Word_EN()After:
from num2words2.lang_en import Num2Word_EN
converter = Num2Word_EN()For large codebases, you can migrate gradually:
# compatibility.py - Create a compatibility layer
try:
from num2words2 import num2words
print("Using num2words2")
except ImportError:
from num2words import num2words
print("Fallback to num2words")
# Use this in your codebase
from .compatibility import num2wordsFlask Example:
# Before
from flask import Flask
from num2words import num2words
# After
from flask import Flask
from num2words2 import num2words
app = Flask(__name__)
@app.route('/convert/<int:number>')
def convert_endpoint(number):
return num2words(number)FastAPI Example:
# Before
from fastapi import FastAPI
from num2words import num2words
# After
from fastapi import FastAPI
from num2words2 import num2words
app = FastAPI()
@app.get("/convert/{number}")
def convert_number(number: int):
return {"result": num2words(number)}If you need to rollback the migration:
The migration script creates backups:
# Find all backup files
find . -name "*.num2words_backup"
# Restore all files
for file in $(find . -name "*.num2words_backup"); do
original="${file%.num2words_backup}"
cp "$file" "$original"
echo "Restored $original"
done
# Clean up backup files
find . -name "*.num2words_backup" -delete- Revert imports in Python files
- Update requirements files back to
num2words - Reinstall original package:
pip install num2words - Remove new package:
pip uninstall num2words2
Issue: ImportError after migration
ImportError: No module named 'num2words2'
Solution: Install num2words2: pip install num2words2
Issue: Different output than expected
AssertionError: Expected 'forty-two' but got 'forty two'
Solution: Check if you're using a different language or locale setting.
Issue: Migration script doesn't find files
No Python files found to scan.
Solution: Ensure you're running the script in the correct directory and have Python files.
num2words2 maintains the same performance characteristics as the original:
import time
from num2words2 import num2words
# Benchmark
start = time.time()
for i in range(10000):
num2words(i)
end = time.time()
print(f"Converted 10,000 numbers in {end - start:.2f} seconds")- Documentation: https://github.com/jqueguiner/num2words
- Issues: https://github.com/jqueguiner/num2words/issues
- Discussions: https://github.com/jqueguiner/num2words/discussions
Use this checklist to ensure complete migration:
-
Code Assessment
- Located all num2words imports in Python files
- Identified requirements files to update
- Checked Docker/CI configurations
-
Automated Migration
- Downloaded migration script
- Ran dry-run to preview changes
- Applied migration script
- Verified backups were created
-
Manual Updates
- Updated requirements.txt/pyproject.toml/setup.py
- Updated Docker files
- Updated CI/CD configurations
- Updated documentation/README
-
Testing
- Installed num2words2
- Ran existing test suite
- Tested basic functionality
- Tested edge cases
- Verified no regressions
-
Cleanup
- Uninstalled original num2words (optional)
- Removed backup files (after verification)
- Updated team documentation
Q: Is num2words2 compatible with the original num2words? A: Yes, num2words2 is designed as a drop-in replacement with full backward compatibility.
Q: Will my existing code break? A: No, the API is identical. Only the import statements need to change.
Q: Can I use both libraries simultaneously? A: Not recommended as they may conflict. Choose one for your project.
Q: What about performance? A: Performance is equivalent or better than the original library.
Q: Are all languages supported? A: Yes, all original languages plus additional languages (Armenian, Mongolian, Shona).
Need more help? Check our migration examples or open an issue.