← 返回
数据分析 中文

Python包鸿蒙兼容性测试技能

Check Python library compatibility with HarmonyOS. Downloads source from GitHub/PyPI, detects Windows-specific dependencies, runs pytest with per-test-case r...
检查 Python 库与 HarmonyOS 的兼容性。从 GitHub/PyPI 下载源码,检测 Windows 特定依赖,运行 pytest 按测试用例执行。
terryking1992
数据分析 clawhub v1.0.1 1 版本 100000 Key: 无需
★ 0
Stars
📥 844
下载
💾 214
安装
1
版本
#latest

概述

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

OptionDescriptionDefault
------------------------------
-w, --workers NNumber of parallel workers4
--sequentialRun checks one at a timeFalse
-r, --requirements FILECheck packages from requirements file-
--keep-sourceKeep downloaded source code for verificationFalse

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

MetricDescription
---------------------
Total TestsIndividual test functions run
PassedSuccessfully completed
FailedFailed due to code issues
Environment IssuesPermission/temp dir failures (not counted)
Pass Ratepassed / total
Valid Pass Ratepassed / (passed + failed) - excludes environment issues

Test Workflow

  1. Download Source - GitHub (preferred) or PyPI
  2. Windows Check - Scan for Windows-specific imports (win32api, pythoncom, pywin32, ctypes.windll)
    • Found → Incompatible (no further testing)
  3. Find Tests - From installed package (preferred) or source
  4. Run Tests - pytest with verbose output, per-function reporting
  5. Analyze - Categorize: environment issues vs code issues
  6. Report - Markdown + JSON with detailed results

Status

StatusCriteria
------------------
✅ CompatibleInstalls + valid pass rate ≥ 80% + no Windows deps
⚠️ PartialInstalls + valid pass rate 50-79%
❌ IncompatibleCannot install OR valid pass rate < 50% OR Windows deps detected

Error Classification

TypeDescriptionCounts Against
-----------------------------------
EnvironmentPermission errors, temp dir issues❌ No
CodeImport errors, API incompatibilities✅ Yes
PlatformWindows/macOS/X11 dependencies✅ Yes

Known Issues

Platform Dependencies

PlatformProblematic PackagesAlternative
--------------------------------------------
Windowspywin32, wmi, pythoncom, xlwingsCross-platform libs
macOSapplescript, quartz, cocoa-
X11python-xlib, xcbpynput

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

  1. Review error classifications (environment vs code)
  2. Use --keep-source to verify tested code
  3. Run multiple times for transient failures
  4. Check Windows dependencies first

For Authors

  1. Include tests in your package
  2. Use pytest
  3. Avoid platform-specific tests (or use @pytest.mark.skipif)
  4. Document system dependencies
  5. Use conditional imports: if sys.platform == 'win32'

版本历史

共 1 个版本

  • v1.0.1 当前
    2026-03-30 08:26 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

suspicious
查看报告

🔗 相关推荐

data-analysis

A股量化 AkShare

mbpz
A股量化数据分析工具,基于AkShare库获取A股行情、财务数据、板块信息等。用于回答关于A股股票查询、行情数据、财务分析、选股等问题。
★ 165 📥 60,015
data-analysis

Data Analysis

ivangdavila
{"answer":"数据分析与可视化。查询数据库、生成报告、自动化电子表格,将原始数据转化为清晰可行的见解。适用于:(1) 您……"}
★ 198 📥 65,120
data-analysis

Stock Analysis

udiedrichsen
{"answer":"基于雅虎财经数据,分析股票与加密货币。支持投资组合管理、自选股预警、股息分析、8维评分、热门趋势扫描及传闻/早期信号探测。适用于股票分析、持仓追踪、财报异动、加密监控、热门股追踪或提前发掘非主流传闻。"}
★ 270 📥 56,972