Python HarmonyOS Compatibility Checker
When to Use
- Check if a Python package works on HarmonyOS
- Validate multiple packages before deployment
- Migrate a Python project to HarmonyOS
- Detect Windows-specific API dependencies (win32api, pythoncom, pywin32, etc.)
Usage
# Single package
python scripts/check_compatibility.py requests
# Multiple packages (parallel, 4 workers default)
python scripts/check_compatibility.py requests numpy pandas flask
# Specify workers
python scripts/check_compatibility.py --workers 8 requests numpy pandas
# From requirements file
python scripts/check_compatibility.py -r requirements.txt
python scripts/check_compatibility.py -w 8 -r requirements.txt
# Sequential mode (debugging)
python scripts/check_compatibility.py --sequential requests numpy
# Keep downloaded source for verification
python scripts/check_compatibility.py --keep-source numpy
Options
| Option |
Description |
Default |
-w, --workers N |
Number of parallel workers |
4 |
--sequential |
Run checks one at a time |
False |
-r, --requirements FILE |
Check packages from requirements file |
- |
--keep-source |
Keep downloaded source code for verification |
False |
Output
Reports saved to current directory:
compatibility_report_*.md - Markdown with summary and details
compatibility_report_*.json - Machine-readable with per-test-case results
Timestamps use 东八区时间 (UTC+8/北京时间).
Example (numpy)
📦 numpy v2.2.1
Status: ✅ Compatible
Test Pass Rate: 88.9%
Total: 45 tests | Passed: 40 | Failed: 5
Metrics
| Metric |
Description |
| Total Tests |
Individual test functions run |
| Passed |
Successfully completed |
| Failed |
Failed due to code issues |
| Environment Issues |
Permission/temp dir failures (not counted) |
| Pass Rate |
passed / total |
| Valid Pass Rate |
passed / (passed + failed) - excludes environment issues |
Test Workflow
- Download Source - GitHub (preferred) or PyPI
- Windows Check - Scan for Windows-specific imports (win32api, pythoncom, pywin32, ctypes.windll)
- Found → Incompatible (no further testing)
- Find Tests - From installed package (preferred) or source
- Run Tests - pytest with verbose output, per-function reporting
- Analyze - Categorize: environment issues vs code issues
- Report - Markdown + JSON with detailed results
Status
| Status |
Criteria |
| ✅ Compatible |
Installs + valid pass rate ≥ 80% + no Windows deps |
| ⚠️ Partial |
Installs + valid pass rate 50-79% |
| ❌ Incompatible |
Cannot install OR valid pass rate < 50% OR Windows deps detected |
Error Classification
| Type |
Description |
Counts Against |
| Environment |
Permission errors, temp dir issues |
❌ No |
| Code |
Import errors, API incompatibilities |
✅ Yes |
| Platform |
Windows/macOS/X11 dependencies |
✅ Yes |
Known Issues
Platform Dependencies
| Platform |
Problematic Packages |
Alternative |
| Windows |
pywin32, wmi, pythoncom, xlwings |
Cross-platform libs |
| macOS |
applescript, quartz, cocoa |
- |
| X11 |
python-xlib, xcb |
pynput |
Windows Module Detection
Scans for: win32api, win32con, win32gui, win32com, pythoncom, pywintypes, ctypes.windll, winsound, msvcrt
Detection includes:
- Direct imports:
import win32api, from win32com import ...
- Dynamic imports:
importlib.import_module('pythoncom')
- ctypes Windows DLL:
ctypes.windll, ctypes.WinDLL
- References in setup.py, pyproject.toml
Compatible Packages
requests, numpy (45 tests, 88.9%), pandas, flask, django, pytest, beautifulsoup4, pillow
Scripts
check_compatibility.py
Features:
- Source download from GitHub/PyPI
- Windows dependency detection
- Test discovery from installed package or source
- pytest integration with per-test-case reporting
- Environment issue detection
- Source code retention (
--keep-source)
pytest Integration:
- Uses
pytest -v --tb=short --import-mode=importlib
- Runs from
/tmp to avoid source import conflicts
- Parses output for individual test function results
- Limits to 15 test files, shows progress every 5 files
Troubleshooting
Permission Errors
# Clean up pytest temp directories
rm -rf pytest-of-*
Source Verification
# Keep source for inspection
python scripts/check_compatibility.py --keep-source xlwings
# Check Windows imports
grep -r "import win32" /path/to/source/
False Positives
Some failures may be due to:
- Missing optional system libraries
- Network-dependent tests
- pytest configuration issues
Review detailed reports to distinguish real incompatibilities from environment issues.
Integration
CI/CD
- name: HarmonyOS Compatibility
run: python scripts/check_compatibility.py -r requirements.txt --keep-source
Programmatic
from scripts.check_compatibility import check_package
result = check_package("requests")
print(f"Compatible: {result.compatible}, Issues: {result.issues}")
Limitations
- Binary dependencies may fail to compile
- Some packages require system-level dependencies
- Pure HarmonyOS (NEXT) has stricter security policies
- Not all packages have unit tests
- Network required for source download
Related
references/python-env-setup.md - Python setup on HarmonyOS
references/compatibility-database.md - Known package status
Best Practices
For Users
- Review error classifications (environment vs code)
- Use
--keep-source to verify tested code
- Run multiple times for transient failures
- Check Windows dependencies first
For Authors
- Include tests in your package
- Use pytest
- Avoid platform-specific tests (or use
@pytest.mark.skipif)
- Document system dependencies
- Use conditional imports:
if sys.platform == 'win32'
1---2name: python-harmony-compatibility-checker3description: Check Python library compatibility with HarmonyOS. Downloads source from GitHub/PyPI, detects Windows-specific dependencies, runs pytest with per-test-case reporting, and generates detailed compatibility reports.4---56# Python HarmonyOS Compatibility Checker78## When to Use910- Check if a Python package works on HarmonyOS11- Validate multiple packages before deployment12- Migrate a Python project to HarmonyOS13- Detect Windows-specific API dependencies (win32api, pythoncom, pywin32, etc.)1415## Usage1617```bash18# Single package19python scripts/check_compatibility.py requests2021# Multiple packages (parallel, 4 workers default)22python scripts/check_compatibility.py requests numpy pandas flask2324# Specify workers25python scripts/check_compatibility.py --workers 8 requests numpy pandas2627# From requirements file28python scripts/check_compatibility.py -r requirements.txt29python scripts/check_compatibility.py -w 8 -r requirements.txt3031# Sequential mode (debugging)32python scripts/check_compatibility.py --sequential requests numpy3334# Keep downloaded source for verification35python scripts/check_compatibility.py --keep-source numpy36```3738### Options3940| Option | Description | Default |41|--------|-------------|---------|42| `-w, --workers N` | Number of parallel workers | 4 |43| `--sequential` | Run checks one at a time | False |44| `-r, --requirements FILE` | Check packages from requirements file | - |45| `--keep-source` | Keep downloaded source code for verification | False |4647## Output4849Reports saved to current directory:50- `compatibility_report_*.md` - Markdown with summary and details51- `compatibility_report_*.json` - Machine-readable with per-test-case results5253Timestamps use **东八区时间 (UTC+8/北京时间)**.5455### Example (numpy)5657```58📦 numpy v2.2.159 Status: ✅ Compatible60 Test Pass Rate: 88.9%61 Total: 45 tests | Passed: 40 | Failed: 562```6364### Metrics6566| Metric | Description |67|--------|-------------|68| **Total Tests** | Individual test functions run |69| **Passed** | Successfully completed |70| **Failed** | Failed due to code issues |71| **Environment Issues** | Permission/temp dir failures (not counted) |72| **Pass Rate** | `passed / total` |73| **Valid Pass Rate** | `passed / (passed + failed)` - excludes environment issues |7475## Test Workflow76771. **Download Source** - GitHub (preferred) or PyPI782. **Windows Check** - Scan for Windows-specific imports (win32api, pythoncom, pywin32, ctypes.windll)79 - Found → **Incompatible** (no further testing)803. **Find Tests** - From installed package (preferred) or source814. **Run Tests** - pytest with verbose output, per-function reporting825. **Analyze** - Categorize: environment issues vs code issues836. **Report** - Markdown + JSON with detailed results8485### Status8687| Status | Criteria |88|--------|----------|89| **✅ Compatible** | Installs + valid pass rate ≥ 80% + no Windows deps |90| **⚠️ Partial** | Installs + valid pass rate 50-79% |91| **❌ Incompatible** | Cannot install OR valid pass rate < 50% OR Windows deps detected |9293### Error Classification9495| Type | Description | Counts Against |96|------|-------------|----------------|97| **Environment** | Permission errors, temp dir issues | ❌ No |98| **Code** | Import errors, API incompatibilities | ✅ Yes |99| **Platform** | Windows/macOS/X11 dependencies | ✅ Yes |100101## Known Issues102103### Platform Dependencies104105| Platform | Problematic Packages | Alternative |106|----------|---------------------|-------------|107| Windows | pywin32, wmi, pythoncom, **xlwings** | Cross-platform libs |108| macOS | applescript, quartz, cocoa | - |109| X11 | python-xlib, xcb | pynput |110111### Windows Module Detection112113Scans for: `win32api`, `win32con`, `win32gui`, `win32com`, `pythoncom`, `pywintypes`, `ctypes.windll`, `winsound`, `msvcrt`114115**Detection includes:**116- Direct imports: `import win32api`, `from win32com import ...`117- Dynamic imports: `importlib.import_module('pythoncom')`118- ctypes Windows DLL: `ctypes.windll`, `ctypes.WinDLL`119- References in setup.py, pyproject.toml120121### Compatible Packages122123- `requests`, `numpy` (45 tests, 88.9%), `pandas`, `flask`, `django`, `pytest`, `beautifulsoup4`, `pillow`124125## Scripts126127### check_compatibility.py128129**Features:**130- Source download from GitHub/PyPI131- Windows dependency detection132- Test discovery from installed package or source133- pytest integration with per-test-case reporting134- Environment issue detection135- Source code retention (`--keep-source`)136137**pytest Integration:**138- Uses `pytest -v --tb=short --import-mode=importlib`139- Runs from `/tmp` to avoid source import conflicts140- Parses output for individual test function results141- Limits to 15 test files, shows progress every 5 files142143## Troubleshooting144145### Permission Errors146147```bash148# Clean up pytest temp directories149rm -rf pytest-of-*150```151152### Source Verification153154```bash155# Keep source for inspection156python scripts/check_compatibility.py --keep-source xlwings157158# Check Windows imports159grep -r "import win32" /path/to/source/160```161162### False Positives163164Some failures may be due to:165- Missing optional system libraries166- Network-dependent tests167- pytest configuration issues168169Review detailed reports to distinguish real incompatibilities from environment issues.170171## Integration172173### CI/CD174175```yaml176- name: HarmonyOS Compatibility177 run: python scripts/check_compatibility.py -r requirements.txt --keep-source178```179180### Programmatic181182```python183from scripts.check_compatibility import check_package184result = check_package("requests")185print(f"Compatible: {result.compatible}, Issues: {result.issues}")186```187188## Limitations189190- Binary dependencies may fail to compile191- Some packages require system-level dependencies192- Pure HarmonyOS (NEXT) has stricter security policies193- Not all packages have unit tests194- Network required for source download195196## Related197198- `references/python-env-setup.md` - Python setup on HarmonyOS199- `references/compatibility-database.md` - Known package status200201## Best Practices202203### For Users2042051. Review error classifications (environment vs code)2062. Use `--keep-source` to verify tested code2073. Run multiple times for transient failures2084. Check Windows dependencies first209210### For Authors2112121. Include tests in your package2132. Use pytest2143. Avoid platform-specific tests (or use `@pytest.mark.skipif`)2154. Document system dependencies2165. Use conditional imports: `if sys.platform == 'win32'`