6

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
$ 
o-90
  • 17,045
  • 10
  • 39
  • 63
Emily
  • 79
  • 1
  • 1
  • 4

3 Answers3

10

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.

Community
  • 1
  • 1
  • Please explain what this code does / how it works. Why not just add `main()` call at the end? – martineau Jan 27 '17 at 18:53
  • That's not how or why, just _what_ it does. – martineau Jan 27 '17 at 18:56
  • 1
    Python interpreter executes all the code in the file. So if you ever import this file, the code will be always executed if you don't use that if statement. But if run it from command line, `__name__` variable, which contains the name of the module will be '__main__' and the code will be executed. Basically the idea is to prevent executing code you don't want to be executed if you are importing the file as a module. Please refer to this question for more details: http://stackoverflow.com/questions/419163/what-does-if-name-main-do – Olexander Yermakov Jan 27 '17 at 18:58
  • 1
    I suggest that you [edit] your answer and add the explanation—then it might be worth an up-vote, IMO. – martineau Jan 27 '17 at 19:02
  • 2
    I agree with @martineau. Although I am guilty of this myself, we should not encourage [cargo-cult programming](https://en.wikipedia.org/wiki/Cargo_cult_programming). – juanpa.arrivillaga Jan 27 '17 at 19:04
  • 1
    Edited my answer. Thanks @martineau for the advice. – Olexander Yermakov Jan 27 '17 at 19:09
  • The other answer has this exact info in it – s g Jan 28 '17 at 04:30
6

In Python, the main() function is not an entry point, unlike other languages like C or Java. Like other people pointed out, your main() is not being called anywhere in your code.

You may just call your main() function at the top level of your module as follows:

import ctypes
import os

(your functions)

main()

However, everything at the top level will be evaluated when either you run this directly with the python command or import this module from somewhere else. Suppose the file name of your script is callee.py and you are writing another script called caller.py as follows:

import callee

Then all statements at the top level of callee.py will be evaluated, including main(). If this is not your intention, you can check if your script was directly invoked from the command line by checking the value of __name__ variable.

if __name__ == '__main__':
    main()

If your script is directly called from the command line, the value of __name__ will be __main__. Otherwise it will be the name of the module. In this case, callee.

sbbs
  • 1,450
  • 2
  • 13
  • 20
1

This script just defines a bunch of functions. You need to actually call main() in order to execute it.

Mureinik
  • 297,002
  • 52
  • 306
  • 350