Call a function from another file?

前端 未结 17 2591
梦谈多话
梦谈多话 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:58

    Rename the module to something other than 'file'.

    Then also be sure when you are calling the function that:

    1)if you are importing the entire module, you reiterate the module name when calling it:

    import module
    module.function_name()
    

    or

    import pizza
    pizza.pizza_function()
    

    2)or if you are importing specific functions, functions with an alias, or all functions using *, you don't reiterate the module name:

    from pizza import pizza_function
    pizza_function()
    

    or

    from pizza import pizza_function as pf
    pf()
    

    or

    from pizza import *
    pizza_function()
    

提交回复
热议问题