Python Forum
Getting "name 'get_weather' is not defined error and no json_data returned?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting "name 'get_weather' is not defined error and no json_data returned?
#1
I'm stumped again. Been trying different ways of getting this to work and its solution is evading me. LOL
Just learning and am running Python 3.9.7
My next hurdle is to get the api key from it once and store in config.ini permanently and then get the user interface window up and running.

from tkinter import *
from configparser import ConfigParser
import requests

name = input("Name?: ")

age = input("Age?: ")

weightLBS = input("Weight in lbs?: ")

diagnosed = input("Have you been diagnosed with MD by doctor?: ")

api_key = input("API key? ")

#Weather API json and data pull

url = "http//api.openweathermap.org/data/2.5/weather?q={},{}&appid={}"

city = input("City where you are?: ")

state = input("State where you are? ")

api_key = config_file = 'config.ini'

config = ConfigParser()

config.read('config_file')

#api_key = input(api_key)
#(config.ini[api_key][key])


def get_weather(city, state, api_key):
    result = requests.get_weather(url.format(city, state, api_key))  
    if result:
        print(result.json_data)

        
        json_data = result.json()
        city = json['name']
        temp_kelvin = json['main']['temp']
        temp_min_kelvin= json['main']['temp_min']
        temp_max_kelvin = json['main']['temp_max']
        temp_fahrenheit = (temp_kelvin - 273.15) * 9 / 5 + 32
        temp_min_fahrenheit = (temp_min_kelvin - 273.15) * 9 / 5 + 32
        temp_max_fahrenheit = (temp_max_kelvin - 273.15) * 9 / 5 + 32
        humidity = json['main']['humidity']
        pressure = json['main']['pressure' *  0.0295301]
        condition = json_data['weather'][0]['main']
        #max_press = int(json_data['main']['pressure_max'])
        #min_press = int(json_data['main']['pressure_min'])
        final = (name, city, state, age, weight, diagnosed, temp_fahrenheit, temp_min_fahrenheit, temp_max_fahrenheit, humidity, pressure, condition)
        return final

print(get_weather(city, state, api_key))       
Reply
#2
It is not clear to me what you are asking. You are getting an error message? Then please show the complete error stack trace (in error tags).
You import requests. But does "requests" have a method "get_weather"?
(Because in line 34 you call requests.get_weather().)
Reply
#3
I showed you a working function in your last Thread
Now you mess it up with serval errors one mention bye ibreeden and call json should be json_data.
All the globals values should not just get be used in function if there not are in function parameter.
As mention in last post do small teste as your function now it's not working at all,
then you stop and fix function before adding ConfigParser and GUI.

To show a example with ConfigParser using my code from last Thread.
As api_key is personal to you should not need to type in every time,have in api_key in the .ini file.
key.ini:
[key]
api_key = xxxxxxxxxxxxx
import requests
from configparser import ConfigParser

def get_weather(city, api_key):
    api = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"
    json_data = requests.get(api).json()
    return json_data

def get_api_key():
    config = ConfigParser()
    config.read('key.ini')
    api_key = config['key']['api_key']
    return api_key

if __name__ == '__main__':
    city = 'Winnsboro,Louisiana'
    weather = get_weather(city, get_api_key())
    print(weather['name'])
    print(weather['main']['temp'])
    print(weather[ 'weather'][0]['description'])
Output:
Winnsboro 295.99 overcast clouds
Reply
#4
Sorry, replied to wrong reply.
Reply
#5
(Sep-13-2021, 12:44 PM)snippsat Wrote: I showed you a working function in your last Thread
Now you mess it up with serval errors one mention bye ibreeden and call json should be json_data.
All the globals values should not just get be used in function if there not are in function parameter.
As mention in last post do small teste as your function now it's not working at all,
then you stop and fix function before adding ConfigParser and GUI.

To show a example with ConfigParser using my code from last Thread.
As api_key is personal to you should not need to type in every time,have in api_key in the .ini file.
key.ini:
[key]
api_key = xxxxxxxxxxxxx
import requests
from configparser import ConfigParser

def get_weather(city, api_key):
    api = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"
    json_data = requests.get(api).json()
    return json_data

def get_api_key():
    config = ConfigParser()
    config.read('key.ini')
    api_key = config['key']['api_key']
    return api_key

