无法弄清楚 beautifulsoup4 在我的 python 3 脚本中有什么问题

Cannot figure out what's wrong with beautifulsoup4 in my python 3 script

Traceback (most recent call last):   File "urlgrabber.py", line 1, in <module>
    from bs4 import BeautifulSoup   File "/Users/asdf/Desktop/Scraper/bs4/__init__.py", line 29, in <module>
    from .builder import builder_registry   File "/Users/asdf/Desktop/Scraper/bs4/builder/__init__.py", line 4, in <module>
    from bs4.element import (   File "/Users/asdf/Desktop/Scraper/bs4/element.py", line 5, in <module>
    from bs4.dammit import EntitySubstitution   File "/Users/asdf/Desktop/Scraper/bs4/dammit.py", line 13, in <module>
    import logging   File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/logging/__init__.py", line 28, in <module>
    from string import Template ImportError: cannot import name 'Template'

我的代码基本上可以从网站上删除 link。它工作正常,直到最近才开始在命令提示符下返回此错误。我正在使用 python 3. 我已经下载了 bs4 并与 python 一起更新了它。我仍然收到此错误。 Python/programming 还是个新手,我不确定如何解决这个问题。我把命令提示符的消息放在上面。之前的代码是标准的抓取代码,可以正常工作,但由于某种原因最近无法正常工作。我可以在新的 .py 文件上有 'from bs4 import BeautifulSoup',它会给我同样的错误:

File "bs4fml.py", line 1, in <module>
    from bs4 import BeautifulSoup
  File "/Users/asdf/Desktop/Scraper/bs4/__init__.py", line 29, in <module>
    from .builder import builder_registry
  File "/Users/asdf/Desktop/Scraper/bs4/builder/__init__.py", line 4, in <module>
    from bs4.element import (
  File "/Users/asdf/Desktop/Scraper/bs4/element.py", line 5, in <module>
    from bs4.dammit import EntitySubstitution
  File "/Users/asdf/Desktop/Scraper/bs4/dammit.py", line 13, in <module>
    import logging
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/logging/__init__.py", line 28, in <module>
    from string import Template

我搜索了您的错误消息并得出以下结论: https://bbs.archlinux.org/viewtopic.php?id=142036

看起来问题是 Python 试图从本地源而不是它自己的字符串库 import from string 因为你有一个类似命名的 string.py and/or string.pyc 工作目录中的文件。

尝试同时删除 string.py 和 string.pyc 文件并重新运行 您的脚本。