Python Forum
Importing a function from another file runs the old lines also - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Importing a function from another file runs the old lines also (/thread-34200.html)



Importing a function from another file runs the old lines also - dedesssse - Jul-06-2021

I'm trying to import a function, an "def" function, to a new file, but when i do, the old line ABOVE are also read. Why!?


RE: Importing a function from another file runs the old lines also - deanhystad - Jul-06-2021

When asking questions about your code it is a good idea to post the code.

When you import a file the first time it is evaluated to get all the variables, functions and classes. If you have code you don't want evaluated you need to hide it inside a function or behind a conditional. The code below has a print statement that prints when the code is evaluated. This will occur if the file is executed or the first time the file is imported. Additional prints are hidden inside functions or behind a condition that is only True if this file is being executed.
"""Interesting function"""
def my_func(arg):
    print("this code is protected until my_func is called")
    return arg + arg**2 + arg**3

print("this code is not protected")

if __name__ == '__main__':
    """This code is only executed when running this file from the command line"""
    print("call my_func")
    my_func(1)
When I run this file from the command line I see this:
Output:
this code is not protected call my_func this code is protected until my_func is called
When I import this this module I see this:
Output:
this code is not protected
Notice the code protected by the conditional is not executed.


RE: Importing a function from another file runs the old lines also - dedesssse - Jul-06-2021

This file is called "Testfile0

print("lowest number//")
print(min(4, 40404))
print(sqrt(25))

def calcus():
    print("Why are the files above read?")
]

This is another file
from Testfile0 import calcus


calcus()



RE: Importing a function from another file runs the old lines also - deanhystad - Jul-06-2021

Your Testfile0 should be written like this:
import math

"""expose these functions"""
def calcus():  # <- Want to see this function
    pass

if __name__ == '__main__':
    print("lowest number//")   # Only execute if run as "python Testfile0.py"
    print(min(4, 40404))
    print(math.sqrt(25))



RE: Importing a function from another file runs the old lines also - dedesssse - Jul-06-2021

#Testfile0

import math

print("lowest number//")
print(min(4, 40404))
print(sqrt(25))

"""expose these functions"""


def calcus():  # <- Want to see this function
    print("This is what i want only")
    pass

if __name__ == '__main__':
    print("lowest number//")  # Only execute if run as "python Testfile0.py"
    print(min(4, 40404))
    print(math.sqrt(25))
#Calculator
from Testfile0 import   calcus
calcus()



RE: Importing a function from another file runs the old lines also - jefsummers - Jul-06-2021

You're not listening.
When you import a function from a file, it starts reading at the beginning of the file and will execute the code. def protects code from being run (only runs when the function is called). Anything that is not in a function definition will be executed. And, actually that includes that last block of code but since the if condition is not met those lines are skipped.

So, on import, your first 3 print lines will execute. The function will be defined. The if statement tested and fails so the last 3 prints do not occur.


RE: Importing a function from another file runs the old lines also - deanhystad - Jul-06-2021

Look at comments to see what Python does when importing this file the first time.
import math    # Imports math.  evaluates math if this is the first import
 
print("lowest number//")  # evaluates print command which prints to stdout
print(min(4, 40404))  # evaluates min command which returns 4.  evaluates print command which prints to stdout
print(math.sqrt(25))  # evaluates sqrt command which returns 5.  evaluates print command which prints to stdout
 
"""expose these functions"""
 
 
def calcus():  # <- # Evaluates function declaration.  Adds function named "calculus" with no arguments to module namespace
    pass
 
if __name__ == '__main__':  # Evaluates if __name__ == '__main__'.  Is not True, so skip following INDENTED lines
    print("lowest number//")
    print(min(4, 40404))
    print(math.sqrt(25))
If you don't want those print statements to print anything, don't put them where they will be evaluated!