How To Share A Global Variable Across Files And Modules In Python

  • I do this not to repeat myself (DRY principle)
  • I am not using global keyword (no help here)
  • I am simplifying the approach from stack overflow
  • My use is in my short Jirc (Jira Card from Terminal) script
  • My global variable is a path to a config file that I need on various occasions in various places


  • Move all globals to a file, I call this file
  • As we know, the code (outside of functions) is executed upon import statement
  • This fact binds a variable upon import to the proper environment frame and voila you have access to the variable
  • This is simply how my looks like
config_file_path = "./jirc/jirc/config.json"
  • Then, every other file that wants to share variables needs to import settings
  • My preference is to use from settings import * form that removes the need to use the module prefix
  • You could use import settings and refer to the global variable with settings.variable_name to make it more explicit
  • After import, variables are readily available




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me: