To share global variables across modules in Python, the canonical approach is to create a dedicated configuration module (e.g., config.py or settings.py) that holds the shared variables.
Create a Configuration Module
Define your global variables in a separate module, such as config.py:
# config.py
app_name = "MyApp"
debug_mode = False
user_id = NoneImport and Use Across Modules
Import this module in any other file where you need access to the variables:
# main.py
import config
print(config.app_name) # Output: MyApp
config.debug_mode = True # Modify the global variable# utils.py
import config
def log_message():
if config.debug_mode:
print("Debug: This is a debug message")Key Points
Single Instance: Python loads each module only once. Changes made to the module object are reflected everywhere it's imported.
Modify with
globalin Functions: If you modify a global variable inside a function, use theglobalkeyword to ensure you're modifying the module-level variable.Avoid
from module import *: It can clutter the namespace and make code harder to debug.Use Setter Functions (Recommended): For better control and encapsulation, wrap variables in functions:
# config.py _debug_mode = False def set_debug(value): global _debug_mode _debug_mode = value def get_debug(): return _debug_mode
This approach ensures consistent, predictable access and modification of shared state across your application.
The problem is you defined myList from main.py, but subfile.py needs to use it. Here is a clean way to solve this problem: move all globals to a file, I call this file settings.py. This file is responsible for defining globals and initializing them:
# settings.py
def init():
global myList
myList = []
Next, your subfile can import globals:
# subfile.py
import settings
def stuff():
settings.myList.append('hey')
Note that subfile does not call init()— that 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
This way, you achieve your objective while avoid initializing global variables more than once.
Answer from Hai Vu on Stack OverflowThe problem is you defined myList from main.py, but subfile.py needs to use it. Here is a clean way to solve this problem: move all globals to a file, I call this file settings.py. This file is responsible for defining globals and initializing them:
# settings.py
def init():
global myList
myList = []
Next, your subfile can import globals:
# subfile.py
import settings
def stuff():
settings.myList.append('hey')
Note that subfile does not call init()— that 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
This way, you achieve your objective while avoid initializing global variables more than once.
See Python's document on sharing global variables across modules:
The canonical way to share information across modules within a single program is to create a special module (often called config or cfg).
config.py:
x = 0 # Default value of the 'x' configuration settingImport the config module in all modules of your application; the module then becomes available as a global name.
main.py:
import config print (config.x)
In general, don’t use from modulename import *. Doing so clutters the importer’s namespace, and makes it much harder for linters to detect undefined names.