简体   繁体   中英

Python requests and beautifulsoup module not importing

I have installed python in my mac. When I type python3 in terminal and then import requests and bs4 it imports it and run the program correctly. But when I run it on python file as python3 file_name.py , it gives the following error:

 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.

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 . 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.

As an easy fix, try to rename your string.py module to something else.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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