Breaking a game into multiple files [SOLVED]

Hey everyone, I’m a 3D artist with some coding experience and I’ve decided to make myself a game using Panda3D as the engine. Things are going good so far, I’m pretty happy with my progress. But I have run into a problem which I can’t find a solution to, probably because it’s such a simple thing.

I prefer to organize my OOP code into multiple files, usually a separate file for each class. Then when I use Notepad++ and have all the files open in tabs, it’s easy to switch around between the classes and keep all my code in arm’s reach.

So, here’s the problem. How do I include additional .py files in my primary file? Also, if I use class A in class B (for instance, if one of class A’s functions is passed an instance of class B) will I need to include class B’s file in class A’s file, even though they are both included in the ‘master’ file? Lastly, will I need to do all the panda module imports for each file, or is just putting them in the ‘master’ file enough?

Also, will structuring my code this way reduce the efficiency, making it run slower or anything like that? Most of my coding up to now has been in Processing and Action Script, just for you reference.

Thanks in advance.

the answer to your question can be found in most, if not every python tutorial/book.
it’s quite easy.
if you have main.py and yourclass.py
you can simply do (inside the main.py)

import yourclass

if panda is missing modules when you try to use them , it’ll immediately throw an error message telling you whats missing.

Thanks for the reply, Thomas. I did what you suggested using

import FILENAME

and I received an error message:

TypeError: ‘module’ object is not callable

So I ran a search on this error in the forums here and I think I figured out what is was doing. It was treating the contents of the file as an add-on or something similar, and not as a class. I fixed this problem with the suggestion I found here on the forums and changed my import line to

from FILENAME import CLASSNAME

That prevented the error from occurring. Thanks again for your help.

To be precise, the file is a module, and the class inside it is part of the module. So if you have a class called “enemy” and it’s stored in “enemy.py”, then there are two things both called “enemy” : the enemy module, and the enemy class which is part of the enemy module.

So there are several approaches:

from enemy import enemy

e = enemy()

or

import enemy

e=enemy.enemy()

or

from enemy import *
e=enemy()

the last one is useful if you have more than one class in a single file; it imports them all.

the second method can be a little more confusing, because of the repetition when you’re instancing the class, but it’s often considered better practice because that way you are very explicit about what class you’re using and you avoid accidentally colliding with another class named “enemy” that you might have imported from somewhere else.

Good to know, Benchang, thanks for the heads up. If the file and the class had different names, that would prevent the error message too, I take it?

As for the issue of calling different classes with the same name, it’s easier for me to keep things straight if I don’t use classes with the same name.