hi everyone! I have two files, main.py and sprite_class.py. sprite_class.py is in a separate folder called lib. here's a quick diagram:

------------------------------------
/folder engine/
main.py
/folder lib/
sprite_class.py
------------------------------------

...sprite_class.py contains a class called Sprite:

# sprite class

class Sprite():
     def __init__( self, start_x, start_y, image_path ):
          self.x = start_x
          self.y = start_y		
          self.starting_y = self.y
          self.image_path = image_path
          self.sprite = pygame.image.load( self.image_path )

since i'm kind of rusty when it comes to import statements, I created a small def. function to handle it for me. also at the bottom, I import my sprite_class.py file:

def CD( Dir_String ):
    sys.path.append( Dir_String )
    # changes the system's current dir. path to specified path

CD( "/lib" )

import sprite_class

Now, the only problem I have is trying to run the class in main.py. I know how to run def. statements, but how do you run classes?

Any hints?
Thank you in advanced!

Doesn't spriteObj = sprite_class.Sprite() work?

# changes the system's current dir. path to specified path

It doesn't change the path, it adds that path to the list that Python searches. You can print sys.path if you want to see what it is.
sys.path.append("/engine/lib/") ## or whatever it is
import sprite_class
and then
sprite_obj = sprite_class.Sprite()
to call a function in the class Sprite, use
sprite_obj.function_name(vars)
to access a variable
print sprite_obj.starting_y
Note that the function CD does not return anything so sprite_obj will be destroyed when you exit the function.

First off, to include a file in a different folder, you don't have to change your current working directory. A folder in python is called a package, and the file in that folder is called a module. To correctly create a package, you have to create a file called __init__.py inside that folder.

When you have created that file, all you have to do is

from lib.sprite_class import Sprite

mySpriteObject = Sprite()

If you have a programming background from java or a similair language, it's the usual practice to have each class in a separate file. In python you don't do that. You have similair classes in one module (i.e. one file) and you structure your modules in packages (i.e. folders). It's usually not a good idea to bundle each class in a separate module inside a folder (package) called lib. It's counter-intuivite. Think about which classes goes together, and bundle those in the same module.

Example from standard python library (version 3.0)
there's a package called html. That package has a module called client. The client-module has classes and functions that are related to the html-client.

There's a package called urllib. That package has a module called parse. The parse module has classes and functions that are related to parsing urls.

If you manage to structure your code like this, and try to keep each module separate, i.e. you don't have inter-dependencies in your module, your code will (probably) be easier to understand and easier to maintain.

thanks everyone!
I got it working just fine. all I needed was the __init__.py file and some code clean up!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.