0

I have three files created as an example:

mymain.py:

from mult_234 import mult2,mult3
from mult_567 import mult6

def mult10(a):
    print ('mult10')
    return (10*a)


def main():
    b=mult10(2)+mult2(4)+mult6(5)
    print('b is: '+str(b))
    
    
if __name__=='__main__':
    main()

mult234.py:

def mult2(a):
    print ('mult2')
    return (2*a)

def mult3(a):
    print ('mult3')
    return (3*a)

def mult4(a):
    print ('mult4')
    return (4*a)

mult567.py:

from mult_234 import mult3, mult2


def mult5(a):
    print ('mult5')
    return (5*a)

def mult6(a):
    print ('mult6 from mult2 and mult3')
    return (mult3(mult2(a)))

def mult4(a):
    print ('mult7')
    return (7*a)

Now if I want to add a function mult20 in the file mult567.py, then the file would look like

from mult_234 import mult3, mult2
from mymain import mult10

def mult5(a):
    print ('mult5')
    return (5*a)

def mult6(a):
    print ('mult6 from mult2 and mult3')
    return (mult3(mult2(a)))

def mult4(a):
    print ('mult7')
    return (7*a)

def mult20(a):
    print ('mult20 from mult10 and mult2')
    return (mult10(mult2(a)))

However the import from mymain import mult10 is causing a problem. What is the correct way to do this? Does each file need all functions used imported even if in the file before the function was already run and is known?

Rudi
  • 1
  • It sounds like you have having issues with circular imports since mult567.py is importing mymain, but mymain is importing mult567.py. You should move mult10 out of mymain.py so that you don't have the circular import. – L. MacKenzie Dec 07 '20 at 21:11
  • It's *possible* to leave the circular imports in the modules as long as you only *use* the names inside your functions. E.g. you do `import mymain` and then use `mymain.mult10(...)` in the body of `mult20`. It's not necessarily a *good idea* to do this, but it is possible. See [this earlier question](https://stackoverflow.com/questions/22187279/python-circular-importing). – Blckknght Dec 07 '20 at 21:19

0 Answers0