Python中如何使用pyenv设置Python版本并解决pip安装BeautifulSoup4不识别的问题
(v365env) ➜ music163-spiders git:(master) ✗ python get_artists.py
Traceback (most recent call last):
File "get_artists.py", line 44, in <module>
get_artists(url)
File "get_artists.py", line 26, in get_artists
soup = BeautifulSoup(r.text, 'html5lib')
File "/Users/qk/.pyenv/versions/v365env/lib/python3.6/site-packages/bs4/__init__.py", line 198, in __init__
% ",".join(features))
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: html5lib. Do you need to install a parser library?
(v365env) ➜ music163-spiders git:(master) ✗ python --version
Python 3.6.5
(v365env) ➜ music163-spiders git:(master) ✗ pip install requests BeautifulSoup4
Requirement already satisfied: requests in /Users/qk/.pyenv/versions/3.6.5/envs/v365env/lib/python3.6/site-packages (2.20.1)
Requirement already satisfied: BeautifulSoup4 in /Users/qk/.pyenv/versions/3.6.5/envs/v365env/lib/python3.6/site-packages (4.6.3)
Requirement already satisfied: idna<2.8,>=2.5 in /Users/qk/.pyenv/versions/3.6.5/envs/v365env/lib/python3.6/site-packages (from requests) (2.7)
Requirement already satisfied: certifi>=2017.4.17 in /Users/qk/.pyenv/versions/3.6.5/envs/v365env/lib/python3.6/site-packages (from requests) (2018.10.15)
Requirement already satisfied: urllib3<1.25,>=1.21.1 in /Users/qk/.pyenv/versions/3.6.5/envs/v365env/lib/python3.6/site-packages (from requests) (1.24.1)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Users/qk/.pyenv/versions/3.6.5/envs/v365env/lib/python3.6/site-packages (from requests) (3.0.4)
(v365env) ➜ music163-spiders git:(master) ✗ python get_artists.py
Traceback (most recent call last):
File "get_artists.py", line 44, in <module>
get_artists(url)
File "get_artists.py", line 26, in get_artists
soup = BeautifulSoup(r.text, 'html5lib')
File "/Users/qk/.pyenv/versions/v365env/lib/python3.6/site-packages/bs4/__init__.py", line 198, in __init__
% ",".join(features))
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: html5lib. Do you need to install a parser library?
参考例子:
Python中如何使用pyenv设置Python版本并解决pip安装BeautifulSoup4不识别的问题
‘’‘
➜ ~ pyenv versions
* system (set by /Users/qk/.pyenv/version)
3.6.5
3.6.5/envs/v365env
v365env
’’’
帖子我看了,你这个问题其实挺典型的,就是pyenv环境没配好,导致pip装包的时候路径错乱。核心就两步:用pyenv正确切换Python版本,然后确保pip命令绑定到了当前激活的版本。
下面给你一个完整的操作流程和代码示例,你跟着做一遍应该就能解决。
1. 确认并设置pyenv的Python版本
首先,打开你的终端,运行以下命令来查看你系统里有哪些Python版本,以及当前激活的是哪个。
# 列出所有已安装的版本
pyenv versions
# 显示当前shell会话使用的Python版本(带*号的是当前激活的)
pyenv version
假设你想用Python 3.9.18,但发现当前激活的不是它。你需要先安装它(如果还没安装的话),然后设置为全局或本地版本。
# 安装指定版本的Python(例如3.9.18)
pyenv install 3.9.18
# 将该版本设置为全局默认版本(影响整个系统)
pyenv global 3.9.18
# 或者,更推荐的做法:只在当前项目目录下使用这个版本
cd /path/to/your/project
pyenv local 3.9.18
执行pyenv local后,它会在你的项目目录下生成一个.python-version文件,pyenv会自动读取这个文件来切换版本。
关键一步:重新初始化shell。 更改版本后,为了让路径生效,你需要重新加载你的shell配置,或者直接新开一个终端窗口。也可以运行:
eval "$(pyenv init -)"
2. 验证Python和pip的对应关系
版本切换后,最重要是检查python和pip命令是否指向了刚设置的版本。
# 检查Python解释器路径和版本
which python
python --version
# 检查pip的路径和版本,确保它和上面的python来自同一位置
which pip
pip --version
pip --version输出的第一行会显示它绑定到的Python路径,比如/Users/yourname/.pyenv/versions/3.9.18/bin/pip。这必须和which python的路径在同一个版本目录下。如果pip还指向系统自带的Python(比如/usr/bin/pip),那肯定装不上包。
3. 安装BeautifulSoup4
确认路径一致后,就可以用pip安装了。
# 直接安装最新版
pip install beautifulsoup4
# 如果需要指定版本
pip install beautifulsoup4==4.12.2
4. 验证安装
安装完成后,写个简单的脚本测试一下。
# test_bs4.py
import bs4
print(f"BeautifulSoup4 version: {bs4.__version__}")
print("Import successful!")
运行它:
python test_bs4.py
如果成功输出版本号,就说明一切正常。
总结一下: 问题根源通常是pyenv切换版本后,pip命令没跟着变。务必用which pip和pip --version确认它和当前Python版本是配对的。
一句话建议: 用pyenv local管理项目版本,并始终在切换版本后验证pip --version的路径。
➜ ~ pyenv versions
* system (set by /Users/qk/.pyenv/version)
# 你这显示的表示你当前所在位置用的还是系统自带的 python 啊,并没有用到你 pyenv 安装的版本。
" Couldn’t find a tree builder with the features you requested: html5lib"
这么明确的提示了吗… 缺啥就装啥啊。
确认你在你项目的目录下,确认使用你正确的 pyenv 或者 virtualenv 环境,
pip search html5lib # 先确认 pip 库里有没有,我确认过了,有的。
pip install html5lib # 装完它之后,再试吧
html5lib 并不是 beautifulsoap 的硬性依赖,要用的话得 pip install html5lib
(v365env) ➜ music163-spiders git:(master) ✗ pip install html5lib
Collecting html5lib
Using cached https://files.pythonhosted.org/packages/a5/62/bbd2be0e7943ec8504b517e62bab011b4946e1258842bc159e5dfde15b96/html5lib-1.0.1-py2.py3-none-any.whl
Collecting six>=1.9 (from html5lib)
Using cached https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Collecting webencodings (from html5lib)
Using cached https://files.pythonhosted.org/packages/f4/24/2a3e3df732393fed8b3ebf2ec078f05546de641fe1b667ee316ec1dcf3b7/webencodings-0.5.1-py2.py3-none-any.whl
Installing collected packages: six, webencodings, html5lib
Successfully installed html5lib-1.0.1 six-1.11.0 webencodings-0.5.1
(v365env) ➜ music163-spiders git:(master) ✗ pyenv versions
system
3.6.5
3.6.5/envs/v365env
* v365env (set by PYENV_VERSION environment variable)

