Tôi đang cố sử dụng importlib.import_module
trong trăn 2.7.2 và chạy vào lỗi lạ.Cách nhập mô-đun bằng Python với importlib.import_module
xem xét cấu trúc thư mục sau:
a | + - __init__.py - b | + - __init__.py - c.py
a/b/__init__.py
có đoạn mã sau: (. Trong mã thực "c"
có một cái tên)
import importlib mod = importlib.import_module("c")
Đang cố gắng để nhập a.b
mang lại sự lỗi sau:
>>> 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
Tôi đang thiếu gì?
Cảm ơn.
Nó rất hữu ích khi tên module là một biến. –