Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group mutliple modules into a single namespace?

I have a python3.5 project which I decided to make a one class per module. I decided to do so because i found out my files were very long and I it was hard to me to understand whats going on.

After I made the change I feel like I am repeating myself in each import file:

from school.student import Student
from school.classroom import ClassRoom
from school.teacher import Teacher

Is there any way pass that repeat? I would like that my imports will be more like:

from school import Student, ClassRoom, Teacher
like image 471
roy999 Avatar asked Apr 11 '16 18:04

roy999


People also ask

Can we import multiple modules with single import statement?

Import multiple modulesYou can write multiple modules separated by commas after the import statement, but this is not recommended in PEP8. Imports should usually be on separate lines.

What is a module namespace?

In Python-speak, modules are a namespace—a place where names are created. And names that live in a module are called its attributes. Technically, modules correspond to files, and Python creates a module object to contain all the names defined in the file; but in simple terms, modules are just namespaces.

What is __ init __ py?

The __init__.py file makes Python treat directories containing it as modules. Furthermore, this is the first file to be loaded in a module, so you can use it to execute code that you want to run each time a module is loaded, or specify the submodules to be exported.


2 Answers

Use an __init__ module to accomplish this. The contents of an __init__ module are evaluated when the parent module/package is imported. Like this:

# __init__.py
from school.student import Student
from school.classroom import ClassRoom
from school.teacher import Teacher

This brings Student, ClassRoom and Teacher into the school namespace. Now you can import using the school module as you like.

Caution: it is really easy to pollute your namespace this way. It's probably better to import explicitly in each module the way you started, or to use the approach in this answer and use your classes employing explicit module references (e.g., school.Teacher() instead of Teacher()).

If you feel school.Teacher() is too long, you can shorten things up a bit this way:

import school as sch
teacher = sch.Teacher()

etc etc.

However, on the other hand, if those classes are objects that should be available at the package level, importing them into the package namespace is probably the correct thing to do. Only you can make that determination.

like image 189
Rick supports Monica Avatar answered Sep 23 '22 22:09

Rick supports Monica


I supposed school is a package, if not you need to create a package and put all your modules into the package then create a __init__.py file where you basically glue everything together. You need to use a relative import in your __init__.py file.

You package must now look like this:

school/
__init__.py
student.py
classroom.py
teacher.py

Then you __init__.py must look like this:

from .student import Students
from .classroom import ClassRoom
from .teacher import Teacher

Next only call your class using package name like this:

import school
t = school.Teacher()
like image 39
styvane Avatar answered Sep 25 '22 22:09

styvane