add new modules?

Kent Johnson kent37 at tds.net
Fri Jun 17 14:47:01 EDT 2005


Dennis Clark wrote:
> This is a total newb question, you have been warned...
> 
> I've been all over the www.python.org site and googled, but I've not
> found just how to add new modules.  I've tried setting PYTHONPATH,
> I've tried putting the new module directories into the site-packages
> directory, I've tried creating the .pth files, I've even done all 
> three of these things at the same time and still my python script
> refuses to import.  What is the cannonical way to add new modules
> to python?  I am running on OS X 10.4 (Macintosh obviously) on basically
> freeBSD, os I'm doing UNIX type stuff at the console.

If you have a single file that you want to make available, put it directly in site-packages. For example put mymodule.py in site-packages, then in code you can say
from mymodule import MyClass

If you have a directory of files that you consider related, that is a package. Put the directory in site-packages, add a file named __init__.py to the directory, and import qualified with the directory name. For example if you have
site-packages/
  mystuff/
    __init__.py
    mymodule.py

then in code you say
from mystuff.mymodule import MyClass

where in both cases MyClass is defined in mymodule.py.

Kent



More information about the Python-list mailing list