2

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?

chanwcom
  • 4,420
  • 8
  • 37
  • 49
  • 3
    If you `import a`, you do realise that `a.b.whatever` is available? In many ways this is better, as it's more explicit what names are now in scope. – jonrsharpe Sep 19 '15 at 17:52
  • maybe you can use `from a import *` if `a.py` contains only import statements? – zhangxaochen Sep 19 '15 at 17:53
  • 2
    I think python went the way of "Explicit is better than implicit" so that you always know where something came from. – syntonym Sep 19 '15 at 17:59

5 Answers5

3

You can use an alternate method:

from a import *
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • 2
    This is a very bad idea for [several reasons](http://stackoverflow.com/a/2386740/1599111). And just throwing this at someone new to Python without any further explanation is worthy of a downvote in my book. – Lukas Graf Sep 19 '15 at 19:04
3

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 *.

sigifredo
  • 152
  • 1
  • 8
2

There a few ways you can import in python

1) import a

What this will allow you to do is use anything from the module a but you'll need to use a.foo before calling it

2) import somethinglong as a

This will take a long module name and assign a to it to prevent somethinglong.foo, but have a.foo

3) from a import b

This will allow only b to be imported so you don't include the library as a whole

4) from a import *

This will import everything from module a and will allow you to use whatever is included without a prefix, i.e. instead of a.foo simply foo

Note #4 is what you're looking for.

Leb
  • 15,483
  • 10
  • 56
  • 75
1
# x.py
from y import *
print z.hello

# y.py
import z

# z.py
hello = 'there'
Robert E
  • 413
  • 4
  • 7
0

So it looks like the most safe and pythonic way is separated file like MyImports.py with

import b
import c
import d

and

from MyImports import *

in the main module?

I would propose variant of adding method that runs imports to your low-level utility module

# MyUtils.py
def importAbc():
    import b
    import c
    import d

but this wouldn't work...

Mikhail M
  • 929
  • 2
  • 10
  • 23
  • This is a bit gross, but I suppose you could do `a, b, c = myutils.importAbc()`, and within `importAbc` you could load each module using `importlib.import_module()` and combine them in a tuple as the return value. Using `from MyImports import *` and customizing the `__all__` within `MyImports` to include just modules a, b, & c seems less brittle. – Ryan Feeley Jul 10 '22 at 04:45