Python requests and beautifulsoup module not importing

Question

I have installed python in my mac. When I type python3in 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.


Show source
| osx   | python   | python-3.x   | beautifulsoup   | python-requests   2016-12-29 08:12 1 Answers

Answers ( 1 )

  1. 2016-12-29 08:12

    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

◀ Go back