Drop requests dep and handle PEP 668 environments#1
Open
solfx wants to merge 1 commit intostormzhang:mainfrom
Open
Drop requests dep and handle PEP 668 environments#1solfx wants to merge 1 commit intostormzhang:mainfrom
solfx wants to merge 1 commit intostormzhang:mainfrom
Conversation
- Replace `requests` with stdlib `urllib.request` so the script runs with zero third-party deps on Linux / macOS, avoiding the PEP 668 "externally-managed-environment" failure on Homebrew Python 3.11+ and recent Debian/Ubuntu. - Make remaining installs (Windows-only `colorama` / `tzdata`) retry with `--user` and `--break-system-packages --user`, and degrade silently instead of exiting when install still fails. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
urllib.request替换requests,脚本在 Linux / macOS 上零第三方依赖即可运行,规避 Homebrew Python 3.11+ / 新版 Debian/Ubuntu 的 PEP 668 "externally-managed-environment" 报错。colorama/tzdata自动安装改为多策略(普通 →--user→--break-system-packages --user),装不上时静默降级而非直接退出。实际遇到的问题
在 macOS + Homebrew Python 3.14 的环境下直接运行
python ip_check.py,脚本检测到缺少requests,提示自动安装并得到用户确认,但pip install被 PEP 668 拦截:脚本随后走到
ensurepip --upgrade兜底分支,同样被拦截(引导出的 pip 依然受同一限制)并抛出CalledProcessError,最终打印"无法自动安装 pip"后退出。用户拿到一个能跑的脚本,却在第一步就卡住。受影响的环境不是个例,而是当前主流的"干净系统 Python":
为什么这么改
考虑过的几种方案,按"用户侧摩擦从高到低"排列:
venv/pipx:能解决问题,但每次换机器都要记住激活流程,对非开发者用户门槛高;脚本的定位是"开代理后一键检测",引入虚拟环境反而破坏了这个体验。--break-system-packages:Homebrew 官方 STRONGLY 不推荐,可能污染系统 Python,不能作为默认行为。requests依赖:脚本里一共只有 3 处requests.get(url, params=..., timeout=...).json(),标准库urllib.request+json几行就能覆盖,且 Python 3 自带。一次性根治,用户侧零感知。最终选 4。
requests带来的便利(连接池、session、重试)本脚本根本用不到,为这点便利换来 PEP 668 摩擦不划算。Windows-only 的
colorama/tzdata保留了 pip 安装路径,但做了两点加固:--user→--break-system-packages --user,覆盖 Windows 上 Store 版 Python 等类似受限场景;sys.exit(1),保证核心功能始终可用。Changes
ip_check.py:新增_http_get_json工具函数,替换三处requests.get(...).json()(get_public_info/get_ip_risk/get_stopforumspam)。_pip_install:依次尝试 普通 /--user/--break-system-packages --user,并在ensurepip引导后再试。_ensure_deps:只保留 Windows 下的可选增强包检测;所有失败路径改为降级提示,不再sys.exit(1)。Test plan
python3 ip_check.py运行成功,输出与原版一致。python3 -c \"import ast; ast.parse(open('ip_check.py').read())\"语法校验通过。colorama/tzdata的安装回退路径(未本地验证,逻辑层面兼容原有行为)。🤖 Generated with Claude Code