Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Importing the classes from a python module directly

I have a python module I am writing that has the structure:

top_level_module/
    __init__.py
    sub_module1/
        __init__.py
        Class1.py
        Class2.py
    sub_module2/
        __init__.py
        Class3.py

The capitalizations there are correct. This structure was chosen by someone else but they are set on keeping it like that (I believe they are used to C++ or Java naming). I want to be able to import the classes easier than a bunch of consecutive
from top_level_module.sub_module1.Class1 import Class1 statements. Is there a way to do this that is reasonable?

I currently have a classes.py in a couple modules that just import all the classes so I can at least do:
from top_level_module.sub_module1.classes import Class1, Class2
but it just seems wrong. I would rather use init.py somehow, but I do not want to block the ability to import the modules names ClassX and only allow the import of the classes within them.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I understand that the question is similar to How to import class from a Python package directly but I think it is different enough.

>Solution :

in the top_level_module/sub_module1/__init__.py file, you can import the classes from Class1.py and Class2.py

from .Class1 import Class1
from .Class2 import Class2

and of course in the top_level_module/sub_module2/__init__.py file, you can do the same for Class3.py

from .Class3 import Class3

now, you can import the classes directly from the sub-modules without having to explicitly specify the file names and the __init__.py files act as a bridge between the top-level module and the classes within the sub-modulesand, something like this :

from top_level_module.sub_module1 import Class1, Class2
from top_level_module.sub_module2 import Class3

update:
when you import a package,python looks for an __init__.py file in the directory and if it finds one,it executes the code inside the __init__.py file, so it allows you to define what gets imported when you import the package!

in your example,if you want to import sub_module1 as a package and also import the classes within it,you can modify the __init__.py file in top_level_module/sub_module1

from .Class1 import Class1
from .Class2 import Class2

__all__ = ['Class1', 'Class2']

as you see __all__ variable is a list that specifies the names that should be imported when you use the from sub_module1 import * syntax and by default,python only imports names that do not start with an underscore,so by setting __all__ to include the class names,you can now import the classes directly from sub_module1 or import the package itself!!!

let me give you an example :

from top_level_module.sub_module1 import Class1, Class2
from top_level_module.sub_module1 import sub_module1

#imported classes
obj1 = Class1()
obj2 = Class2()

#imported package
obj3 = sub_module1.Class1()
obj4 = sub_module1.Class2()

good luck !

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading