Python import functions from module twice with different internal imports

前端 未结 3 1671
难免孤独
难免孤独 2021-01-22 20:13

I have a module lib that needs numpy. So, for instance, let\'s say I have a hypothetical function that looks like

import numpy
def doS         


        
3条回答
  •  遇见更好的自我
    2021-01-22 20:35

    You don't need to do anything to achieve this.

    If you do

    from lib import doSomething
    from lib_with_autograd import doSomething as doSomethingAutograd
    

    each of those functions uses the numpy imported in their specific module. So doSomethingAutograd uses the one imported in lib_with_autograd and doSomething uses the one imported in lib

提交回复
热议问题