Python请求和beautifulsoup模块不导入

时间:2016-12-29 07:36:21

标签: python macos python-3.x beautifulsoup python-requests

我在我的mac中安装了python。当我在终端中键入python3然后导入请求和bs4时,它会导入它并正确运行程序。 但是当我在python文件上以python3 file_name.py运行它时,它会出现以下错误:

 import requests
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/__init__.py", line 52, in <module>
    from .packages.urllib3.contrib import pyopenssl
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/__init__.py", line 27, in <module>
    from . import urllib3
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/urllib3/__init__.py", line 8, in <module>
    from .connectionpool import (
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 3, in <module>
    import logging
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 28, in <module>
    from string import Template
  File "/Users/dark_archer/Desktop/src/string.py", line 1, in <module>
    n1,n2=map(int,input().split())
ValueError: not enough values to unpack (expected 2, got 0)

我在python 3.5和python 3.6中都遇到了同样的错误。

1 个答案:

答案 0 :(得分:1)

问题在于您将模块命名为string.py,因此它使导入程序混乱,因为日志记录模块也尝试从标准库模块string.py导入某些内容。这会导致一个称为&#34;名称阴影&#34;加载本地定义的模块而不是标准库模块。

当您导入string.py版本时,会触发导致错误的代码。

作为一个简单的解决方法,请尝试将您的string.py模块重命名为其他内容。

有关名称阴影的更多信息,请查看&#34;名称阴影陷阱&#34;此链接的一部分:http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html