I have two files practice.py and weatherui.py.
I am importing practice in weatherui.py and using its functions. I now want to import weatherui into practice.py and specifically the class UiMainWindow which is in the weatherui.py.
weatherui.py (only the imports are mentioned):
from PyQt5 import QtCore, QtGui, QtWidgets
import practice
information = practice.get_weather_data(practice.generate_url(practice.new_url, practice.lat, practice.lon))
print(information)
class Ui_MainWindow(object):
practice.py:
from datetime import datetime
import requests
import mysql.connector
from weatherui import Ui_MainWindow //this causes the traceback
url = 'https://api.openweathermap.org/data/2.5/weather?q='
new_url = 'https://api.openweathermap.org/data/2.5/onecall?'
city_input = Ui_MainWindow.clickMethod().capitalize()
apiKey = '82e2832b8f0eac296c9e75cb93d03317'
theUrl = url + city_input + '&appid=' + apiKey;
r = requests.get(theUrl)
data = r.json()
lat = data['coord']['lat']
lon = data['coord']['lon']
city_name = data['name']
def generate_url(url, lat, lon):
return url + 'lat=' + str(lat) + '&lon=' + str(lon) + '&exclude={part}' + '&appid=' + apiKey
def get_weather_data(url):
r = requests.get(url)
data = r.json()
main_desc = data['current']['weather'][0]['main']
temp_main = data['current']['temp']
feels_like = data['current']['feels_like']
pressure = data['current']['pressure']
humidity = data['current']['humidity']
wind_speed = data['current']['wind_speed']
daily = data['daily']
dt1 = datetime.fromtimestamp(daily[1]['dt']).strftime('%d-%m-%Y')
dt2 = datetime.fromtimestamp(daily[2]['dt']).strftime('%d-%m-%Y')
dt3 = datetime.fromtimestamp(daily[3]['dt']).strftime('%d-%m-%Y')
dt4 = datetime.fromtimestamp(daily[4]['dt']).strftime('%d-%m-%Y')
dt5 = datetime.fromtimestamp(daily[5]['dt']).strftime('%d-%m-%Y')
dt6 = datetime.fromtimestamp(daily[6]['dt']).strftime('%d-%m-%Y')
dt7 = datetime.fromtimestamp(daily[7]['dt']).strftime('%d-%m-%Y')
icon_main = data['current']['weather'][0]['icon']
icon1 = daily[1]['weather'][0]['icon']
icon2 = daily[2]['weather'][0]['icon']
icon3 = daily[3]['weather'][0]['icon']
icon4 = daily[4]['weather'][0]['icon']
icon5 = daily[5]['weather'][0]['icon']
icon6 = daily[6]['weather'][0]['icon']
icon7 = daily[7]['weather'][0]['icon']
temp1 = daily[1]['temp']['day']
temp2 = daily[2]['temp']['day']
temp3 = daily[3]['temp']['day']
temp4 = daily[4]['temp']['day']
temp5 = daily[5]['temp']['day']
temp6 = daily[6]['temp']['day']
temp7 = daily[7]['temp']['day']
return temp_main, main_desc, icon_main+'.png', feels_like, pressure, humidity, wind_speed
def update_database(weather_information):
mydb = mysql.connector.connect(host="localhost", user="root", passwd="adarsh", database="example")
mycursor = mydb.cursor()
sql = "REPLACE INTO weather_information (city, temperature_main, descp, icon, feels_like, pressure, humidity, wind_speed) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
mycursor.execute(sql, weather_information)
mydb.commit()
def fetch_to_database():
mydb = mysql.connector.connect(host="localhost", user="root", passwd="adarsh", database="example")
mycursor = mydb.cursor(buffered=True)
sql = """SELECT * FROM weather_information WHERE city = %s"""
mycursor.execute(sql, (city_input,))
record = mycursor.fetchall()
# print(record)
for row in record:
city, temp_main, descp, icon, feels_like, pressure, humidity, wind_speed = row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]
mydb.commit()
return city, temp_main, descp, icon, feels_like, pressure, humidity, wind_speed
When I run above code I get this traceback:
I am unable to do so no matter what I try. First I thought it is a problem of circular dependency and I used all the recommended solutions to eliminate it but to no avail.
Erros i came across:
- AttributeError: 'module' object has no attribute
- Import Name Error
Note: weatherui.py is class based and practice.py is function based (no classes in practice.py)
Any help would be appreciated.