Mailing List Archive

Transistion from module to package and __init__.py
Hello,

I have a design question that for me primarily occurs when I have a module
that needs to transition to a package. Lets assume I have module that
services a database. Initially the module just has some functions to pull
data out of the database format them etc. As the project grows I add tables
some of the tables have static data or initialization data that are stored
in CSVs. I build functions that initialize the database a schema file is
now used. Now my module is still the only Python code file I have but I
have these other data files the module needs, time to make a package. So
naturally I make a package folder called database, put and empty
__init__.py, in goes my database module and all resource files. Here is
where I my primary design question comes in. As organized now as described
to import and use the package I need

from database import database

or I have to put in the init file

from database import *

Either of these still leaves a database.database namespace laying about and
to me it just seems untidy. So for a while now I have taken to converting
my module, in this example database.py, to the __init__.py of the package.
I remember reading years back that having code in the __init__.py was bad
practice but I can't remember reading for any specific reason. So then I
ask is there anything in fact wrong with this practice? I have probably a
dozen packages now that are used internal to my organization that follow
this pattern and I haven't encountered any issues. So I was hoping to get
feed back from a wider audience about if there are any issues with this
type of design that I just haven't encounter yet or find out what other
people do with this same problem.

Thanks

Chris
--
https://mail.python.org/mailman/listinfo/python-list
Re: Transistion from module to package and __init__.py [ In reply to ]
On 5/4/21 7:20 PM, Chris Nyland wrote:

> Here is
> where I my primary design question comes in. As organized now as described
> to import and use the package I need
>
> from database import database
>
> or I have to put in the init file
>
> from database import *
>
> Either of these still leaves a database.database namespace laying about and
> to me it just seems untidy. So for a while now I have taken to converting
> my module, in this example database.py, to the __init__.py of the package.

> So I was hoping to get
> feed back from a wider audience about if there are any issues with this
> type of design that I just haven't encounter yet or find out what other
> people do with this same problem.

This is the same method I use, and it works fine for smaller packages. I do have one package, dbf, that I am in the
process of converting to multiple files simply because there is so much (10k lines for me, YMMV).

--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list
Re: Transistion from module to package and __init__.py [ In reply to ]
Chris Nyland wrote at 2021-5-4 22:20 -0400:
> ...
>So for a while now I have taken to converting
>my module, in this example database.py, to the __init__.py of the package.
>I remember reading years back that having code in the __init__.py was bad
>practice but I can't remember reading for any specific reason.

If your package is not a so called "namespace package"
(this is a package components of which are stored in different places
and can be installed independent from one another), then
I see no reason not to put code into `__init__.py`.

When I transform a module into a package, I start by using the former
module as the new package's `__init__.py`. Later refactoring steps may
move things out into separate modules/subpackages.
--
https://mail.python.org/mailman/listinfo/python-list