简体   繁体   English

Python请求和beautifulsoup模块未导入

[英]Python requests and beautifulsoup module not importing

I have installed python in my mac. 我已经在Mac中安装了python。 When I type python3 in terminal and then import requests and bs4 it imports it and run the program correctly. 当我在终端中键入python3 ,然后导入请求和bs4时,它将导入它并正确运行程序。 But when I run it on python file as python3 file_name.py , it gives the following error: 但是当我在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)

I got the same error with both python 3.5 and python 3.6. 我在python 3.5和python 3.6上都遇到了相同的错误。

The issue is that you named a module string.py so it's confusing the importer because the logging module is also trying to import something from the standard library module string.py . 问题是您为模块命名为string.py所以它使导入器感到困惑,因为日志记录模块还试图从标准库模块string.py导入某些string.py This causes an issue known as "name shadowing" where your locally defined module is loaded instead of the the standard library module. 这会导致一个称为“名称屏蔽”的问题,在该问题中将加载本地定义的模块而不是标准库模块。

When your version of string.py gets imported it triggers the code which is causing your error. 导入您的string.py版本时,它将触发导致您出错的代码。

As an easy fix, try to rename your string.py module to something else. 作为一个简单的解决方法,请尝试将string.py模块重命名为其他名称。

For more info on name shadowing check out the "The name shadowing trap" section of this link: http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html 有关名称隐藏的更多信息,请查看此链接的“名称隐藏陷阱”部分: http : //python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM