1
#conf.py
def init():
 global mylist 
 mylist=[]
#change.py
import conf
def change():
 if __name__ == "__main__":
 print('Direct')
 conf.mylist.append('Directly executed')
 print(conf.mylist)
 else:
 conf.mylist.append('It was imported') 
#exec.py
import conf
import change
conf.init() 
change.change()
print (conf.mylist)

When running exec.py the result is what I expected, but when running change.py directly I didn't get any output (no Direct , no conf.mylist)

ForceBru
45.1k10 gold badges72 silver badges104 bronze badges
asked Apr 17, 2015 at 13:00

2 Answers 2

9

Yes, that's the normal behavior. You need to call the change function for this code to execute.

You can add the following to the end of change.py

if __name__=="__main__":
 change()
answered Apr 17, 2015 at 13:02
Sign up to request clarification or add additional context in comments.

Comments

2

This is because change is never invoked. Invoke it at the end of the file with change()

answered Apr 17, 2015 at 13:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.