Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print statements not showing up when running script in terminal

I am new to running python scripts in the terminal. I have ran the script ./filename.py and made sure it is executable with chmod +x filename. I also put #!/usr/bin/env python at the top of my program. i am getting no errors but none of my print statements are showing in my terminal. attached is my code. any ideas?

#!/usr/bin/env python

import ctypes
import os

def is_hidden(filepath):
    name = os.path.basename(os.path.abspath(filepath))
    return ('.' + name) or (has_hidden_attribute(filepath))

def has_hidden_attribute(filepath):
    try:
        attrs = ctypes.windll.kernel32.GetFileAttributesW(unicode(filepath))
        assert attrs != -1
        result = bool(attrs & 2)
    except (AttributeError, AssertionError):
        result = False
    return result

def main():
    print ('whatup')
    print(is_hidden('~/.jupyter'))
    print('hey')

And then from the terminal

$ ./makepass_jup.py
$ 
like image 574
Emily Avatar asked Dec 10 '22 13:12

Emily


1 Answers

You are not calling your main function anywhere. Add this at the end of the file:

if __name__ == '__main__':
    main()

if statement here is the common pattern in Python. It works as a guard to prevent executing code when importing file as a module. When Python interpreter imports the file, it sets __name__ variable. If this file is being imported from another module, __name__ will be set to the module's name. But if the file was executed as the main program the __name__ variable will be set to __main__, so the code inside that statement will be executed only if the file is executed as a program.

See the accepted answer to the question What does if __name__ == “__main__”: do? for more information.

like image 121
Olexander Yermakov Avatar answered Feb 05 '23 13:02

Olexander Yermakov