MacOS下pip安装Python包提示找不到python3.7路径的问题求助
pip安装Python包时出现Python3.7路径错误的解决建议
问题描述
我在MacOS系统上使用pip安装部分Python包(例如requests-html)时,频繁遇到路径错误,提示找不到/usr/local/opt/python/bin/python3.7。当前Python版本为3.10.6,pip已升级至最新版。
安装requests-html时的报错:
louis@louis ~ % python3 -m pip install requests-html Collecting requests-html Using cached requests_html-0.10.0-py3-none-any.whl (13 kB) Collecting parse Downloading parse-1.19.0.tar.gz (30 kB) ERROR: Error [Errno 2] No such file or directory: '/usr/local/opt/python/bin/python3.7' while executing command python setup.py egg_info Preparing metadata (setup.py) ... error ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: '/usr/local/opt/python/bin/python3.7'
当前Python版本:
louis@Louis ~ % python3 --version Python 3.10.6
更新信息
检查/usr/local/opt/python/bin/目录,发现存在python3.7文件,但指定python3.10执行安装仍报错:
louis@Louis ~ % python3.10 -m pip install requests-html Collecting requests-html Using cached requests_html-0.10.0-py3-none-any.whl (13 kB) Collecting w3lib Downloading w3lib-2.1.1-py3-none-any.whl (21 kB) Collecting pyppeteer>=0.0.14 Using cached pyppeteer-1.0.2-py3-none-any.whl (83 kB) Collecting pyquery Using cached pyquery-1.4.3-py3-none-any.whl (22 kB) Collecting bs4 Using cached bs4-0.0.1.tar.gz (1.1 kB) ERROR: Error [Errno 2] No such file or directory: '/usr/local/opt/python/bin/python3.7' while executing command python setup.py egg_info Preparing metadata (setup.py) ... error ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: '/usr/local/opt/python/bin/python3.7'
解决建议
1. 修复Python3.7的可执行权限
目录中存在python3.7文件但无法调用,可能是缺少可执行权限,执行以下命令添加权限:
chmod +x /usr/local/opt/python/bin/python3.7
2. 重新创建Python3.7的软链接
如果python3.7是无效文件或指向错误路径,先确认系统中Python3.7的实际位置,再重新创建软链接:
# 先查找系统中Python3.7的真实路径 which python3.7 # 假设找到的路径是/usr/local/bin/python3.7,执行以下命令创建软链接 ln -sf /usr/local/bin/python3.7 /usr/local/opt/python/bin/python3.7
如果系统未安装Python3.7,可通过Homebrew安装:
brew install python@3.7
3. 强制pip使用当前Python环境
设置环境变量,让pip明确使用当前Python3.10的解释器,避免调用旧版本:
export PYTHONPATH=$(python3.10 -c "import sys; print(':'.join(sys.path))") export PATH=$(dirname $(which python3.10)):$PATH # 再次执行安装命令 python3.10 -m pip install requests-html
4. 使用虚拟环境隔离依赖
创建Python3.10专属的虚拟环境,在干净环境中安装包,避免全局环境冲突:
# 创建虚拟环境 python3.10 -m venv myenv # 激活虚拟环境 source myenv/bin/activate # 安装目标包 pip install requests-html
5. 修改包源码中的硬编码路径
部分旧包的setup.py会硬编码Python路径,可手动下载源码修改后本地安装:
# 下载requests-html源码包(不下载依赖) pip download requests-html --no-deps # 解压源码包 tar -xzf requests-html-0.10.0.tar.gz cd requests-html-0.10.0 # 替换setup.py中的Python3.7路径为当前Python3.10路径 sed -i '' 's/\/usr\/local\/opt\/python\/bin\/python3.7/\/usr\/local\/opt\/python\/bin\/python3.10/g' setup.py # 本地安装修改后的包 pip install .
内容的提问来源于stack exchange,提问作者L.S
相关产品推荐
相关产品推荐

