Importing a program file

I am trying to import a program into another program that l have written, but it is showing unresolved reference.
The file is called app.py and l wanted the function called def main(num), from it.

so in the new program l wrote

from app import main(), but it is not working

thanks

When you include the parenthesis in a line this makes it a function call, which is a SyntaxError in an import. To import a function from another file don’t include the parenthesis:

main.py

from lib import foo
foo()

lib.py

def foo():
  print("'ello!")

It is still showing me unresolved reference, after removing the parenthesis

There’s not really enough information to go on here. Is the error even a python exception or is it something else.

what further information can l give?

so the code which l wanted to import is not importing and there is no exception shown in the console

the original program is from here, that l want to import

That looks like it ran exercise.py successfully. Does your main function even return anything? There is a big different between print which outputs strings to console or similar and return which actually passes objects to the caller.

1 Like


so when l run the main function it gives me 10 but also a None, which is also appearing here

That is correct and what you would expect, see message above regarding print and return.

ok, what l understand is a function has to have a return statement, then you can use the print statement, after putting the return statement in the main function it works