2

In python 3.6.2 I am trying to import a module with importlib. I am able to import the module directly as:

from scripts import config_A_2

but when trying with importlib as follows

module = importlib.import_module('config_A_2', 'scripts')

I get an error

 File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked    
ModuleNotFoundError: No module named 'config_A_2'

Maybe I am using importlib in a wrong way?

Info: Its a Mac...

1 Answer 1

1

You can use a relative import:

importlib.import_module('.config_A_2', 'scripts')

Or an absolute one:

importlib.import_module('scripts.config_A_2', 'scripts')
Sign up to request clarification or add additional context in comments.

2 Comments

For the first suggestion I get an error SystemError: Parent module 'scripts' not loaded, cannot perform relative import .
The second suggestion seems to work. Seems awkwardly complicated for python!! Maybe I create a wrapper for simple use ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.