I am trying to figure out global variables in python, and while there are many forum posts and examples, none of them really match what I need. Here is the problem: I am using a MySQL database to receive 11 different variables. These variables are used for control purposes, such as the opening and closing of valves at specific times and days. I need these variables to be global and they must be able to be used in many other files. At the moment, these variables only exist within the file that reads the database and assigns the variables a value.
Here's my question:
When I import the variables from the database, can I simply assign them to be global in that script? Like this:
cursor.execute ("select variable1,variable2,....variableN from transaction where TransactionNumber=(select MAX(TransactionNumber) from transaction)")
readfromdb= cursor.fetchone()
variable1,variable2,.....variableN=list(readfromdb)
global variable1,variable2...variableN.
And then import this file in each other file I'm using?
I have tried doing this method, but receive the following error:
SyntaxError: name 'TransactionNumber' is assigned to before global declaration
I am open to other suggestions to make this process more efficient/streamlined. But my only requirement is that the script that is currently reading the database MUST not change. It is a fundamental requirement that it continues reading and assigning the database variables. I won't bore you with details, but that script is vital to checking the data we receive from the database and making sure that it is correctly formatted for use since we will be receiving data from another computer.