Using global variables between files?

I am a bit confused about how global variables work. I have a large project with about 50 files, and I need to define global variables for all these files.

What I did was define them in my main.py projects, as shown below:

 # ../myproject/main.py # Define global myList global myList myList = [] # Imports import subfile # Do something subfile.stuff() print(myList[0]) 

I am trying to use myList in subfile.py as below

 # ../myproject/subfile.py # Save "hey" into myList def stuff(): globals()["myList"].append("hey") 

In another way, I tried, but didnโ€™t work either

 # ../myproject/main.py # Import globfile import globfile # Save myList into globfile globfile.myList = [] # Import subfile import subfile # Do something subfile.stuff() print(globfile.myList[0]) 

And inside subfile.py I had this:

 # ../myproject/subfile.py # Import globfile import globfile # Save "hey" into myList def stuff(): globfile.myList.append("hey") 

But then again, this did not work. How do I implement this? I understand that it cannot work when these two files really do not know each other (the necessary subfile does not know the basic information), but I canโ€™t figure out how to do this without using an io letter or pickle, which I donโ€™t want this do.

+129
python share globals
Oct 23 '12 at 15:54
source share
6 answers

The problem is that you defined myList from main.py , but subfile.py should use it. Here is an easy way to solve this problem: move all global variables to a file, I call this file settings.py . This file is responsible for the definition of globals and their initialization:

 # settings.py def init(): global myList myList = [] 

Then your subfile can import the global variables:

 # subfile.py import settings def stuff(): settings.myList.append('hey') 

Note that subfile does not call init() - this task belongs to main.py :

 # main.py import settings import subfile settings.init() # Call only once subfile.stuff() # Do stuff with global var print settings.myList[0] # Check the result 

Thus, you achieve your goal by avoiding the initialization of global variables more than once.

+226
Oct 23 '12 at 16:16
source share

See the Python doc on sharing global variables between modules :

The canonical way of exchanging information between modules within the same program is to create a special module (often called config or cfg).

config.py:

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

Import the configuration module into all modules of your application; the module becomes available as a global name.

main.py:

 import config print config.x 

or

 from config import x print x 

It will also allow you to dynamically set such a variable:

 import config config.x = my_function 



In general, do not use import modulo *. This clutters the importers namespace and makes it difficult for linter to detect undefined names.

+34
Aug 12 '17 at 4:24 on
source share

You might think that Python global variables are "modular" variables - and therefore they are much more useful than traditional "global variables" from C.

The global variable is actually defined in the __dict__ module and can be accessed from outside this module as a module attribute.

So in your example:

 # ../myproject/main.py # Define global myList # global myList - there is no "global" declaration at module level. Just inside # function and methods myList = [] # Imports import subfile # Do something subfile.stuff() print(myList[0]) 

and

 # ../myproject/subfile.py # Save "hey" into myList def stuff(): # You have to make the module main available for the # code here. # Placing the import inside the function body will # usually avoid import cycles - # unless you happen to call this function from # either main or subfile body (ie not from inside a function or method) import main main.mylist.append("hey") 
+19
Oct 23
source share

Using from your_file import * should fix your problems. It defines everything so that it is globally accessible (with the exception of local variables in the import, of course).

eg:

 ##test.py: from pytest import * print hello_world 

and

 ##pytest.py hello_world="hello world!" 
+11
Oct 23 '12 at 15:59
source share

The answer to Hai Vu works great, just one comment:

If you use the global module in another module and want to dynamically set the global mode, pay attention to importing other modules after setting global variables, for example:

 # settings.py def init(arg): global myList myList = [] mylist.append(arg) # subfile.py import settings def print(): settings.myList[0] # main.py import settings settings.init("1st") # global init before used in other imported modules # Or else they will be undefined import subfile subfile.print() # global usage 
+7
Dec 23 '15 at 9:23
source share

Your second attempt will work just fine and is actually a really good way to handle the variable names you want globally. But you have a name error in the last line. Here's how it should be:

 # ../myproject/main.py # Import globfile import globfile # Save myList into globfile globfile.myList = [] # Import subfile import subfile # Do something subfile.stuff() print(globfile.myList[0]) 

See the last line? myList is the attr of the globfile, not the subfile. This will work the way you want.

Mike

+3
Oct 23
source share



All Articles