Crispy13
Crispy13

Reputation: 369

Omitting subdirectories when import custom modules python

crispy13/
    __init__.py
    core/
        __init__.py
        ecf.py

How can i load the ecf module in the following ways?

from crispy13 import ecf
OR
from crispy13.ecf import *

instead of
from crispy13.core.ecf import *

Upvotes: 0

Views: 38

Answers (2)

Vishal Singh
Vishal Singh

Reputation: 6234

like sahasrara62 said it can be done in the following manner

in crispy13/__init__.py import your module as

from .core import ecf

you can also make use of __all__ variable in your __init__.py file.

This is a very good way of importing modules/functions/classes.

Little bit insight to why we do imports in this way.
Let's suppose you are using a third-party library/package and there are some implementation changes in the module

  1. changes the place of the function/class that you are importing
  2. removed a module and put its contents into another module of the same package

then your import will break.

That's why all third party libraries/packages include all of their consumable functions/classes/modules in the root __init__.py file of their package.

Upvotes: 1

sahasrara62
sahasrara62

Reputation: 11238

in crispy13/__init__.py import the package as

from .core import ecf

Upvotes: 1

Related Questions