Python 3.12 下 Playwright 安装可行,需避开 pip 缓存和浏览器二进制下载失败:用 --no-cache-dir、切官方源、升级 setuptools;playwright install 失败时检查缓存目录、设下载镜像、勿用 sudo;验证需实际启动浏览器;测试必用 pytest-playwright 且版本对齐。

Python 3.12 下 Playwright 安装基本可行,但需避开 pip 缓存和浏览器二进制版本错配这两个最常卡住的点。
pip install playwright 会失败或不完整?检查这三点
Playwright 的 Python 包本身支持 3.12(自 playwright==1.42.0 起官方声明兼容),但实际安装失败往往不是语言版本问题,而是:
- 本地 pip 缓存了旧版 wheel(比如之前装过 3.11 环境下的包),导致 pip 误判兼容性 —— 执行
pip install --no-cache-dir playwright强制跳过缓存 - 你用的是国内镜像源(如清华、阿里云),但部分镜像未及时同步 3.12 兼容的 wheel —— 临时切回官方源:
pip install -i https://pypi.org/simple/ playwright - 某些 Linux 发行版默认 Python 3.12 的
distutils模块已被移除,而旧版 setuptools 会报错 —— 升级后再装:pip install -U setuptools pip
python -m playwright install 失败:浏览器二进制不认 3.12
这个命令本质是调用 Playwright 内置的下载器去拉 Chromium/Firefox/WebKit 的预编译二进制。它和 Python 版本无关,但容易因网络或权限中断。常见现象是报错 ERROR: Failed to download ... 或卡在 “Downloading chromium” 不动。
- 先确认是否真没下完:检查
~/.cache/ms-playwright/(Linux/macOS)或%LOCALAPPDATA%\ms-playwright\(Windows)目录下是否有非空子目录(如chromium-1234567) - 公司内网或校园网大概率被拦截,设环境变量走镜像:
PLAYWRIGHT_DOWNLOAD_HOST=https://npmmirror.com/mirrors/playwright - 不要用
sudo或管理员权限运行该命令 —— Playwright 默认写入用户目录,提权反而可能写到系统路径导致后续权限混乱
验证安装是否真正就绪:别只跑 import
仅执行 from playwright.sync_api import sync_playwright 成功不代表能用。必须验证浏览器可启动:
from playwright.sync_api import sync_playwright
<p>with sync_playwright() as p:</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill9965" title="macOS UI 自动化技能包 —— 用 Python 控制你的 Mac"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20dbae7c507.jpg" alt="macOS UI 自动化技能包 —— 用 Python 控制你的 Mac" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill9965" title="macOS UI 自动化技能包 —— 用 Python 控制你的 Mac" class="overflowclass">macOS UI 自动化技能包 —— 用 Python 控制你的 Mac</a>
<p class="overflowclass">让 macOS 自动化像说话一样简单 —— 用代码控制你的 Mac,无需学习 AppleScript
系统要求
macOS 14+ (Sonoma)
Python 3.9+
需要开启:辅助功能 + 屏幕录制权限</p>
</div>
<a rel="nofollow" href="/xiazai/skill9965" title="macOS UI 自动化技能包 —— 用 Python 控制你的 Mac" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><h1>关键:显式加 headless=False + slow_mo=500,方便观察是否真弹窗</h1><pre class="brush:php;toolbar:false;">browser = p.chromium.launch(headless=False, slow_mo=500)
page = browser.new_page()
page.goto("https://example.com")
print(page.title())
browser.close()
- 如果报
TimeoutError: Timeout 30000ms exceeded.,通常是浏览器进程被杀(杀毒软件拦截)、或chromium子进程无法加载共享库(Linux 上缺libnss3等依赖) - Windows 用户若看到黑窗口闪退,大概率是防病毒软件把
chrome-win\chrome.exe当恶意程序隔离了 —— 临时禁用或添加信任路径 - macOS 上首次运行可能弹“已损坏,无法打开”,需右键 → “打开”绕过 Gatekeeper,或执行:
xattr -d com.apple.quarantine ~/.cache/ms-playwright/chromium-*/chrome-mac/Chromium.app
初始化项目时,pytest-playwright 是刚需还是可选?
如果你写的是测试用例(而非单次脚本),pytest-playwright 不是可选插件,而是避免资源泄漏的底线配置。
- 裸用
sync_playwright()写测试,每次browser.close()忘写或异常退出就会残留进程,几次之后chromium占满内存 -
pytest-playwright提供browser、page等 fixture,自动管理生命周期 —— 安装后只需在conftest.py里加一行:pytest_plugins = ["pytest_playwright"] - 注意版本对齐:
pytest-playwright必须与playwright主版本一致(例如都用1.47.0),否则 fixture 注入失败,报fixture 'page' not found
最易被忽略的一点:Playwright 的浏览器二进制和 Python 包是解耦的。你可以用 Python 3.12 装最新 playwright,但 python -m playwright install 下载的仍是 Chromium v128(当前稳定版),它不关心你 Python 是 3.9 还是 3.12 —— 别在版本号上自我设限,重点盯住下载路径和进程行为。
Python免费学习笔记(深入):立即使用
在学习笔记中,你将探索 Python 的核心概念和高级技巧!










