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

前端 未结 7 1298
别跟我提以往
别跟我提以往 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:31

    Be sure to append the modules directory to your python path.

    Your path (the list of directories Python goes through to search for modules and files) is stored in the path attribute of the sys module. Since the path is a list you can use the append method to add new directories to the path.

    For instance, to add the directory /home/me/mypy to the path:

    import sys
    sys.path.append("/home/me/mypy") 
    

提交回复
热议问题