if __name__ == '__main__':
    city = 'Winnsboro,Louisiana'
    weather = get_weather(city, get_api_key())
    print(weather['name'])
    print(weather['main']['temp'])
    print(weather[ 'weather'][0]['description'])
Output:
Winnsboro 295.99 overcast clouds

I can copy/paste your code into my VS code as a new file and use the identical config.ini and get errors. I don't know if I don't have something set up correctly in VS code or what. The only difference is that I want to input the city and state in because I travel and need to change locations. In the error at the bottom it shows Python 37, I only have 3.9. installed.

import requests
from configparser import ConfigParser

city = input("City where you are?: ")

state = input("State where you are? ")

##Weather API json and data
def get_weather(city, api_key):
    api = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"
    json_data = requests.get(api).json()
    return json_data
 
def get_api_key():
    config = ConfigParser()
    config.read('key.ini')
    api_key = config['key']['api_key']
    return api_key
 
if __name__ == '__main__':
    city = 'Winnsboro, Texas'
    weather = get_weather(city, get_api_key())
    print(weather['name'])
    print(weather['main']['temp'])
    print(weather[ 'weather'][0]['description'])
I get this error now.

City where you are?: Winnsboro
State where you are? Texas
Traceback (most recent call last):
File "c:/Users/xxxxxxxxxxxxx.py", line 22, in <module>
weather = get_weather(city, get_api_key())
File "c:/Users/xxxxxxxxxxxxxx.py", line 17, in get_api_key
api_key = config['key']['api_key']
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\configparser.py", line 958, in __getitem__
raise KeyError(key)
KeyError: 'key'
Reply
#6
(Sep-13-2021, 03:01 PM)trthskr4 Wrote: In the error at the bottom it shows Python 37, I only have 3.9. installed.
Click down in left corner then Python: Select Interpreter will show up an choice Python 3.9
Now it use version the come with Microsoft Visual Studio stand alone,and not the same as Vs Code.
Some setup tips here VS Code from start

You most have key.ini in same folder as you run script,or give path where it is.
Add input last not before the function,so like this.
import requests
from configparser import ConfigParser

def get_weather(city, api_key):
    api = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"
    json_data = requests.get(api).json()
    return json_data

def get_api_key():
    config = ConfigParser()
    config.read('key.ini')
    api_key = config['key']['api_key']
    return api_key

if __name__ == '__main__':
    city = input("City where you are?: ")
    state = input("State where you are? ")
    city = f'{city},{state}'
    weather = get_weather(city, get_api_key())
    print(weather['name'])
    print(weather['main']['temp'])
    print(weather[ 'weather'][0]['description'])
Test.
# Version
G:\div_code\answer\weather
λ python -V
Python 3.9.5

# Files in folder
G:\div_code\answer\weather
λ ls
key.ini  open_weather.py

# Run
G:\div_code\answer\weather
λ python open_weather.py
City where you are?: Houston
State where you are? Texas
Houston
296.82
mist
Reply
#7
I finally got this to work beautifully. I uninstalled Python 3.9.7 and installed 3.9.6 and set it back up and voila. Thanks for all your help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Libraries installed with pipenv, but ModuleNotFoundError returned jpncsu 2 559 Sep-06-2021, 07:24 PM
Last Post: jpncsu
  Error when refering to class defined in 'main' in an imported module HeRo 2 835 Apr-13-2021, 07:22 PM
Last Post: HeRo
  Why does lambda throw 'name value_o is not defined' error? karabakh 3 885 Dec-14-2020, 05:45 PM
Last Post: karabakh
  name error "name"is not defined MaartenRo 1 1,329 Jul-28-2020, 02:39 AM
Last Post: bowlofred
  Name Error: name 'Stockton' is not defined Pinokchu 3 1,101 Jun-13-2020, 02:48 PM
Last Post: Yoriz
  python library not defined in user defined function johnEmScott 2 1,088 May-30-2020, 04:14 AM
Last Post: DT2000
  error ,,name append is not defined'' Killdoz 1 2,157 May-24-2020, 06:23 PM
Last Post: bowlofred
  name error:name 'clf' is not defined saiankith 3 4,728 Mar-31-2020, 06:49 PM
Last Post: jefsummers
  Exception: Returned Type Mismatch Error devansing 1 2,501 Mar-06-2020, 07:26 PM
Last Post: ndc85430
  Error: variable can not be defined julio2000 2 1,686 Feb-09-2020, 08:51 PM
Last Post: julio2000

Forum Jump:

User Panel Messages

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