global variables in imported modules

vsoler vicente.soler at gmail.com
Sun May 16 17:42:40 EDT 2010


Taken from www.python.org, FAQ 2.3 How do I share global variables
across modules?

config.py:

x = 0   # Default value of the 'x' configuration setting


mod.py:

import config
config.x = 1


main.py:

import config       # try removing it
import mod
print config.x

The example, such as shown in the website, works perfectly well.
However, I don't fully understand why I have to import config in
main.py, since it has already been imported by mod.py.

As the website explains, there is only one module namespace for each
module, and mod.py has aleady created the config namespace by
importing it. Why should I import it again in main.py if that
namespace already exists?

If I remove ->   import config       # try removing it     in main.py,
the application does not run

What am I missing?



More information about the Python-list mailing list