【问题标题】:Need help dealing with beautifulsoup errors on python 2.7 and python 3需要帮助处理 python 2.7 和 python 3 上的 beautifulsoup 错误
【发布时间】:2020-06-29 02:47:22
【问题描述】:

我正在尝试设置 BeautifulSoup4,以便我可以运行以下命令:

from bs4 import BeautifulSoup

但是,当我去安装 BeautifulSoup4 时,我得到以下信息:

Defaulting to user installation because normal site-packages is not writeable. 
Requirement already satisfied: beautifulsoup4 in ./Library/Python/2.7/lib/python/site-packages (4.8.2)
Requirement already satisfied: soupsieve>=1.2 in ./Library/Python/2.7/lib/python/site-packages (from beautifulsoup4) (1.9.5)
Requirement already satisfied: backports.functools-lru-cache; python_version < "3" in ./Library/Python/2.7/lib/python/site-packages (from soupsieve>=1.2->beautifulsoup4) (1.6.1)

$ from bs4 import BeautifulSoup4
from: can't read /var/mail/bs4

在 Python3 中运行时出现此错误

Python 3.8.2 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import beautifulsoup4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'beautifulsoup4' from 'bs4' (/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/bs4/__init__.py)

我的电脑上同时安装了 Python 3 和 Python 2.7,但不知道如何让它识别 bs4

感谢您的建议!

【问题讨论】:

  • 你是如何安装 BeautifullSoup 的?使用 pip 或其他东西?
  • 这难道不是安装包和使用不同 Python 安装运行程序的典型问题吗?我建议使用虚拟环境。
  • 是的 - 我通过 pip 添加了它
  • 这能回答你的问题吗? install beautiful soup using pip

标签: python python-3.x python-2.7 beautifulsoup


【解决方案1】:

在一个系统上拥有多个 Python 版本可能会导致混乱,这是每个 Python 开发人员在某些时候都会遇到的问题。

要解决您的问题,请尝试使用python -m 命令安装bs4,例如python -m pip install beautifulsoup4。这样可以确保 pip 将包安装到您调用的 python 中。

听起来是时候了解virtual environments了。虚拟环境允许您为每个项目单独的 python 实例,因此您不会在包之间遇到冲突。

如果您想了解有关使用多个版本的 python 安装 BeautifulSoup4 的更多信息check out this question

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 2014-11-04
    • 2012-10-27
    • 1970-01-01
    相关资源
    最近更新 更多