【问题标题】:Unable to install libraries with pip due to outdated BeautifulSoup由于过时的 BeautifulSoup,无法使用 pip 安装库
【发布时间】:2020-01-10 16:26:27
【问题描述】:

命令

pip install pyteaser

产生这个错误:

Collecting pyteaser
  Using cached https://files.pythonhosted.org/packages/d4/7a/310592c6e7998440e56a8650446ecf3ded076431415c60f0f3b946b54462/pyteaser-2.0.tar.gz
Requirement already satisfied: Pillow in /usr/lib/python3/dist-packages (from pyteaser) (5.1.0)
Requirement already satisfied: lxml in /home/abhishek/PycharmProjects/learning-python/venv/lib/python3.6/site-packages (from pyteaser) (4.4.2)
Collecting cssselect (from pyteaser)
  Using cached https://files.pythonhosted.org/packages/3b/d4/3b5c17f00cce85b9a1e6f91096e1cc8e8ede2e1be8e96b87ce1ed09e92c5/cssselect-1.1.0-py2.py3-none-any.whl
Collecting jieba (from pyteaser)
  Using cached https://files.pythonhosted.org/packages/b2/c8/e0785494690876adbb5ac364eb148a240b738a614d7b39bf8dba8a7ad066/jieba-0.41.tar.gz
Collecting beautifulsoup (from pyteaser)
  Using cached https://files.pythonhosted.org/packages/40/f2/6c9f2f3e696ee6a1fb0e4d7850617e224ed2b0b1e872110abffeca2a09d4/BeautifulSoup-3.2.2.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pycharm-packaging/beautifulsoup/setup.py", line 3
        "You're trying to run a very old release of Beautiful Soup under Python 3. This will not work."<>"Please use Beautiful Soup 4, available through the pip package 'beautifulsoup4'."
                                                                                                        ^
    SyntaxError: invalid syntax

    ----------------------------------------

Command "python setup.py egg_info" failed with error code 1 in /tmp/pycharm-packaging/beautifulsoup/

还有命令

pip install pytldr

报告此错误:

pip install pytldr
Collecting pytldr
  Using cached https://files.pythonhosted.org/packages/70/09/02ed27061159e5f6d35abad4ec9ef3cac8e220093d61a2f7a42f53c9cb22/PyTLDR-0.1.4.tar.gz
Requirement already satisfied: numpy>=1.8.0 in /home/abhishek/PycharmProjects/learning-python/venv/lib/python3.6/site-packages (from pytldr) (1.18.1)
Requirement already satisfied: nltk>=3.0.0 in /home/abhishek/.local/lib/python3.6/site-packages (from pytldr) (3.4.5)
Requirement already satisfied: scipy>=0.13.2 in /home/abhishek/PycharmProjects/learning-python/venv/lib/python3.6/site-packages (from pytldr) (1.4.1)
Collecting scikit-learn>=0.15.2 (from pytldr)
  Using cached https://files.pythonhosted.org/packages/d1/48/e9fa9e252abcd1447eff6f9257636af31758a6e46fd5ce5d3c879f6907cb/scikit_learn-0.22.1-cp36-cp36m-manylinux1_x86_64.whl
Collecting goose-extractor>=1.0.25 (from pytldr)
  Using cached https://files.pythonhosted.org/packages/ab/f3/2e89688e4e1d8786e542ea460431f24f1392d885e60303dc20d476a96424/goose-extractor-1.0.25.tar.gz
Collecting networkx>=1.9.1 (from pytldr)
  Using cached https://files.pythonhosted.org/packages/41/8f/dd6a8e85946def36e4f2c69c84219af0fa5e832b018c970e92f2ad337e45/networkx-2.4-py3-none-any.whl
Requirement already satisfied: six in /home/abhishek/.local/lib/python3.6/site-packages (from nltk>=3.0.0->pytldr) (1.12.0)
Collecting joblib>=0.11 (from scikit-learn>=0.15.2->pytldr)
  Using cached https://files.pythonhosted.org/packages/28/5c/cf6a2b65a321c4a209efcdf64c2689efae2cb62661f8f6f4bb28547cf1bf/joblib-0.14.1-py2.py3-none-any.whl
