Global variables in python between files

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I have read and attempted the answers here in stackoverflow but still cant seem to get this to work. Im have a file global.py and a file weather.py. Global holds the variables for parsing a url and storing the global in that file like this:

f = urllib2.urlopen( http://api.wunderground.com/api/MYAPI/forecast/q/04002.json )
json_string = f.read()
parsed_json = json.loads(json_string)
#location = parsed_json[ location ][ city ]
day0 = parsed_json["forecast"]["simpleforecast"]["forecastday"][0]
day1 = parsed_json["forecast"]["simpleforecast"]["forecastday"][1]
day2 = parsed_json["forecast"]["simpleforecast"]["forecastday"][2]

dayname_0 = day0[ date ][ weekday_short ]
high_0 = day0[ high ][ fahrenheit ]

and here is my weather.py file

from PyQt4.QtCore import Qt
from PyQt4.QtGui import QWidget, QApplication, QSplitter, QLabel, QVBoxLayout, QColor
import Adafruit_DHT
import urllib2
import wunderground

temp = QLabel("Todays High: %s" % (high_0), self)

When i run the code i get the error that global name high_0 is not defined. What am i doing wrong. When i import the wunderground at the top does that not give me the globals from that file as well?

Answers

You need to import the global file

from global import high_0

I would suggest you to rename this file though as global is a built-in keyword.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/36486838/global-variables-in-python-between-files

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils