【问题标题】:Beautiful Soup Python Installation Not WorkingBeautiful Soup Python 安装不工作
【发布时间】:2016-02-16 01:45:55
【问题描述】:

在过去的一个小时里,我一直在尝试一切来安装漂亮的汤。

我使用以下安装但它不起作用

1) 下载包 2) 转到终端中的源文件夹并使用 pip setup.py install 进行安装

设置成功,我得到 Writing /Library/Python/2.7/site-packages/beautifulsoup4-4.1.3-py2.7.egg-info

但是,当我使用 PyCharm 导入时,出现以下错误:

from bs4 import BeautifulSoup
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 954, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 896, in _find_spec
  File "<frozen importlib._bootstrap_external>", line 1136, in find_spec
  File "<frozen importlib._bootstrap_external>", line 1112, in _get_spec
  File "<frozen importlib._bootstrap_external>", line 1093, in _legacy_get_spec
  File "<frozen importlib._bootstrap>", line 444, in spec_from_loader
  File "<frozen importlib._bootstrap_external>", line 530, in spec_from_file_location
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/beautifulsoup4-4.4.0-py3.5.egg/bs4/__init__.py", line 48
    'You are trying to run the Python 2 version of Beautiful Soup under Python 3. This will not work.'<>'You need to convert the code, either by installing it (`python setup.py install`) or by running 2to3 (`2to3 -w bs4`).'
                                                                                                       ^
SyntaxError: invalid syntax

我也尝试过使用 python3 和 2to3 来解释使用 python3.5 的上述情况。这不应该是问题,因为漂亮的汤现在与 python 3 兼容。

【问题讨论】:

  • 你的路径变量中添加了 python 3 吗?如果 python 2 在你的路径变量中,它将使用 pip 和 python 2 并为该版本安装包。

标签: python python-3.x beautifulsoup installation pycharm


【解决方案1】:

您似乎安装了错误版本的 BeautifulSoup 软件包。

您正在尝试在 Python 3 下运行 Python 2 版本的 Beautiful Soup

您可以使用 pip 重新安装 BeautifulSoup 包

pip install beautifulsoup4

或使用easy_install

easy_install beautifulsoup4

或使用pip 下载binary package

pip install beautifulsoup4-4.4.1-py3-none-any.whl

您也可以查看here了解更多详情

希望对你有帮助。

【讨论】:

  • 我想我在终端中安装了正确的版本,但是 PyCharm 中的包已经过时了。有没有可能两者不一样?
  • 我猜你的机器上可能安装了两个版本的 Python。如果安装的 BeautifulSoup 包是版本正确的,那么您的 PyCharm Python 解释器可能有问题。您可以查看项目设置以获取更多详细信息。
【解决方案2】:

1) 选择您的项目并按 ctrl + Alt + s(打开设置)。
2) 转到无标题项目,然后转到项目解释器。
3) 点击右侧的+号,安装所需的包。

来源:https://www.jetbrains.com/pycharm/help/installing-uninstalling-and-upgrading-packages.html

【讨论】:

  • 我检查了项目检查器,它说安装了漂亮的汤包。但是,当我从 bs4 import BeautifulSoup 运行时,它会返回上述错误。
  • 您的 PyCharm 运行配置可能使用与“项目解释器”不同的 Python。转到运行 -> 编辑配置并查看正在使用的 Python。
猜你喜欢
  • 1970-01-01
  • 2014-08-10
  • 2021-08-22
  • 2017-12-07
  • 1970-01-01
  • 2018-04-22
  • 2010-10-01
  • 2014-06-28
  • 1970-01-01
相关资源
最近更新 更多