[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
mymodule.foo()

or
from mymodule import foo
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:
site-packages/
     mypackage/
         __init__.py  (required to have the folder recognized as  package, 
though the file can be empty)
         mymodule1.py
         mymodule2.py

Then you would say
import mypackage.mymodule1
mypackage.mymodule1.foo()

or
from mypackage import mymodule1
mymodule1.foo()

or
from mypackage.mymodule import foo
foo()

HTH
Kent

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
>
>
>
>nik wrote:
>
>>hi,
>>
>>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?
>>
>>nik
>>_______________________________________________
>>Tutor maillist  -  Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list