Issue
I'm trying to use importlib.import_module
in Python 2.7.2 and run into the strange error.
Consider the following dir structure:
a | + - __init__.py - b | + - __init__.py - c.py
a/b/__init__.py
has the following code:
import importlib mod = importlib.import_module("c")
(In real code "c"
has a name.)
Trying to import a.b
, yields the following error:
>>> import a.b Traceback (most recent call last): File "", line 1, in File "a/b/__init__.py", line 3, in mod = importlib.import_module("c") File "/opt/Python-2.7.2/lib/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) ImportError: No module named c
What am I missing?
Thanks!
Solution
For relative imports you have to:
- a) use relative name
b) provide anchor explicitly
importlib.import_module('.c', 'a.b')
Of course, you could also just do absolute import instead:
importlib.import_module('a.b.c')
Answered By - Cat Plus Plus Answer Checked By - Senaida (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.