issues with making a package. where do classes link?

Gabriel Genellina gagsl-py at yahoo.com.ar
Mon Dec 11 20:03:33 EST 2006


At Monday 11/12/2006 15:47, krishnakant Mane wrote:

>I am struggling a bit in making python packages.
>when I am doing a project I want to create all my python modules
>inside a single package.

A package is a directory with an __init__.py file in it. That's the 
only thing needed.

>I want to know when I make a package, what are the basic things I must
>generally do in the __init__.py file?

Most of the time, just an empty file. Or a lot of things. If you 
don't have nothing special to do, just let it empty.

Maybe, you have a lot of functions scattered along many modules 
inside your package, for your own convenience, but you prefer that 
users of the package refer to those functions as top-level functions 
(hiding the internal structure that you may consider an implementation detail).
By example, your package foo (in directory foo) contains:

__init__.py
bar.py which contains functions bar1, bar2
fido\
     fido.py which contains Fido class
     dido.py which contains Dido class

And you want your uses to refer to class Fido as foo.Fido (instead of 
foo.fido.fido.Fido), same thing for Dido, and foo.bar1 (instead of 
foo.bar.bar1)
So in __init__.py you populate the package's namespace as:

from bar import bar1, bar2
from fido.fido import Fido
from fido.dido import Dido

>and how do I link all the modules in my project with each other?

Inside the same directory, just by name: inside dido.py you could 
import class Fido as "from fido import Fido"
If you are using Python 2.5, you can use relative imports, see 
http://docs.python.org/whatsnew/pep-328.html

>like when I make a compilation unit in java under a package, the first
>like of that file will read "package mypackage".

Not needed. Python is aware of the directory containing the package, 
that directory becomes the package's name.

>I did not find any such thing in python that can say that this xyz
>file is in the abc package.

Look at the location of the file. From code, look at it's __file__ attribute.

>if I want to use one module into another in my same package I can't
>figure how python figures it out?

See above.

>I want every thing in a single package.
>so that's my confusion.

I hope it's clearer now. Read the Python tutorial about packages, and 
the PEP328 above.


-- 
Gabriel Genellina
Softlab SRL 

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis! 
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar



More information about the Python-list mailing list