scorpion35
scorpion35

Reputation: 1084

Python is printing everything form module when imported into another module

I have a 2 files.

  1. funcattrib.py
  2. test_import.py

funcattrib.py

import sys

def sum(a,b=5):
    "Adds two numbers"
    a = int(a)
    b = int(b)
    return a+b

sum.version = "1.0"
sum.author = "Prasad"
k = sum(1,2)
print(k)

print("Function attributes: - ")
print("Documentation string:",sum.__doc__)
print("Function name:",sum.__name__)
print("Default values:",sum.__defaults__)
print("Code object for the function is:",sum.__code__)
print("Dictionary of the function is:",sum.__dict__)

#writing the same information to a file

f = open('test.txt','w')
f.write(sum.__doc__)
f.close()
print("\n\nthe file is successfully written with the documentation string")

test_import.py

import sys
from funcattrib import sum

input("press <enter> to continue")

a = input("Enter a:")
b = input("Enter b:")
f = open('test.txt','a')
matter_tuple = "Entered numbers are",a,b
print(matter_tuple)
print("Type of matter:",type(matter_tuple))
matter_list = list(matter_tuple)
print(list(matter_list))
finalmatter = " ".join(matter_list)
print(finalmatter)
f.write(finalmatter)
f.close()
print("\n\nwriting done successfully from test_import.py")

I imported sum function from funcattrib.py. when I try to execute test_import.py, I am seeing the output of the whole funcattrib.py. I just wanted to use the sum function.

Please advise, what wrong I am doing, or is there another way of importing module without actually executing it?

Upvotes: 0

Views: 242

Answers (2)

BrenBarn
BrenBarn

Reputation: 251398

Python executes top to bottom, always. Function definitions are executable code like anything else. When you import a module, all the code at the top level of that module is run. Python has to run it all, because the functions are part of the code.

The solution is to protect code that you don't want run on import under an if __name__=="__main__" block:

if __name__ == "__main__":
     print("Some info")

Upvotes: 2

Martijn Pieters
Martijn Pieters

Reputation: 1122162

All statements in the 'top level' of a module are executed when imported.

You you don't want that to happen, you need to distinguish between the module being used as a script and a module. Use the following test for that:

if __name__ == '__main__':
    # put code here to be run when this file is executed as a script

Applying that to your module:

import sys

def sum(a,b=5):
    "Adds two numbers"
    a = int(a)
    b = int(b)
    return a+b

sum.version = "1.0"
sum.author = "Prasad"

if __name__ == '__main__':
    k = sum(1,2)
    print(k)

    print("Function attributes: - ")
    print("Documentation string:",sum.__doc__)
    print("Function name:",sum.__name__)
    print("Default values:",sum.__defaults__)
    print("Code object for the function is:",sum.__code__)
    print("Dictionary of the function is:",sum.__dict__)

    #writing the same information to a file

    f = open('test.txt','w')
    f.write(sum.__doc__)
    f.close()
    print("\n\nthe file is successfully written with the documentation string")

Upvotes: 4

Related Questions