[Tutor] my own site-package

Kent Johnson kent_johnson at skillsoft.com
Wed Oct 6 16:43:55 CEST 2004

Putting your module in a folder creates a package.

If you have just one module mymodule.py, put it directly in site-packages. 
If mymodule.py contains a function foo(), you can say
import mymodule

from mymodule import foo

If you have more than one module that you want to group together you can 
put them in a folder and import them as a package. Your site-packages would 
look like this:
         __init__.py  (required to have the folder recognized as  package, 
though the file can be empty)

Then you would say
import mypackage.mymodule1

from mypackage import mymodule1

from mypackage.mymodule import foo


At 04:30 PM 10/6/2004 +0200, nik wrote:
>I seem to have got it working, but I'm not sure why.
>If I run the python interpreter in the same folder as my module, then doing;
>import mymodule
>works fine, and I'm able to call all the methods in mymodule.py
>If I create a folder called mymodule under site-packages, then the above 
>doesn't work *unless* I do;
>from mymodule import *
>I'm not seeing the distinction between the two situations - was naming the 
>folder the same as the python file a mistake perhaps?
>nik wrote:
>>I put my homemade python module into a subfolder of the site-packages 
>>folder. However, while I can import the module ok, I get the error 
>>'attribute not defined' when I try to access any of the functions or variables.
>>Is there more to it than just adding the .py files into a folder under 
>>site-packages? I have a __init__.py file with;
>>__all__ = ["mapping", "firebird", "variable"]
>>in it, which are names of the  python files in the folder I added.
>>Any ideas why I can't see the attributes? Also, do the .py files listed 
>>in that __all__ need to be in their own folders?
>>Tutor maillist  -  Tutor at python.org
>Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list