import error?

T

Tom

Hi folks,

I have a really strange problem. I have a module and at the beginning I
include some others by:
from xxx import *
works perfectly except for one import. I always get an error saying:
NameError: global name 'yyy' is not defined (yyy is a function in xxx)

I don't understand that. I checked everything and I have no typos in my
code.
If I copy the import line from the beginning of my module and paste it
right before I refere to it in the function (which is of course
extreemly bad programming, but I just did that to find the error) it
works. I don't get it. Shouldn't it work if I import it at the beginning
of the code? It works for all the other modules I import!! I have no
typos and all the other imports work. Also the module itself works, I
checked that, but I get this name error and I don't understand that. :-(

Thanks for your help.
Regards, Tom
 
P

Peter Hansen

Tom said:
I have a really strange problem. I have a module and at the beginning I
include some others by:
from xxx import *
works perfectly except for one import. I always get an error saying:
NameError: global name 'yyy' is not defined (yyy is a function in xxx)

Is there a complete trackback you could post? Then nobody would
have to guess what your problem is.
I don't understand that. I checked everything and I have no typos in my
code.

Obviously something is trying to access "yyy" before it actually
exists, so you must be missing something. Do you have a circular
reference in xxx? Are you aware that importing a module for the
first time actually causes any statements at module level to be
executed (which when the "def" and "class" statements are executed,
for example)?

-Peter
 
T

Tom

Peter said:
Is there a complete trackback you could post? Then nobody would
have to guess what your problem is.
Traceback (most recent call last):
File "<string>", line 1, in ?
File "C:\Test\TestAnaMod.py", line 103, in ?
mul.multi(ActualState, Data, d, BoundModels, Automat, LogName)
File "C:\Test\multi_C.py", line 102, in multi
ReIni = initialize_C()
NameError: global name 'initialize_C' is not defined

At the very beginning of the module I include the other module which has
the class initialize_C. This is not the only time I refere to another
module and its class and functions. But it is the only time I get an
error. But I have no typos in the code. Strangewise I can call the
module with the class initialize_C from another module without causing
any problems!??! The weird thing is, that I have this at the beginning
of my module "from IniMod import *" (this is the module that has the
class initialize_C) and it doesn't work. But if I cut this line and
paste it into line 101, it works perfectly. Why is that? If I include it
at the very beginning of my module, shouldn't it be valid for the entire
module? If I import the module in line 101 I get this little warning
saying that I should import this at the beginning of the module, but it
works at least. :)

I hope this helps to clear things up and you guys can help me. I am
quite confused right now.
Thanks for your help, Tom
 
P

Peter Hansen

Tom said:
Traceback (most recent call last):
File "<string>", line 1, in ?
File "C:\Test\TestAnaMod.py", line 103, in ?
mul.multi(ActualState, Data, d, BoundModels, Automat, LogName)
File "C:\Test\multi_C.py", line 102, in multi
ReIni = initialize_C()
NameError: global name 'initialize_C' is not defined

At the very beginning of the module I include the other module which has
the class initialize_C. This is not the only time I refere to another
module and its class and functions. But it is the only time I get an
error. But I have no typos in the code. Strangewise I can call the
module with the class initialize_C from another module without causing
any problems!??! The weird thing is, that I have this at the beginning
of my module "from IniMod import *" (this is the module that has the
class initialize_C) and it doesn't work. But if I cut this line and
paste it into line 101, it works perfectly. Why is that? If I include it
at the very beginning of my module, shouldn't it be valid for the entire
module? If I import the module in line 101 I get this little warning
saying that I should import this at the beginning of the module, but it
works at least. :)

I hope this helps to clear things up and you guys can help me. I am
quite confused right now.

We're now as confused as you are, but only because you aren't including
enough clear details to make the answer obvious yet...

You say "at the beginning of the module"... _what_ module? The above
example seems to be one that you got by typing things at the interactive
interpreter. Otherwise I would expect to see file names and not "?"
and <string> in the traceback.

You talk about the "other module" (which one?) with initialize_C, but the
code above has code that is trying to call initialize_C() without
prefixing the name with the name of the module where initialize_C is
defined. Presumably therefore it is supposed to be defined in IniMod,
which you claim to have imported first in multi_C.py, but that simply
cannot be or you would not get that traceback.

Is it possible that you did the "from IniMod import *" in the file
TestAnaMod.py, but not in the file multi_C.py? In that case the
problem is simply that you didn't actually tell multi_C.py where
to find the initialize_C() class... you can't expect one import
statement to apply to all modules automatically like that.

Without a better picture of the layout of your functions and modules,
I can't help more.

By the way, don't get caught up in the "works on line 101" sort of
thing. The answer will be very simple once you find it, not some
strange bug relating to having things above or below something
else but only on Tuesdays. :)

-Peter
 
T

Tom

Hi Peter,

thanks for trying to help me. I can see that it can be confusing to read
my posts, but it is so hard to explain a problem if you do not even know
the source of the problem?! :-( But I appreciate your patience.

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. My
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. I did that because I didn't
want to overload the main program. After every step (which usually means
after calling a module) I have an output in the shell window. When I
said that I include stuff at the beginning I meant at the total
beginning of my main programm and at the total beginning of each module
that I use (before defining the class and function). Of course I only
include what I also need. I use this one module that later causes my
problems in the main program for the first time. It works fine there.
Than I can start my business which means that I call all the different
modules depending on the action that is neccessary. Sometimes it is
necessary to use this specific module (that I already used before) in
one of the other modules that I just called from my main program. Is
that maybe a problem, that I can't include the module in the main
program and in a module called by the main program? I import everything
that is neccessary it in both!

I also tried this: I made an exact copy of my module and just renamed
it. Using the original module in the main program and the renamed module
in the other module that I call from my main program. That worked fine.
Maybe because they were different modules and python doesn't like it if
I call the same module? But what still irritates me is the fact that
everything also works fine if I write the "from bla import *" line
directly before I refere to the function in that module. I don't like
this but I meantion it, because I think it is strange that it works then.

Well, I could use this copied and renamed module, but because it
consists exactly the same code and just changed the filename (all the
class and function names are still the same), I don't really see the
necessity. Maybe it is one of the Thursday only problems! :) But I
would love to find the find out what's wrong :)
I don't know if this helps or just confuses more. :) I hope not. :)

Thank you very much for all your help anyway.

Regards, Tom
 
P

Peter Hansen

Tom said:
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
 
T

Tom

Hi Peter,

thanks a lot for your explanations. I'll try what you suggested, but
that looks like a lot of work,so I am not sure when I find time for
that! :)
But I will defintely consider your concerns about my programming for my
next piece of code right from the beginning. :) I had no idea that
"from xxx import *"can cause so many problems and is kind of bad style.

Thanks again for your time. I appreciate that a lot.

Regards, Tom
 
P

Peter Hansen

Tom said:
thanks a lot for your explanations. I'll try what you suggested, but
that looks like a lot of work,so I am not sure when I find time for
that! :)
But I will defintely consider your concerns about my programming for my
next piece of code right from the beginning. :) I had no idea that
"from xxx import *"can cause so many problems and is kind of bad style.


See this link for more:

http://www.python.org/doc/faq/progr...e-best-practices-for-using-import-in-a-module

You can also check the comp.lang.python for many discussions of this
(bad) practice and the potential bad outcomes:

http://groups.google.ca/groups?q="import+*"&meta=group=comp.lang.python.*

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,269
Messages
2,571,338
Members
48,029
Latest member
Anchorman2022

Latest Threads

Top