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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:21:01+00:00 2026-06-06T11:21:01+00:00

Tried searching the site, but cannot find an answer to my problem: Lets say

  • 0

Tried searching the site, but cannot find an answer to my problem:

Lets say I have a module, named mymodule.py that contains:

def a():
    return 3

def b():
    return 4 + a()

Then the following works:

import mymodule
print(mymodule.b())

However, when I try defining the module contents dynamically:

import imp

my_code = '''
def a():
    return 3

def b():
    return 4 + a()
'''

mymodule = imp.new_module('mymodule')
exec(my_code, globals(), mymodule.__dict__)
print(mymodule.b())

Then it fails in function b():

Traceback (most recent call last):
File "", line 13, in <module>
File "", line 6, in b
NameError: global name 'a' is not defined

I need a way to preserve the hierarchical namespace searching in modules, which seems to fail unless the module resides on disk.

Any clues as to whats the difference?

Thanks,
Rob.

  • 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-06T11:21:03+00:00Added an answer on June 6, 2026 at 11:21 am

    You’re close. You need tell exec to work in a different namespace like so (see note at bottom for python 3.x):

    exec my_code in mymodule.__dict__
    

    Full example:

    import imp
    
    my_code = '''
    def a():
        return 3
    
    def b():
        return 4 + a()
    '''
    
    mymodule = imp.new_module('mymodule')
    
    exec my_code in mymodule.__dict__
    print(mymodule.b())
    

    This said, I’ve not used this before, so I’m not sure if there are any weird side-effects to this, but it looks like it works to me.

    Also, there’s a small blurb about ‘exec in …’ in the python docs here: http://docs.python.org/reference/simple_stmts.html#the-exec-statement

    Update

    The reason your original attempt didn’t work correctly is you were passing your current module’s globals() dictionary, which is different than the globals() your new module should use.

    This exec line also works (but isn’t as pretty as the ‘exec in …’ style):

    exec(my_code, mymodule.__dict__, mymodule.__dict__)
    

    Update 2: Since exec is now a function in python 3.x, it has no ‘exec in …’ style, so the line above must be used.

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

Sidebar

Related Questions

I tried searching and didn't find anything that fixed my problem. I have a
I have tried searching through related answers but can't quite find something that is
I tried searching in google but could not find any useful answer, if(verifier.length() >
Ok, I have tried searching around for this answer, but with no luck. I
Ok I've tried searching this site for other similar questions, but those that come
Ive tried searching for hours now and cannot find out why my code (aka,
I have tried searching on Google and also read the documentation but no success.
I have tried googling and searching for this issue on SO - but have
I have tried searching the database for a similar issue, but was unsuccessful in
I'm looking for some help. I've tried searching this site and have tried amending

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.