954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

import classes from a seperate file

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!

besktrap
Junior Poster in Training
58 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
 

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

scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
 
# 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.

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

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.

vidaj
Junior Poster in Training
68 posts since Jul 2007
Reputation Points: 45
Solved Threads: 14
 

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

besktrap
Junior Poster in Training
58 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You