Override namespace in Python

后端 未结 3 1426
猫巷女王i
猫巷女王i 2020-12-08 00:01

Say there is a folder, \'/home/user/temp/a40bd22344\'. The name is completely random and changes in every iteration. I need to be able to import this folder in Python using

3条回答
  •  無奈伤痛
    2020-12-08 00:02

    Here's one way to do it, without touching sys.path, using the imp module in Python:

    import imp
    
    f, filename, desc = imp.find_module('a40bd22344', ['/home/user/temp/'])
    project = imp.load_module('a40bd22344', f, filename, desc)
    
    project.some_func()
    

    Here is a link to some good documentation on the imp module:

    • imp — Access the import internals

提交回复
热议问题