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 -

import requests, json # Enter your API key here api_key = "2eacaa2c66e011c77935d17f0b7a72ed" # base_url variable to store url base_url = "http://api.openweathermap.org/data/2.5/weather?" # 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

import requests, json api_key = "2eacaa2c66e011c77935d17f0b7a72ed" base_url = "http://api.openweathermap.org/data/2.5/weather?" 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 1,441 Jun-24-2024, 03:41 PM
Last Post: PiotrBujakowski
  When does Python detect Errors? stamp1t 1 3,536 Oct-21-2023, 05:53 PM
Last Post: deanhystad
  alternative to using request module in python vlearner 2 2,437 Feb-05-2022, 09:35 AM
Last Post: ibreeden
  how can I correct the Bad Request error on my curl request tomtom 8 8,695 Oct-03-2021, 06:32 AM
Last Post: tomtom
  ImportError: cannot import name 'Request' from 'request' abhishek81py 1 6,078 Jun-18-2020, 08:07 AM
Last Post: buran
  Pip Syntax Errors in CMD: Windows 10 and Python 3.8.1 jamesphopper 2 7,349 Feb-08-2020, 07:21 PM
Last Post: jamesphopper
  Python stops without errors shahgourav 4 4,319 Feb-04-2020, 11:44 PM
Last Post: micseydel
  Can the comments produce errors in python? newbieAuggie2019 9 8,075 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 4,250 May-05-2019, 10:37 PM
Last Post: snippsat
  Errors in Python dttomoum 1 3,591 May-23-2018, 08:57 AM
Last Post: Larz60+

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.