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

  • Home
  • SEARCH
  • 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 6605959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:20:43+00:00 2026-05-25T19:20:43+00:00

In a shell script one might do something like: myvar=’default_value’ source myfile.sh echo $myvar

  • 0

In a shell script one might do something like:

myvar='default_value'
source myfile.sh
echo $myvar

where myfile.sh is something like:

echo "myvar='special_value'" > myfile.sh

I know several “safe” ways of doing this in python, but the only way I can think of getting similar behaviour in Python (albeit ugly and unsafe) is the following:

myvar = 'default_value'
myimport = 'from %s import *' % 'myfile'
exec(myimport)
print(myvar)

where myfile.py is in ./ and looks like:

myvar = 'special_value'

I guess I would be happiest doing something like:

m.myvar = 'default_value'
m.update(__import__(myimport))

and have the objects in m updated. I can’t think of an easy way of doing this without writing something to loop over the objects.

  • 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-05-25T19:20:44+00:00Added an answer on May 25, 2026 at 7:20 pm

    There are two ways to look at this question.

    How can I read key-value pairs in python? Is there a standard way to do this?

    and

    How can I give full expressive power to a configuration script?

    If your question is the first; then the most obvious answer is to use the ConfigParser module:

    from ConfigParser import RawConfigParser
    parser = RawConfigParser({"myvar": "default_value"}) # Never use ConfigParser.ConfigParser!
    parser.read("my_config")
    myvar = parser.get("mysection", "myvar")
    

    Which would read a value from a file that looks like a normal INI style config:

    # You can use comments
    [mysection]
    myvar = special_value
    

    For the second option, when you really want to give full power of python to a configuration (which is rare, but sometimes necessary), You probably don’t want to use import, instead you should use execfile:

    config = {}
    execfile("my_config", config)
    myvar = config.get("myvar", "default_value")
    

    Which in turn will read from a python script; it will read the global variable myvar no matter how the script derives it:

    import random
    myvar = random.choice("special_value", "another_value")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a Shell Script that has a if function like this one: if
Does any one know how to execute below cmd through shell script? #!/bin/sh USERNAME=XYZ
I have a Bash shell script in which I would like to pause execution
From a shell script, how do I check if a directory contains files? Something
I have a Windows shell script that opens an application. I'd like to modify
I am writing a simple shell script that fetches output from one command and
I am trying to make a shell script to remove special characters, like {}()!,'
I have a executable shell script name project2. Following is one of the instruction
i m creating one application on ubuntu server in shell script. I write one
In shell script, how do I iterate through each line in an ASCII file

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.