简体   繁体   English

从python中的另一个文件调用函数

[英]Function calling from another file in python

I have a python file called function_bucket.py from which I need to import the modalprem() function into another file named Array1.py . 我有一个名为Python文件function_bucket.py从中我需要importmodalprem()函数到另一个名为文件Array1.py Below I have mentioned the codes of both the functions but while calling the function " No module named modalprem() "error is showing to me!. 下面我提到了这两个函数的代码,但是在调用函数“ No module named modalprem() ”时,向我显示错误! Please help me about this error. 请帮助我解决此错误。

Code for function_bucket.py function_bucket.py代码

def main():
    def modalprem():
        return 1
if __name__ == "__main__":
    main()

Code for Array1.py Array1.py代码

import numpy as np
from Function_Bucket import modalprem()
modalprem()
def main():
    EA=50
    PT = 15
    PPT = 5
    AP = 50000
    Mode = 12
    PM = np.arange(12*PT+1)
    PY = np.arange(12*PT+1)
    AGE = np.arange(12*PT+1)
    PREM = np.arange(12*PT+1)
    i=1
    Modal_Premium = modalprem()
    print(Modal_Premium)
    if (Mode == 1):
        Modal_Premium = AP
    elif (Mode==2):
        Modal_Premium = AP *0.5131
    elif (Mode==4):
        Modal_Premium = AP *0.2605
    elif (Mode==12):
        Modal_Premium = AP *0.0886

    while(i <180):
        PM[i] = i
        PY[i] = int( (i+11)/12)
        AGE[i] = EA + PY[i] -1
        if (PM[i]<=PPT*12 and ((PM[i]-1)%(12/Mode) == 0)):
            PREM[i] = Modal_Premium
        else:
            PREM[i] = 0
        i = i+1
    print(PM)
    print(PY)
    print(AGE)
    print(PREM)
if __name__ == "__main__":
    main()

Thanks 谢谢

Any Help Will Be Appreciated! 任何帮助将不胜感激!

You Also Add Parenthesis in Import Function I Think That's Why You Getting This Error 您还可以在导入函数中添加括号,我认为这就是您收到此错误的原因

Try This in your Array1.py Array1.py中尝试一下

import function_bucket

function_bucket.modalprem()

the function you define in function_bucket is main , not modal_prem . 您在function_bucket定义的function_bucketmain ,而不是modal_prem You need to define it outside of main 您需要在main之外定义它

function_bucket.main()

You Also Add Parenthesis in Import Function 您还可以在导入功能中添加括号

If It's Not Work It's Mean Your Are Not Sharing The right Python File Name 如果不起作用,则表示您未共享正确的Python文件名

Code for function_bucket.py function_bucket.py的代码

def main():
    def modalprem():
        return 1
    return modalprem

Code for Array1.py Array1.py的代码

import numpy as np
from function_bucket import main
modalprem=main()
def main():
    EA=50
    PT = 15
    PPT = 5
    AP = 50000
    Mode = 12
    PM = np.arange(12*PT+1)
    PY = np.arange(12*PT+1)
    AGE = np.arange(12*PT+1)
    PREM = np.arange(12*PT+1)
    i=1
    Modal_Premium = modalprem()
    print(Modal_Premium)
    if (Mode == 1):
        Modal_Premium = AP
    elif (Mode==2):
        Modal_Premium = AP *0.5131
    elif (Mode==4):
        Modal_Premium = AP *0.2605
    elif (Mode==12):
        Modal_Premium = AP *0.0886

    while(i <180):
        PM[i] = i
        PY[i] = int( (i+11)/12)
        AGE[i] = EA + PY[i] -1
        if (PM[i]<=PPT*12 and ((PM[i]-1)%(12/Mode) == 0)):
            PREM[i] = Modal_Premium
        else:
            PREM[i] = 0
        i = i+1
    print(PM)
    print(PY)
    print(AGE)
    print(PREM)
if __name__ == "__main__":
    main()

Hopefully this would work as expected! 希望这会按预期工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM