How to structure packages

Westley Martínez anikom15 at gmail.com
Wed Sep 7 17:35:25 EDT 2011


First of all MyClass.py should be renamed to myclass.py.  Module names
should be lowercase.  Secondly, put this in __init__.py:

    from .myclass import MyClass

and there you go.

On Wed, Sep 07, 2011 at 08:56:32AM -0700, bclark76 wrote:
> I'm learning python, and was playing with structuring packages.
> 
> Basically I want to have a package called mypackage that defines a
> number of classes and functions.
> 
> 
> so I create:
> 
> mypackage
>     __init__.py
>     myfunc.py
>     MyClass.py
> 
> 
> my __init__.py is blank.
> 
> my MyClass.py looks like:
> 
> import blah
> 
> class MyClass(blahblah):
>     blah
>     blah
>     blah
> 
> 
> then I have a run.py that looks like
> 
> from mypackage import MyClass
> 
> 
> x = MyClass()
> 
> 
> This doesn't work because MyClass is mypackage.MyClass.MyClass.
> There's this MyClass module 'in the way'.
> 
> 
> I'm trying to follow the rule that every file defines only one class.
> I could define MyClass in __init__.py, but then what if I wanted to
> define more classes in the mypackage package? My one class per file
> rule goes out the window.
> 
> Is this rule wrongheaded, or is there another way to do this?
> 
> 
> Thanks.
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list