Visibility of global variables in imported modules
I've run into a bit of a wall importing modules in a Python script. I'll do my best to describe the error, why I run into it, and why I'm tying this particular approach to solve my problem (which I will describe in a second):
Let's suppose I have a module in which I've defined some utility functions/classes, which refer to entities defined in the namespace into which this auxiliary module will be imported (let "a" be such an entity):
def f(): print a
And then I have the main program, where "a" is defined, into which I want to import those utilities:
import module1 a=3 module1.f()
Executing the program will trigger the following error:
Traceback (most recent call last): File "Z:\Python\main.py", line 10, in <module> module1.f() File "Z:\Python\module1.py", line 3, in f print a NameError: global name 'a' is not defined
Similar questions have been asked in the past (two days ago, d'uh) and several solutions have been suggested, however I don't really think these fit my requirements. Here's my particular context:
I'm trying to make a Python program which connects to a MySQL database server and displays/modifies data with a GUI. For cleanliness sake, I've defined the bunch of auxiliary/utility MySQL-related functions in a separate file. However they all have a common variable, which I had originally defined inside the utilities module, and which is the cursor object from MySQLdb module. I later realised that the cursor object (which is used to communicate with the db server) should be defined in the main module, so that both the main module and anything that is imported into it can access that object.
End result would be something like this:
def utility_1(args): code which references a variable named "cur" def utility_n(args): etcetera
And my main module:
import MySQLdb, Tkinter db=MySQLdb.connect(#blahblah) ; cur=db.cursor() #cur is defined! from utilities_module import *
And then, as soon as I try to call any of the utilities functions, it triggers the aforementioned "global name not defined" error.
A particular suggestion was to have a "from program import cur" statement in the utilities file, such as this:
from program import cur #rest of function definitions
import Tkinter, MySQLdb db=MySQLdb.connect(#blahblah) ; cur=db.cursor() #cur is defined! from utilities_module import *
But that's cyclic import or something like that and, bottom line, it crashes too. So my question is:
How in hell can I make the "cur" object, defined in the main module, visible to those auxiliary functions which are imported into it?
Thanks for your time and my deepest apologies if the solution has been posted elsewhere. I just can't find the answer myself and I've got no more tricks in my book.
Globals in Python are global to a module , not across all modules. (Many people are confused by this, because in, say, C, a global is the same across all implementation files unless you explicitly make it
There are different ways to solve this, depending on your actual use case.
Before even going down this path, ask yourself whether this really needs to be global. Maybe you really want a class, with
f as an instance method, rather than just a free function? Then you could do something like this:
import module1 thingy1 = module1.Thingy(a=3) thingy1.f()
If you really do want a global, but it's just there to be used by
module1, set it in that module.
import module1 module1.a=3 module1.f()
On the other hand, if
a is shared by a whole lot of modules, put it somewhere else, and have everyone import it:
import shared_stuff import module1 shared_stuff.a = 3 module1.f()
… and, in module1.py:
import shared_stuff def f(): print shared_stuff.a
Don't use a
from import unless the variable is intended to be a constant.
from shared_stuff import a would create a new
a variable initialized to whatever
shared_stuff.a referred to at the time of the import, and this new
a variable would not be affected by assignments to
Or, in the rare case that you really do need it to be truly global everywhere, like a builtin, add it to the builtin module. The exact details differ between Python 2.x and 3.x. In 3.x, it works like this:
import builtins import module1 builtins.a = 3 module1.f()
★ Back to homepage or read more recommendations: