Call a function from another file?

前端 未结 17 2605
梦谈多话
梦谈多话 2020-11-22 08:06

Set_up: I have a .py file for each function I need to use in a program.

In this program, I need to call the function from the external files.

I\'ve tried:

17条回答
  •  独厮守ぢ
    2020-11-22 08:46

    Any of the above solutions didn't work for me. I got ModuleNotFoundError: No module named whtever error. So my solution was importing like below

    from . import filename # without .py  
    

    inside my first file I have defined function fun like below

    # file name is firstFile.py
    def fun():
      print('this is fun')
    

    inside the second file lets say I want to call the function fun

    from . import firstFile
    
    def secondFunc():
       firstFile.fun() # calling `fun` from the first file
    
    secondFunc() # calling the function `secondFunc` 
    

提交回复
热议问题