Create global variable between modules – Python

In this tutorial, we will show you how you can create global variable and access them in different modules in Python.

First, you need to create a file, let’s say “config.py“. And write all your global variables in it.

# config.py

my_name = "Adnan"

Here, I have created a global variable my_name. In order to access it in any other module, simply import it. Then you can use them as normal variables.

# any_other_module.py

# method 1

from config import my_name

print(my_name)

There is another method to import the module.

# any_other_module.py

# method 2

import config

print(config.my_name)

That is how you can create global variable and use it in multiple modules in Python. Kindly let me know if you face any problem in following this.