How can a Python program determine if it was executed as an executable file on a Unix system instead of being called as a script?
./program.py
instead of
python ./program.py
'/program' in sys.argv[0]
cannot distinguish between the example cases.
A somewhat hackish solution would be adding an environment variable indicating this to the shebang line in program.py
:
#!/usr/bin/env noscript=True python
import os
if os.getenv('noscript'):
print("called as executable")
else:
print("called as script")