Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to import a group of modules?

Tags:

python

import

In C/C++, if we have the following header a.h

#include "b.h"
#include "c.h"
#include "d.h"

Then, if we include a.h, b.h, c.h, and d.h are also automatically included. So, it is easy to include multiple related headers simultaneously.

However, in Python it seems that the story is different. Suppose that we have a Python module named a.py as shown:

import b
import c
import d

In this case, even if I import a, b, c, and d are not automatically imported.

In short, I want to find a way to import a group of modules easily. Are there any ways for me to do this?

like image 742
chanwcom Avatar asked Dec 06 '25 07:12

chanwcom


2 Answers

You can use an alternate method:

from a import *
like image 142
hjpotter92 Avatar answered Dec 08 '25 20:12

hjpotter92


When you use #include in C/C++, it is not part of the compiler. It is part of the preprocessor. If my main.cpp has an include, the preprocessor copy all code of the included file. In Python, it is differently because, the preprocessor does not exist, and the interpreter imports the specific module only for the file that has imported.

If you want to import several modules, you must use from a import *.

like image 20
sigifredo Avatar answered Dec 08 '25 20:12

sigifredo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!