Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8319707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:24:26+00:00 2026-06-08T22:24:26+00:00

I apologize if this is a basic question but I can’t seem to find

  • 0

I apologize if this is a basic question but I can’t seem to find the answer here or on Google. Basically I’m trying to create a single config module that would be available to all other modules imported in a python application. Of course it works if I have import config in each file but I would like to make my config dynamic based on the environment the application is running in and I’d prefer not to have to copy the logic into every file.

Here’s an example:

app.py:

import config
import submodule

# do other stuff

submodule.py:

print config.some_config_variable

But python of course complains that config isn’t defined.

I did find some stuff about global variables but that didn’t seem to work either. Here’s what I tried:

Edit I changed this to show that I’d like the actual config being imported to be dynamic. However I do currently have a static config modle for my tests just to figure out how to import globally and then worry about that logic

app.py

# logic here that defines some_dynamic_config
global config
config = __import__(some_dynamic_config)
import submodule

submodule.py

print config.some_config_variable

But config still isn’t defined.

I’m aware that I could create a single config.py and place logic to set the variables but I dislike that. I prefer the config file to just configuration and not contain a bunch of logic.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:24:27+00:00Added an answer on June 8, 2026 at 10:24 pm

    You’ve got to put your logic somewhere. Your config.py could be a module that determines which config files to load, something like this:

    #config.py
    
    import sys
    
    from common_config import *
    
    if sys.platform == 'darwin':
        from mac_config import *
    elif sys.platform == 'win32':
        from win32_config import *
    else:
        from linux_config import *
    

    With this approach, you can put common settings in common_settings.py, and platform-specific settings in their respective files. Since the platform-specific settings are imported after common_settings, you can also override anything in common_settings by defining it again in the platform-specific files.

    This is a common pattern used in Django settings files, and works quite well there.

    You could also wrap each import call with try... except ImportError: blocks if need be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is a basic question, but I can't seem to find an
I apologize if this question is too basic, but I just can't figure out
Apologies for so basic a question, but I can't find an answer anywhere. When
Apologies if this is a very basic question, but I am preparing to create
I apologize if this is a duh question. It seems like the answer should
I know this is a basic question, but I'm having a bit of trouble
This seems like such a basic question, so I apologize if it's already been
I recognise this is a popular question but I couldn't find anything that answered
Silverlight newbie here, so I apologize if this question isn't even worded correctly. I
Apologies for this basic question, but I am noob stumped. The below code works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.