The file directory structure is as follows:
daily
-- 20210504
permutations.py
__init__.py
__init__.py
You can import the permutations
module by __import__
or importlib.import_module
.
The official documentation recommends using importlib.import_module
.
import(name, globals=None, locals=None, fromlist=(), level=0) -> module
Import a module. Because this function is meant for use by the Python interpreter and not for general use, it is better to
useimportlib.import_module()
to programmatically import a module.
What is the difference?
If implemented using __import__
.
For example:
res = __import__('daily.20210504.permutations')
The result of res is the daily module.
So, if you want to get the permutations
module, you need to provide the fromlist parameter, which is written as follows.
res = __import__('daily.20210504.permutations', fromlist=('daily.20210504'))
The result of res can be seen now as
That's the right result.
What if I use importlib.import_module
?
res = importlib.import_module('daily.20210504.permutations')
this allows you to get the permutations
module directly.
8puzzle.py
topuzzle8.py
and useimport puzzle8
. – Cowbind20190911_test_foo.py
,20190911_test_bar.py
,20190911_some_class_used_for_foo_and_bar
). – Polson