Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Request Errors
#1
Hi, so I am trying to run this script -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import requests, json
 
# Enter your API key here
api_key = "2eacaa2c66e011c77935d17f0b7a72ed"
 
# base_url variable to store url
 
# Give city name
city_name = input("Enter city name : ")
 
# complete_url variable to store
# complete url address
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
 
# get method of requests module
# return response object
response = requests.get(complete_url)
 
# json method of response object
# convert json format data into
# python format data
x = response.json()
 
# Now x contains list of nested dictionaries
# Check the value of "cod" key is equal to
# "404", means city is found otherwise,
# city is not found
if x["cod"] != "404":
 
    # store the value of "main"
    # key in variable y
    y = x["main"]
 
    # store the value corresponding
    # to the "temp" key of y
    current_temperature = y["temp"]
 
    # store the value corresponding
    # to the "pressure" key of y
    current_pressure = y["pressure"]
 
    # store the value corresponding
    # to the "humidity" key of y
    current_humidiy = y["humidity"]
 
    # store the value of "weather"
    # key in variable z
    z = x["weather"]
 
    # store the value corresponding
    # to the "description" key at
    # the 0th index of z
    weather_description = z[0]["description"]
 
    # print following values
    print(" Temperature (in kelvin unit) = " +
                    str(current_temperature) +
        "\n atmospheric pressure (in hPa unit) = " +
                    str(current_pressure) +
        "\n humidity (in percentage) = " +
                    str(current_humidiy) +
        "\n description = " +
                    str(weather_description))
 
else:
    print(" City Not Found ")
But I am getting these errors, when I try to run them in the terminal -

