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 6038869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:17:06+00:00 2026-05-23T06:17:06+00:00

I have a problem similar to the first problem in this question , which

  • 0

I have a problem similar to the first problem in this question, which as far as I can see went unanswered.

I have a file “config.py” which contains a lot of parameters to be used by a class (this config.py file will change), however I can’t get these to propagate into the class via execfile.

In an example piece of code:

class Class():
    def __init__(self):
        execfile("config.py")
        print x

# config.py
x = "foo"

>>> t = Class()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in __init__
NameError: global name 'x' is not defined

Any help welcome, or any better methods of retrieving parameters from a file to be used in a class.

Many Thanks.

  • 1 1 Answer
  • 2 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-23T06:17:07+00:00Added an answer on May 23, 2026 at 6:17 am

    I don’t get what you’re trying to do (but i don’t like it, and this is just me) but to fix your problem do (test in python2.6):

    class Class():
        def __init__(self):
            execfile('config.py', locals())  # Not recommanded, maybe you want globals().
            print x
    

    But from the doc:

    Note

    The default locals act as described
    for function locals() below:
    modifications to the default locals
    dictionary should not be attempted.
    Pass an explicit locals dictionary if
    you need to see effects of the code on
    locals after function execfile()
    returns. execfile() cannot be used
    reliably to modify a function’s
    locals.

    and about :

    Any help welcome, or any better
    methods of retrieving parameters from
    a file to be used in a class.

    You can use import.

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

Sidebar

Related Questions

I have a similar problem to this post . I need to display up
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I'll preface this question with the note that I have looked at this similar
This question is very similar to that posed here . My problem is that
I have a question similar to In Java, why can't I declare a final
first time poster. Please be gentle. This topic may be similar to several other
First of all, sorry to post a question like this when so many other
Sometimes I come across this problem where you have a set of functions that
I have a login.jsp page which contains a login form. Once logged in the
I have a problem that I hope you can help/guide me. I saw a

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.