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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:56:39+00:00 2026-05-13T20:56:39+00:00

I looked at a number of existing questions about NameError exceptions when scripts are

  • 0

I looked at a number of existing questions about NameError exceptions when scripts are run with exec statements or execfile() in Python, but haven’t found a good explanation yet of the following behavior.

I want to make a simple game that creates script objects at runtime with execfile(). Below are 4 modules that demonstrate the problem (please bear with me, this is as simple as I could make it!). The main program just loads a script using execfile() and then calls a script manager to run the script objects:

# game.py

import script_mgr
import gamelib  # must be imported here to prevent NameError, any place else has no effect

def main():
  execfile("script.py")
  script_mgr.run()

main()

The script file just creates an object that plays a sound and then adds the object to a list in the script manager:

 script.py

import script_mgr
#import gamelib # (has no effect here)

class ScriptObject:
  def action(self):
    print("ScriptObject.action(): calling gamelib.play_sound()")
    gamelib.play_sound()

obj = ScriptObject()
script_mgr.add_script_object(obj)

The script manager just calls the action() function of each script:

# script_mgr.py

#import gamelib # (has no effect here)

script_objects = []

def add_script_object(obj):
  script_objects.append(obj)

def run():
  for obj in script_objects:
    obj.action()

The gamelib function is defined in a fourth module, which is the troublesome one to be accessed:

# gamelib.py

def play_sound():
  print("boom!")

The above code works with the following output:

mhack:exec $ python game.py
ScriptObject.action(): calling gamelib.play_sound()
boom!
mhack:exec $ 

However, if I comment-out the ‘import gamelib’ statement in game.py and uncomment the ‘import gamelib’ in script.py, I get the following error:

mhack:exec $ python game.py
ScriptObject.action(): calling gamelib.play_sound()
Traceback (most recent call last):
  File "game.py", line 10, in 
    main()
  File "game.py", line 8, in main
    script_mgr.run()
  File "/Users/williamknight/proj/test/python/exec/script_mgr.py", line 12, in run
    obj.action()
  File "script.py", line 9, in action
    gamelib.play_sound()
NameError: global name 'gamelib' is not defined

My question is: 1) Why is the import needed in the ‘game.py’ module, the one that execs the script? 2) Why doesn’t it work to import ‘gamelib’ from the module where it is referenced (script.py) or the module where it is called (script_mgr.py)?

This happens on Python 2.5.1

  • 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-13T20:56:39+00:00Added an answer on May 13, 2026 at 8:56 pm

    From the Python documentation for execfile:

    execfile(filename[, globals[, locals]])

    If the locals dictionary is omitted it defaults to the globals dictionary. If both dictionaries are omitted, the expression is executed in the environment where execfile() is called.

    There are two optional arguments for execfile. Since you omit them both, your script is being executed in the environment where execfile is called. Hence the reason the import in game.py changes the behaviour.

    In addition, I concluded the following behaviour of import in game.py and script.py:

    • In game.py import gamelib imports the gamelib module into both globals and locals. This is the environment passed to script.py which is why gamelib is accessible in the ScriptObject action method (accessed from globals).

    • In script.py import gamelib imports the gamelib module into locals only (not sure of the reason). So when trying to access gamelib from the ScriptObject action method from globals you have the NameError. It will work if you move the import into the scope of the action method as follows (gamelib will be accessed from locals):

      class ScriptObject:
          def action(self):
              import gamelib
              print("ScriptObject.action(): calling gamelib.play_sound()")
              gamelib.play_sound()
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've looked at a number of similar topics on SO, but haven't found one
I looked at SQL Server dateformat codes but I couldn't find dd.mm.yyyy hh:mm format
Has anyone looked at Yahoo's ASTRA ? It's fairly nifty, but I had some
I wrote a prime number generator using Sieve of Eratosthenes and Python 3.1. The
I've looked a number of sources: it seems not possible to declare a type
I've looked for some help on the reddit site itself, but I still can't
I have looked on the net as well as here but can't find an
I looked for the name of a procedure, which applies a tree structure of
I looked at the AdaCore site, as well as for A# (which now appears
I looked in Stevens , and in the Posix Programmer's Guide , and the

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.