WxXiong
WxXiong

Reputation: 25

How does my requests stopped working in python3?

I had this this python3 script running normally using requests.get. However if I switched to another directory other than the one I am working at this error pops up. I tried reinstall requests using pip3 and rename the file but neither worked for me.

Traceback (most recent call last):
  File "bs.py", line 2, in <module>
    import requests
  File "/usr/local/lib/python3.7/site-packages/requests/__init__.py", line 43, in <module>
    import urllib3
  File "/usr/local/lib/python3.7/site-packages/urllib3/__init__.py", line 8, in <module>
    from .connectionpool import (
  File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 3, in <module>
    import logging
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 40, in <module>
    import threading
  File "/Users/pavel/Documents/Code/threading.py", line 10, in <module>
    url = requests.get(host,headers = headers)
AttributeError: module 'requests' has no attribute 'get'

Upvotes: 1

Views: 100

Answers (1)

Atreyagaurav
Atreyagaurav

Reputation: 1195

File "/Users/pavel/Documents/Code/threading.py", line 10, in <module>
    url = requests.get(host,headers = headers)
AttributeError: module 'requests' has no attribute 'get'

Here,"/Users/pavel/Documents/Code/" is your working directory?

When importing something python searches the working directory first so, import threading from anywhere would try to import your threading.py which will make circular reference to requests. Right now requests module is also importing other modules so method get isn't yet defined. That's why the error.

Try renaming your script somthing else. You have to always be careful of the names like that, if you are going to use a module don't name your python script with the same name. Also be careful of other basic python modules.

Upvotes: 3

Related Questions