Importing functions from a module

ruben.ciranni (4650 points)
7 13 31
asked Oct 7, 2021 in Programming in Python by ruben.ciranni (4,650 points)
recategorized Nov 1, 2021 by andrea.sterbini
Hi,

I'm trying to import the functions circle_lenght and circle_area from the module circle.py in another script named test.py saved in the same directory as circle.py. Does anyone know why, even if in test.py I write to import only those two functions, the rest of the program circle.py is also executed?
262 views

5 Answers

answered Oct 7, 2021 by aleX (630 points)
Maybe in your circle.py there are some instructions that there are not  in a function. So when you import circle.py they Will Be executed.
ruben.ciranni (4650 points)
7 13 31
commented Oct 7, 2021 by ruben.ciranni (4,650 points)
yes, but I specified to import only those two functions so, intuitively, I would expect that only the functions are imported, and not the whole module
commented Oct 7, 2021 by aleX (630 points)
It Is python. If you want to maintain your code you can put It inside a function like a main function
ruben.ciranni (4650 points)
7 13 31
commented Oct 7, 2021 by ruben.ciranni (4,650 points)
I’ll try doing like that, thank you!
DavideC. (1350 points)
3 6 9
answered Oct 7, 2021 by DavideC. (1,350 points)
Hi,
To import some functions from another script you need to write "from (name of the script) import (name of the function)". In this case it would be "From circle import circle_lenght, circle_area"
ruben.ciranni (4650 points)
7 13 31
commented Oct 7, 2021 by ruben.ciranni (4,650 points)
yes, that is what I've done. I'm asking why in doing this also other scripts that are in the module circle.py get executed and not only the two functions that I've listed after "import"
gabrimat (5630 points)
2 2 25
answered Oct 7, 2021 by gabrimat (5,630 points)
How are you importing them? Probably I am wrong, but circle.py maybe is running as the main program. If you manage to give more information i will be glad to help.
ruben.ciranni (4650 points)
7 13 31
commented Oct 7, 2021 by ruben.ciranni (4,650 points)
I’m importing them using from … import …
gabrimat (5630 points)
2 2 25
commented Oct 8, 2021 by gabrimat (5,630 points)
I think I have the solution. When using import in python, its not just a declaration, it actually runs the module. So if you have something outside the functions imported, that code will run too. You should add another condition, that actually checks if the file is the main one. I cant give you the code but you are gonna easily find it on the resources we have. Let me know.
ruben.ciranni (4650 points)
7 13 31
commented Oct 8, 2021 by ruben.ciranni (4,650 points)
Yes, you were right! Prof. Spognardi explained how to do it at the end of today’s lesson
Luigi Pizza (6120 points)
11 20 65
answered Oct 7, 2021 by Luigi Pizza (6,120 points)
edited Oct 7, 2021 by Luigi Pizza
Hi, I've been doing what you have suggested:

from circle import circle_length

and by commenting out the rest of the script(in circle.py) there is no problem.

Now, to understand why that may be the case. As we have learnt from prof. Mancini's lessons, python is a compiler language, meaning that it reads and executes only a line at a time. Now, putting the functions on top of the script doesn't make a difference: all circle.py is still computed before the program calculates anything from our test.py. This means that python doesn't read through the script until it finds circle_length (as you've said in another comment, the intuitive thing to do), but it executes the program and saves the function somewhere(to demonstrate this: comment away every line in circle.py apart from the function circle_length and put "print(2)" anywhere in the script. Python will output the 2 before anything else, even if there is an error in test.py). Now, I think this is just a characteristic of "from ... import ..." and specifically of "from" and that there must be another instruction that does the intuitive thing. I'm browsing the web to find an answer. As soon as i find it, I'll modify this comment.
Lorenzo_Pellegrino (770 points)
2 4 10
answered Oct 9, 2021 by Lorenzo_Pellegrino (770 points)
What are the other commands that are executed?
Luigi Pizza (6120 points)
11 20 65
commented Oct 10, 2021 by Luigi Pizza (6,120 points)
everything that is in the imported file is executed (so if you have a print in there, its argument will be printed), apart from anything inside if __name__ == '__main__': (what is in here is executed if and only if the file (where the if statement is) is invoked directly (i. e. if you execute that file))