Help with importing a module.

Ok im working with two different python files one is main.py and the other is mainMenu.py
they are both in the same folder and i put import main at the top of mainMenu and import mainMenu at the top main.py. So i have an area class in main . In mainMenu i made an object out of that class called mainMenu.

so when i did this code:

currentArea = mainMenu.mainMenu

it should have worked but i got this error running main.py:

Traceback (most recent call last):
  File "/home/xzachtmx/Eclipse-Pydev/projects/Starbound3D/src/main.py", line 22, in <module>
    import mainMenu
  File "/home/xzachtmx/Eclipse-Pydev/projects/Starbound3D/src/mainMenu.py", line 13, in <module>
    import main
  File "/home/xzachtmx/Eclipse-Pydev/projects/Starbound3D/src/main.py", line 597, in <module>
    currentArea = mainMenu.mainMenu
AttributeError: 'module' object has no attribute 'mainMenu'

So u are instancing the area class from main.py in mainmenu.py??

If u cud post code it would be easier to help u…

But make sure u are importing area from main into mainmenu

from main import area

Then assign it

mainMenu = area()

Then in main import it like this

from mainMenu import *

Then u shud be able to reference any variable in area instance mainMenu like this

mainMenu.whatevervariableorfunction

I apologize if u already understood all this but if that’s the case post some code so we can get a better look at the problem

Yeah, it sounds like the import is giving you a module definition.
If you want to instantiate a class defined in another file, I find it easiest to use

 from [file] import [class]

to access stuff.
You may also need to double-check capitalization. Panda gets picky with that.

Finally, I’m not sure if this is what you’re doing, but I would advise away from trying to define instances in external files and get at them via import. It makes for ugly code, and really there’s every way to avoid doing so if you have a solid class hierarchy.

Ok im not getting the same problem when i put this in main

from mainMenu import * 

and

from main import area

but now im getting this error:

Traceback (most recent call last):
  File "/home/xzachtmx/Eclipse-Pydev/projects/Starbound3D/src/main.py", line 22, in <module>
    from mainMenu import *
  File "/home/xzachtmx/Eclipse-Pydev/projects/Starbound3D/src/mainMenu.py", line 13, in <module>
    from main import area
  File "/home/xzachtmx/Eclipse-Pydev/projects/Starbound3D/src/main.py", line 597, in <module>
    currentArea = mainMenu
NameError: name 'mainMenu' is not defined

:confused:
any ideas?

[size=150]EDIT: source code can be found at www.kartoongames.tk in the games section. I hope its easy enough to read [/size]

I looked at main.py and mainMeny.py

I could not find the line

currentArea = mainMenu

anywhere in main.py, which is what the traceback seems to be suggesting. Is the code that is there the most current?

Um sorry … i think the line actually says currentArea = sol … that was just to get the game to work… replace it with currentArea = mainMenu (in the main.py) and youll see the error

maybe try instancing the Area to MainMenu inside main.py

OK, I will take a stab at it, but this may get into some areas of Python that I am not 100% on top of.

My first question would be in regard to the circular references of importing area from main, and then importing mainMenu back into main. Dunno how that is going to work out…

Next, when you define:

mainMenu = area()

This is where your init function should be running. I would try using:

mainMenu = area(“menu”)

since self is being called. I am surprised that you are not getting errors about passing the right number of arguments when it is called.

** Reading back before hitting submit, I think the reason for this is now clear: That line of code is never being executed, because everything is crashing and shutting down before the mainMenu definition is being executed. See below**

I notice that the traceback is also throwing complaints about the import lines. I think this may be because of the circular importing I mentioned at first. You may need to either put area into mainMenu so that it can get at it without importing main back into mainMenu. It looks like it is throwing the error while it is doing the imports, because the currentarea=mainMenu is outside of any function definitions, it is attempting to execute at the line where main is imported into mainMenu. unfortunately, mainMenu (the object) has not yet been defined, because it is still only at the line where the import happens.

the more I look at it, the more I think that is the problem. You are going to have to rearrange your definitions so that mainMenu can do whatever it has to without importing main (or any part of main) back in. In general, circular references like that are bad. I suspect that this is one of those situations.

Does that make sense? I am having trouble explaining it, but basically, because there is code in main.py that is not inside a function definition, it is being executed whenever main.py is imported. Because you are importing main into mainMenu, it is trying to run that line of code in main before mainMenu has been defined in mainMenu.py.

Circular reference = bad. That is probably the best summary I can come up with… :^)

ok i just put it back into main… it worked lol thanks