简体   繁体   中英

Import multiple subdirectories/modules as if they are just one module

so I have a module/directory called A and it has init .py file and in it, it has another module/directory called B which have its init .py and a file called function.py which has a function called dummy()

here is the structure of directories

A
|-- __init__.py
|
|-- B
    |
    |-- __init__.py
    |-- function.py

so what I want is to be on the same directory that contains directory A and do that

from A import *
dummy()

what I have done is do that in B/ init .py

from dummy import *

and that in A/ init .py

import B

and I can do that

from A.B import *

I want to write A instead of AB

I changed your import code a bit and it seems to work now like you wanted. So in the B directory's init.py it has:

# __init__.py in B
from .function import *

In the A directory's init.py:

# __init__.py in A
from .B import *

Now, when I run Python shell in the directory that contains A and and use from A import * , it calls dummy() with no problem.

However, there are discussions on using wildcard imports in Python. Check this post for example: Should wildcard import be avoided?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM