0
코드에서 가져 오는 데 문제가없는 모듈을 동적으로 가져올 수 없으며 이유가 없습니다.Python3 : importlib와 importlib.import_module 사이에 다른 동작이 있습니까?
나는 다음과 같습니다
> ls lib
__init__.py main.py
초기화 파일이 비어 있습니다. 다음 작품 :
는> python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import lib.main
>>> lib.main.sayyay()
yay
다음은 작동하지 않습니다 나는 importlib 문서뿐만 아니라 여기에 SO에 대한 답변의 몇 가지 예를 들면, How to import a module in Python with importlib.import_module 및 Dynamically import a method in a file, from a string
을 읽었다
> python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import importlib
>>> importlib.import_module("lib.main")
<module 'lib.main' from '/some/path/lib/main.py'>
>>> lib.main.sayyay()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'lib' is not defined
하지만 내가 뭘 놓치고 있니?
오처럼이를 사용해야합니다. 그러나 이것이 어떻게 유용 할 수 있는지 봅니다. 고맙습니다. – BaCh