Skip to content

Python module global variable | Example code

To create a module global variable you just need to declare the variable as global in Python. Global variables are also accessible from outside the module by using module_name dot var_name.

Syntax

module_name.var_name

Python module global variable example

Simple example code the only global variables Python really has are module-scoped variables.

Let’s make a module-global variable just assign to a name.

First, the main.py file with the variable name msg.

msg = "Hello Main Class"

Now import class it into a Test.py and print the value of variable msg.

import main
print(main.msg)

Output:

Python module global variable

Do comment if you have any doubts or suggestions on this Python variable tutorial.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

1 thought on “Python module global variable | Example code”

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading