Please ignore secret bonuses. Secret tests do NOT award bonus. Max hw grade is 30+2 bonus efficiency

Do you need help?

Importing functions from a module

ruben.ciranni (4650 points)
8 14 31
in Programming in Python by (4.7k points)
recategorized by
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?
806 views

5 Answers

by (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)
8 14 31
by (4.7k 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
by (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)
8 14 31
by (4.7k points)
I’ll try doing like that, thank you!
DavideC. (1350 points)
4 6 9
by (1.4k 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)
8 14 31
by (4.7k 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 4 25
by (5.6k 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)
8 14 31
by (4.7k points)
I’m importing them using from … import …
gabrimat (5630 points)
2 4 25
by (5.6k 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)
8 14 31
by (4.7k points)
Yes, you were right! Prof. Spognardi explained how to do it at the end of today’s lesson
Luigi Pizza (6120 points)
14 20 65
by (6.1k points)
edited by
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 (820 points)
2 6 10
by (820 points)
What are the other commands that are executed?
Luigi Pizza (6120 points)
14 20 65
by (6.1k 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))