import error?

Peter Hansen peter at engcorp.com
Thu Oct 16 11:18:54 EDT 2003


Tom wrote:
> 
> I didn't use the interactive interpreter. I don't now why it says
> <string> and "?". I just use the window (python shell) for output. 

Ah, that's probably it.  I don't use the Python shell window, but
simply run stuff from the DOS command line.  Much simpler (for me)
and doesn't have problems such as the above with <string> and ?.

If you can do that (open DOS window, change to directory where your
files are saved, and execute the main one directly by typing a
command like "python nameofmainmodule.py") you might learn a little
more, or maybe get a more helpful traceback to post.  (That will
work only if "python.exe" is in your PATH, so it might be a bit
of work for you to set that up if you don't already know how.  It's
described in the FAQ at www.python.org if you need help there.)

> code is strctured like this: My main program does not have any classes
> or functions. It is supposed to run all the way through and calls
> different modules for the different tasks. 

Not sure what this means... you can't "call" a module, you can only
import it.  Import it normally (i.e. "import modulexxx") means that
a new module object is created, a reference is inserted into the
sys.modules dictionary, all the statements in that module are executed, 
and then in the namespace of the importing module (the one where the 
import statement occurred) you get a name "modulexxx" with a binding 
to the newly created module object.

If you use the "from module import *" form, all the above happens but
instead of a single new name in the importing module, you get *all*
the names that are defined at the top level in the imported module.

If you are using this latter form ("from module import *") for all
your imports, you can get into trouble in lots of different ways,
including that each import will overwrite ("rebind") any names that
were already imported from older modules that were imported.  And
that's probably the least harmless thing that could happen.

I think probably the way you have structured your code is very awkward
and is leading to the troubles you're having, though I can't say
exactly how that would be just yet.

I strongly recommend removing *all* "from module import *" forms
and switching to the simpler "import module" form.  You will of
course need to change all code that assumes a name can be used
"unadorned" with its module name.  For example, after the 
"from IniMod import *" statement you try using the "initialize_C" 
class (which doesn't sound much like a class... are you sure it's a 
class and not a function?).  You would have to change that line
to read "ReIni = IniMod.initialize_C()" instead, and do the same
thing with all other such uses.

This might be a lot of work, but the more work it appears to be,
the more your code needs it!

I'm also fairly certain that after such a change, even if the problem
still exists, the error messages will point us to the problem in a
much more direct way.  The problem with the "from xxx import *" form
is that it blows away all the safety benefits of namespaces, and 
makes it next to impossible to troubleshoot certain problems.  I
think you may be encountering just such a problem because of the
way you've structured your application.

> I don't know if this helps or just confuses more. :-) I hope not. :-)

Mostly confused more :-), but with this kind of problem that's probably
to be expected.  The only thing that might have helped more is 
better descriptions of *which* module you were talking about whenever
you said "this module" or "the other module"...

-Peter




More information about the Python-list mailing list