Error:
Traceback (most recent call last): File "Weather_Map.py", line 8, in <module> import requests, json File "/Users/marcholder/opt/anaconda3/lib/python3.8/site-packages/requests/__init__.py", line 43, in <module> import urllib3 File "/Users/marcholder/opt/anaconda3/lib/python3.8/site-packages/urllib3/__init__.py", line 8, in <module> from .connectionpool import ( File "/Users/marcholder/opt/anaconda3/lib/python3.8/site-packages/urllib3/connectionpool.py", line 11, in <module> from .exceptions import ( File "/Users/marcholder/opt/anaconda3/lib/python3.8/site-packages/urllib3/exceptions.py", line 2, in <module> from .packages.six.moves.http_client import ( File "<frozen importlib._bootstrap>", line 991, in _find_and_load File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 655, in _load_unlocked File "<frozen importlib._bootstrap>", line 618, in _load_backward_compatible File "/Users/marcholder/opt/anaconda3/lib/python3.8/site-packages/urllib3/packages/six.py", line 203, in load_module mod = mod._resolve() File "/Users/marcholder/opt/anaconda3/lib/python3.8/site-packages/urllib3/packages/six.py", line 115, in _resolve return _import_module(self.mod) File "/Users/marcholder/opt/anaconda3/lib/python3.8/site-packages/urllib3/packages/six.py", line 82, in _import_module __import__(name) File "/Users/marcholder/opt/anaconda3/lib/python3.8/http/client.py", line 71, in <module> import email.parser File "/Users/marcholder/opt/anaconda3/lib/python3.8/email/parser.py", line 12, in <module> from email.feedparser import FeedParser, BytesFeedParser File "/Users/marcholder/opt/anaconda3/lib/python3.8/email/feedparser.py", line 27, in <module> from email._policybase import compat32 File "/Users/marcholder/opt/anaconda3/lib/python3.8/email/_policybase.py", line 9, in <module> from email.utils import _has_surrogates File "/Users/marcholder/opt/anaconda3/lib/python3.8/email/utils.py", line 33, in <module> from email._parseaddr import quote File "/Users/marcholder/opt/anaconda3/lib/python3.8/email/_parseaddr.py", line 16, in <module> import time, calendar File "/Users/marcholder/python/calendar.py", line 4, in <module> holidays = hapi.holidays({ File "/Users/marcholder/opt/anaconda3/lib/python3.8/site-packages/holidayapi/__init__.py", line 16, in holidays response = requests.get(url, params=parameters); AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import)
Or, when I run the script via the terminal in the Visual Studio Code editor, I get this -

Error:
Traceback (most recent call last): File "Weather_Map.py", line 8, in <module> import requests, json ImportError: No module named requests
This started happening yesterday, and I am not completely sure why. I don't know if it is worth noting, but I downloaded Python3.9 yesterday (I was previously using 3.8, although judging by some of these errors, it looks like I am still using 3.8?), could this be an issue? And if so, how do I rectify this? These errors also seems to happen when I try to run other Python files, which previously worked.

The implication of the second error I have posted is that I have not installed requests on the terminal, but I did, and that error still came up. I installed requests for both terminals used.

I hope that all makes sense.

Any help would be much appreciated!
Reply
#2
It is a problem with your python3

runs fine for me

Output:
brian@Esprimo-P400:/tmp$ python3 tmp.py Enter city name : berlin Temperature (in kelvin unit) = 274.91 atmospheric pressure (in hPa unit) = 1009 humidity (in percentage) = 86 description = broken clouds
Reply
#3
Thanks Axel_Erfurt, do you know how I can fix it?
Reply
#4
You have a file /Users/marcholder/python/calendar.py. I think this is your file and it is interfering with module with the same name calendar from the Standard Library. Rename/remove that file.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thank you buran, it works now!
Reply
#6
Why the rubbish variable names x, y and z instead of more meaningful ones like you have for other things?
Reply
#7
(Jan-07-2021, 03:17 PM)ndc85430 Wrote: Why the rubbish variable names x, y and z instead of more meaningful ones like you have for other things?

like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import requests, json
  
api_key = "2eacaa2c66e011c77935d17f0b7a72ed"
city_name = "Berlin" ###input("Enter city name : ")
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
  
response = requests.get(complete_url)
 
x = response.json()
 
if x["cod"] != "404":
 
    y = x["main"]
    current_temperature = f'{(float(y["temp"]) - 273.15):.1f}°C'
    current_pressure = f'{y["pressure"]} hPa'
    current_humidiy = f'{y["humidity"]}%'
    z = x["weather"]
    weather_description = z[0]["description"]
  
    print(f'Weather in {city_name}:\n\
Temperature: {current_temperature}\n\
atmospheric: {current_pressure}\n\
humidity: {current_humidiy}\n\
description: {weather_description}')
  
else:
    print(" City Not Found ")
Output:
Weather in Berlin: Temperature: 1.8°C atmospheric: 1010 hPa humidity: 80% description: light snow
Reply
#8
(Jan-07-2021, 04:41 PM)Axel_Erfurt Wrote: like this
well, I don't think @ndc85430 had this in mind... You still have x, y and z names, e.g. lines 10, 12, 14, etc.
ndc85430 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Linux, Python, C ++, Brain2 and errors. PiotrBujakowski 0 982 Jun-24-2024, 03:41 PM
Last Post: PiotrBujakowski
  When does Python detect Errors? stamp1t 1 2,269 Oct-21-2023, 05:53 PM
Last Post: deanhystad
  alternative to using request module in python vlearner 2 1,952 Feb-05-2022, 09:35 AM
Last Post: ibreeden
  how can I correct the Bad Request error on my curl request tomtom 8 7,159 Oct-03-2021, 06:32 AM
Last Post: tomtom
  Rmarkdown opened by python code - errors Rav013 0 2,680 Apr-27-2021, 03:13 PM
Last Post: Rav013
  ImportError: cannot import name 'Request' from 'request' abhishek81py 1 5,152 Jun-18-2020, 08:07 AM
Last Post: buran
  Pip Syntax Errors in CMD: Windows 10 and Python 3.8.1 jamesphopper 2 5,935 Feb-08-2020, 07:21 PM
Last Post: jamesphopper
  Python stops without errors shahgourav 4 3,615 Feb-04-2020, 11:44 PM
Last Post: micseydel
  Can the comments produce errors in python? newbieAuggie2019 9 6,413 Nov-26-2019, 12:19 AM
Last Post: micseydel
  Running into errors when installing pip and pip3 on python 2.7 and python 3.6 tej7gandhi 1 3,649 May-05-2019, 10:37 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020