semi-newbie module namespace confusion

Fredrik Lundh fredrik at pythonware.com
Tue Oct 4 02:08:29 EDT 2005


pnau at sjm.com wrote:

> The main jist of the problem is that I'm trying add data from one
> module to a list and a dictionary in another module, and it doesn't
> seem to stick over there.
>
> The programs below seem to illustrate the issue.  (The results follow
> the programs).
> Why are the results different from runs of the two programs?  The
> output of nameSpaceTestB.py makes sense to me, but I just don't get why
> nameSpaceTestA.py does what it does.
>
> I'm sure the reason is very simple, but I just don't see it.  Please,
> can someone shed some light on the issue for me?

running a piece of python code as a script isn't the same thing as
importing it as a module:

    "If you run a module as a script (i.e. give its name to the inter-
    preter, rather than importing it), it's loaded under the module
    name __main__.

    If you then import the same module from your program, it's re-
    loaded and reexecuted under its real name. If you're not careful,
    you may end up doing things twice."

    http://effbot.org/zone/import-confusion.htm

try changing your "printing from"-statements to

    print "printing from", __name__

to see what you're really doing.

</F>






More information about the Python-list mailing list