The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

Views: 393,746β€…    Votes:  2β€…
Tags: python   share   global  
Link: πŸ” See Original Answer on Stack Overflow πŸ”—

Title: Using global variables between files?
ID: /2021/07/07/Using-global-variables-between-files_
Created: July 7, 2021
Upload: November 24, 2022    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false

Based on above answers and links within I created a new module called

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# ==============================================================================
# - Global variables shared by all modules.
# ==============================================================================

USER = None                 # User ID, Name, GUID varies by platform

def init():
    """ This should only be called once by the main module
        Child modules will inherit values. For example if they contain
            import global_variables as g
        Later on they can reference 'g.USER' to get the user ID.
    global USER

    import getpass
    USER = getpass.getuser()

# End of

Then in my main module I use this:

import global_variables as g

In another child imported module I can use:

import global_variables as g
# hundreds of lines later....

I’ve only spent a few minutes testing in two different python multiple-module programs but so far it’s working perfectly.

⇧ Weird walkie-talkie interference with Logitech unifying receiver When changing from windows 10 to linux, will the data still on pc or removed?  β‡©