dy-dashboard/vault/知识库/工作指南/训练记录/2026-06-26-修复auto_update使用虚拟环境Python.md
2026-10-28 11:14:17 +08:00

20 lines
787 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 2026-06-26 修复auto_update.py使用虚拟环境Python
## 背景
`auto_update.py` 通过 `os.system()` 调用 `reimport.py` 和 `import_inventory.py` 时使用系统 Python(`python3`),而非虚拟环境的 Python(`venv/bin/python3`)。新电脑 Ubuntu 24.04 限制系统级 pip 安装,导致子脚本报 `ModuleNotFoundError: No module named 'pymysql'`。
旧电脑上因 pymysql 已装到系统 Python 中故未暴露此问题。
## 修复
1. 在 `auto_update.py` 入口检测 `venv/bin/python3` 是否存在
2. 存在则赋值给 `PYTHON` 变量,否则回退 `python3`
3. `os.system()` 中改用 `{PYTHON}` 而非硬编码 `python3`
4. 同时修正新电脑 crontab 中 `python3` 改为 `venv/bin/python3`
## 涉及文件
- `auto_update.py`
- 新电脑 crontab