[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Making A Module



    Thanks for all the suggestions. I thought this is the way it should be done but kept coming up with an error in the import statement. Kept saying it could not find it. So I had tried placing it in the main path where Python25 is but it did not work. So I set it in the site packages folder where all the others are and it did not work. SO I will have to check again and see what I am doing wrong. 

    I thought you should remove everything but the def statements and it is just not finding the file itself. So I will try again and report what I am doing. Maybe the error is something else besides the load of the file but I am sure the error was at that point.

        Bruce


Sent: Monday, October 29, 2007 12:49 PM
Subject: Re: [pygame] Making A Module


On Oct 29, 2007, at 11:31 AM, RR4CLB wrote:

>
> How do you make a module so you can place all your functions into  
> it? I want to be able to load the functions in without having a  
> long list.

A module is simply a .py file (that is for source files) that you do  
not execute directly that is somewhere on python path. The python  
path typically consists of:

- PYTHONPATH environment variable, if set
- The directory of the script you are executing
- Directories of installed third party packages, if any
- Standard library modules (could be a directory or zip archive)
- site-packages directory where 3rd party modules are often installed  
and site.py resides

Note you can inspect and modify the python path using sys.path, or by  
setting PYTHONPATH. though the latter should be avoided except as a  
temporary measure usually.

Typically if you have a long script that you want to pare down, you  
would create one or more .py files in the same directory as the  
script containing the functions you need. Then the script can import  
them to use the functions, plus they can import each other as needed.

This tutorial page has more info: http://docs.python.org/tut/node8.html

-Casey