简体   繁体   中英

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 . Below I have mentioned the codes of both the functions but while calling the function " No module named modalprem() "error is showing to me!. Please help me about this error.

Code for function_bucket.py

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

Code for 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

import function_bucket

function_bucket.modalprem()

the function you define in function_bucket is main , not modal_prem . You need to define it outside of 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

Code for function_bucket.py

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

Code for 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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