How to import all things defined the files in a module directory in __init__.py?

Ben Finney ben+python at benfinney.id.au
Thu Sep 22 00:14:28 EDT 2016


Peng Yu <pengyu.ut at gmail.com> writes:

> I want to import all the thing (or the ones available in the
> respective __all__) defined in each of the file by putting the
> following lines in __init__.py
>
> from file1 import *
> ....
> from filen import *
>
> However, I don't want to hardcode the file names in __init__.py. Is
> there an automatic way of doing it?

Why do you want to do that? It will defeat static analysis of the code,
which is one of the more important tools to make your code reliable.

It will also make the names in the code impossible to automatically
match against where they came from. Explicit is better than implicit;
you are proposing to make an unknown horde of names in the code implicit
and untraceable.

Why? What problem are you trying to solve that you believe needs this
approach?

-- 
 \           “Anything that we scientists can do to weaken the hold of |
  `\        religion should be done and may in the end be our greatest |
_o__)                  contribution to civilization.” —Steven Weinberg |
Ben Finney




More information about the Python-list mailing list