How can I import a package using __import__() when the package name is only known at runtime?

前端 未结 7 1242
别跟我提以往
别跟我提以往 2020-12-14 21:08

I have a messages folder(package) with __init__.py file and another module messages_en.py inside it. In __init__.py if I import

相关标签:
7条回答
  • 2020-12-14 21:34

    If it is a path problem, you should use the level argument (from docs):

    __import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module
    
    Level is used to determine whether to perform
    absolute or relative imports.  -1 is the original strategy of attempting
    both absolute and relative imports, 0 is absolute, a positive number
    is the number of parent directories to search relative to the current module.
    
    0 讨论(0)
提交回复
热议问题