简体   繁体   中英

Requests Module not working with Python3 but works with python

It shows I have requests module already installed when I enter: pip3 install requests

Response:

Requirement already satisfied: requests in /usr/local/lib/python3.7/site-packages (2.24.0)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/site-packages (from requests) (2.9)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/site-packages (from requests) (1.25.8)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/site-packages (from requests) (2019.11.28)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/site-packages (from requests) (3.0.4)

So when I created a file named http.py with code:

import requests

url = "https://google.com"
response = requests.get(url)
print(f"your request to {url} came back w/ status code {response.status_code}")

I get an error saying:

Traceback (most recent call last):
  File "23_Making-HTTP-Requests/http.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 7, in <module>
    from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
  File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 11, in <module>
    from .exceptions import (
  File "/usr/local/lib/python3.7/site-packages/urllib3/exceptions.py", line 2, in <module>
    from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 668, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 638, in _load_backward_compatible
  File "/usr/local/lib/python3.7/site-packages/urllib3/packages/six.py", line 199, in load_module
    mod = mod._resolve()
  File "/usr/local/lib/python3.7/site-packages/urllib3/packages/six.py", line 113, in _resolve
    return _import_module(self.mod)
  File "/usr/local/lib/python3.7/site-packages/urllib3/packages/six.py", line 82, in _import_module
    __import__(name)
  File "/Users/colechalland/Code/Python-3/Modern_Python3_Bootcamp/23_Making-HTTP-Requests/http.py", line 5, in <module>
    response = requests.get(url)
AttributeError: module 'requests' has no attribute 'get'

But when I run this code with python2:

import requests

url = "https://google.com"
response = requests.get(url)
print(response.status_code)

I get no errors and prints out: 200

I have tried uninstalling and reinstalling requests with pip3 but that doesn't fix the problem. The weird thing is that it works in when I run python3 in the terminal with the same code:

Python 3.7.3 (default, Jun 19 2019, 07:38:49)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> url = "https://google.com"
>>> response = requests.get(url)
>>>
>>> print(f"your request to {url} came back w/ status code {response}")
your request to https://google.com came back w/ status code <Response [200]>
>>>

The problem is your http.py file, just rename it to something else. There is a http package and the underlying library ( urllib3 ) is importing some modules from it.

Do you have a file named requests.py in your cwd? If it is there then you should rename it and make sure that it does not match to any other module of python otherwise you will get an error.

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