#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
2 Answers 2
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
ForceBru
45.1k10 gold badges72 silver badges104 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
This is because change is never invoked. Invoke it at the end of the file with change()
answered Apr 17, 2015 at 13:02
Nafiul Islam
82.9k33 gold badges145 silver badges202 bronze badges
Comments
lang-py