Python Forum
code to understand the script execution
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code to understand the script execution
#1
i'd like to have a script with a #! in the first line test to see if it was executed by a means that actually used the #! or if a python interpreter was explicitly run and the path to the script was passed to it in the first argument after any preceding options. python tweaks sys.argv so that the interpreter token is removed, making things consistent for the most common cases, but hindering this particular case. anyone have any ideas?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I don't think it is possible because the shell interpretes the shebang line #! before running the program: it adds the interpreter name in front of the program name. However, the OS knows the whole command line that started the process, and you can cheat by using an unlikely interpreter name in the shebang line. The following example uses module psutil together with this trick to detect if the shebang line was used:
#!/usr/bin/././././python3

import psutil

if __name__ == '__main__':
    proc = psutil.Process()
    cmd = proc.cmdline()
    if cmd[0] == '/usr/bin/././././python3':
        print("This script was probably run from the shebang line.")
    else:
        print("This script was called without shebang line.")
Reply
#3
cheating works. i learned this in school.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to understand the meaning of the line of code. jahuja73 0 346 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Different code execution times Wirbelwind94 4 815 Oct-06-2023, 12:30 PM
Last Post: snippsat
  In consistency in code execution Led_Zeppelin 1 1,153 Jun-27-2022, 03:00 AM
Last Post: deanhystad
  Unable to understand how given code takes a fixed input value, without inputting. jahuja73 4 2,783 Jan-28-2021, 05:22 PM
Last Post: snippsat
  Don't understand example code from a textbook lil_fentanyl 1 1,864 Jan-25-2021, 07:02 PM
Last Post: nilamo
  PyCharm Script Execution Time? muzikman 3 8,573 Dec-14-2020, 11:22 PM
Last Post: muzikman
  Unable to understand a statement in an existing code ateestructural 1 2,266 Aug-01-2020, 09:38 PM
Last Post: deanhystad
  Trying to understand the python code spalisetty 2 1,922 Mar-16-2020, 08:11 AM
Last Post: javiertzr01
  I couldn't understand the output of the below code ravich129 1 1,958 Dec-12-2019, 06:24 AM
Last Post: sandeep_ganga
  Minimizing the CMD window during code execution Shaswat 1 4,648 Oct-03-2019, 07:44 AM
Last Post: Shaswat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020