
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Share Common Data Among Multiple Python Files
In Python, sharing common data such as constants, configuration settings or shared resources among multiple files is a routine task in modular application development. The data will be centralized in a dedicated module which allows it to be maintained in one place and imported wherever needed.
Before proceeding with various approaches to share data among multiple Python files, first we need to understand what is a module and what is shared data.
What is a Module?
A Module is a Python file (.py) that contains definitions such as variables, functions, classes or runnable code. By placing shared data in its own module we can import that module in other files and access the data directly.
What is Shared Data?
The Shared data refers to values or objects such as configuration parameters, constants or resource handles which are defined once and used across multiple modules. This pattern ensures consistency, avoids duplication and simplifies maintenance.
In this article we are going to see the various methods available in Python to share common data among multiple files.
Using a config.py
Module
The config.py module holds shared constants and configuration settings that can be imported by other Python files.
# config.py APP_NAME = "Tutorialspoint" VERSION = "1.0.0" DATABASE_URL = "postgresql://user:pass@localhost:5432/mydb"
Example
Here in this example, we are importing the config.py file to access the common data available in the config.py -
# main.py import config def start_app(): print(f"Starting {config.APP_NAME} v{config.VERSION}") print(f"Connecting to {config.DATABASE_URL}") start_app()
Following is the output of the above program -
Starting Tutorialspoint v1.0.0 Connecting to postgresql://user:pass@localhost:5432/mydb
Using Environment Variables
Environment variables help us to keep sensitive or environment-specific data, such as API keys, credentials or other secrets, out of our source code. We can read them in Python with the os module, or we can use a helper such as python-dotenv to load from a .env file
For example, let us assume we have the following data in the .env file -
# .env API_KEY=abcdef123456
Below is the settings.py, which is a helper file -
# settings.py import os from dotenv import load_dotenv load_dotenv() API_KEY = os.getenv("API_KEY")
Example
In this example, we are importing the setting.py file to use the API key defined in the .env file in our current program -
# app.py from settings import API_KEY def call_service(): print(f"Using API key: {API_KEY}") call_service()
Here is the output after executing the above program -
Using API key: abcdef123456
Using a JSON Configuration File
When we use an external JSON file to keep the configuration data, we will separate code from settings and make it easy to update without changing our Python code. We can load the JSON file at runtime and share the resulting dictionary across different modules.
For example, let's consider the below data as configuration data, which is saved in the external JSON file, config.json -
# config.json { "app_name": "Tutorialspoint", "version": "1.0.0", "features": ["auth", "logging", "analytics"] }
Following is the loader.py file, which uses the above-mentioned configuration file -
# config_loader.py import json with open("config.json", "r") as f: settings = json.load(f)
Example
Here in the example, we are using the configuration data along with the settings data by importing the settings.py file in our current Python program -
# run.py from config_loader import settings print(f"Running {settings['app_name']} v{settings['version']}") print("Enabled features:", ", ".join(settings["features"]))
Below is the output of the above program -
Running Tutorialspoint v1.0.0 Enabled features: auth, logging, analytics
Using a Singleton Configuration Class
A singleton configuration class ensures that only one instance of our configuration exists throughout the application. Any module that imports it receives the same object with shared settings.
Following is the configuration file -
# singleton_config.py class Config: _instance = None def __new__(cls): if cls._instance is None: cls._instance = super().__new__(cls) cls._instance.app_name = "Tutorialspoint" cls._instance.version = "1.0.0" return cls._instance
Example
In this example, we are using the above-created singleton configuration file to get the configuration data in our current program -
# usage.py from singleton_config import Config cfg1 = Config() cfg2 = Config() print(cfg1 is cfg2) # True print(cfg1.app_name, cfg2.version)
Following is the output of the above program -
True Tutorialspoint 1.0.0