Requirement already satisfied: Pillow in /usr/lib/python3/dist-packages (from goose-extractor>=1.0.25->pytldr) (5.1.0)
Requirement already satisfied: lxml in /home/abhishek/PycharmProjects/learning-python/venv/lib/python3.6/site-packages (from goose-extractor>=1.0.25->pytldr) (4.4.2)
Collecting cssselect (from goose-extractor>=1.0.25->pytldr)
  Using cached https://files.pythonhosted.org/packages/3b/d4/3b5c17f00cce85b9a1e6f91096e1cc8e8ede2e1be8e96b87ce1ed09e92c5/cssselect-1.1.0-py2.py3-none-any.whl
Collecting jieba (from goose-extractor>=1.0.25->pytldr)
  Using cached https://files.pythonhosted.org/packages/b2/c8/e0785494690876adbb5ac364eb148a240b738a614d7b39bf8dba8a7ad066/jieba-0.41.tar.gz
Collecting beautifulsoup (from goose-extractor>=1.0.25->pytldr)
  Using cached https://files.pythonhosted.org/packages/40/f2/6c9f2f3e696ee6a1fb0e4d7850617e224ed2b0b1e872110abffeca2a09d4/BeautifulSoup-3.2.2.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-9oa07p4e/beautifulsoup/setup.py", line 3
        "You're trying to run a very old release of Beautiful Soup under Python 3. This will not work."<>"Please use Beautiful Soup 4, available through the pip package 'beautifulsoup4'."
                                                                                                        ^
    SyntaxError: invalid syntax

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-9oa07p4e/beautifulsoup/
Note: you may need to restart the kernel to use updated packages.

编辑:

执行BeautifulSoup的命令后

pip install beautifulsoup4

回复是:

Requirement already satisfied: beautifulsoup4 in /home/abhishek/PycharmProjects/learning-python/venv/lib/python3.6/site-packages (4.8.2)
Requirement already satisfied: soupsieve>=1.2 in /home/abhishek/PycharmProjects/learning-python/venv/lib/python3.6/site-packages (from beautifulsoup4) (1.9.5)
Note: you may need to restart the kernel to use updated packages.

【问题讨论】:

  • 有什么问题?您有一个非常具体的错误消息,告诉您如何解决您的问题。您是否尝试按照说明进行操作?
  • 执行pip install beautifulsoup4,然后执行pip install pyteaserpip install pytldr
  • 是的,它说,“已经满意”。
  • @PMende:这些特定说明将适用于 使用 BeautifulSoup 的库,而不适用于 OP 正在使用的代码。
  • 查看此处提供的解决方案:stackoverflow.com/a/61509808>,它可能会有所帮助。

标签: python python-3.x beautifulsoup


【解决方案1】:

这里的问题是该库还没有(还)为 Python3 做好准备,您可以在对该库问题的评论中看到 here。这就是你问题的根源。如您所见,如果您安装 Python2.7 的库,则没有问题:

$ sudo apt install python-pip  # or https://pip.pypa.io/en/stable/installing/
$ python --version
Python 2.7.17
$ python -m pip install pytldr
Successfully installed Pillow-6.2.2 beautifulsoup-3.2.2 cssselect-1.1.0 decorator-4.4.2 goose-extractor-1.0.25 jieba-0.42.1 lxml-4.5.0 networkx-2.2 nltk-3.4.5 numpy-1.16.6 pytldr-0.1.4 scikit-learn-0.20.4 scipy-1.2.3 singledispatch-3.4.0.3 six-1.14.0

在这里,您可以看到一切正常,因为安装了 beautifulsoup-3.2.2 版本。对 Python3 尝试相同的命令会导致您发布的错误:

$ python3 --version
Python 3.6.9
$ python3 -m pip install pytldr
Collecting beautifulsoup
  Using cached BeautifulSoup-3.2.2.tar.gz (32 kB)
    ERROR: Command errored out with exit status 1:
     command: /usr/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-12aluvvq/beautifulsoup/setup.py'"'"'; __file__='"'"'/tmp/pip-install-12aluvvq/beautifulsoup/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-12aluvvq/beautifulsoup/pip-egg-info
         cwd: /tmp/pip-install-12aluvvq/beautifulsoup/
    Complete output (6 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-12aluvvq/beautifulsoup/setup.py", line 3
        "You're trying to run a very old release of Beautiful Soup under Python 3. This will not work."<>"Please use Beautiful Soup 4, available through the pip package 'beautifulsoup4'."
                                                                                                        ^
    SyntaxError: invalid syntax
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

错误信息很清楚:

您正在尝试在 Python 3 下运行非常旧的 Beautiful Soup 版本。这将不起作用。请使用 Beautiful Soup 4,可通过 pip 包“beautifulsoup4”获得。

仅仅安装 BeautifulSoup4 并不能解决问题,因为该库仍然使用 BeautifulSoup3 的代码。这是 pytldr 包的依赖项。

现在你有几个选择,也许更多:

  1. 不要使用这个库。
  2. 使用 Python2(但 it's the year 2020)。
  3. 已经有an open pull request 提供了据称已更新库以兼容 Python3 的代码。您可以使用那里提供的代码从源代码构建库(不保证这会起作用)或等到拉取请求被批准(或写信给作者)。
  4. Go through the forks 看看是否有人已经为 Python3 启动并运行了该库。
  5. 尝试自己更新库,也许可以在2to3 的帮助下。

您显然也可以直接安装this fork 而不是第 3 步。当然,这个分支可能在某些时候已经过时,或者它可能被修改为不再可用。还有installing a specific commit 的选项。但也不能保证这会奏效。

【讨论】:

  • 我看到的所有 pyteaser 分支都没有工作,因为没有一个可以删除依赖项。
  • 是的,因此我添加了一句话:“但也不保证这会奏效。” Your answer 更接近根本问题,并且已经很好地解释了 (+1) 为什么仍然存在问题。
【解决方案2】:

pyteaser 和 goose-extractor 库只使用 BeautifulSoup 3 库,它使用了一个与 BeautifulSoup 4 不同的名称。您不能只安装 BeautifulSoup 4,因为pyteasergoose-extractor 需要更新以使用更新的 BeautifulSoup 库。这将是更多的工作,因为这 2 个项目与 Python 3 都不兼容

除非您愿意自己完成所有升级工作或信任不完整的分叉,否则您基本上会在这些项目中使用 Python 2:

  • 不幸的是,即使是声称使 pyteaser Python 3 兼容的分支也留下了错误的依赖项。该项目实际上甚至没有在任何地方导入 BeautifulSoup,因此您必须克隆 Python 3 分支之一 (like this one) 并删除 beautifulsoup entry from the install_requires line

  • 对于 PyTLDR,this fork 可以安装在 Python 3 上:

    pip install git+https://github.com/vinodnimbalkar/PyTLDR.git#egg=pytldr
    

    它完全放弃了鹅。

    但是,您的里程数可能会有所不同,因为在我看来,转换有点随意。例如,对于 Unicode 文本(Python 3 中的默认值),normalization will never take placeifelif 分支现在都测试相同的条件)。虽然它删除了 goose-extractor 依赖,但 code still tries to import it if you referenced it 也不起作用。

如果你想直接使用goose-extractor,你可以替换成goose3。请注意,这也将模块名称更改为 goose3(因此上面的 PyTLDR 分支可能可以通过更改导入的模块名称来修复)。

【讨论】:

    猜你喜欢
    • 2021-06-01
    • 2016-04-19
    • 2017-10-20
    • 1970-01-01
    • 2021-12-29
    • 2019-02-27
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多