1

I am currently writing a python file that is meant to be run through the command line like pip and npm, but I also need to know when the user launches it directly through the file explorer (as in windows). Is this completely impossible (restricted to the program only knowing that it's run with no sys.argv arguments), or is there a way to make the program differentiate if it's being run directly through something like the file explorer, or if it's being run through the command line? Thanks!

Atalajaka
  • 125
  • 1
  • 2
  • 14
Ceyhun
  • 39
  • 7
  • 1
    From the program's perspective, there is no difference. When you execute it through file explorer, the operating system _does_ invisibly just invoke the script using the command line. Checking for `sys.argv` not having the required arguments is probably the best solution for you – Green Cloak Guy Feb 28 '22 at 15:19
  • You could probably obtain some answers [here](https://stackoverflow.com/questions/9839240/how-to-determine-if-python-script-was-run-via-command-line) and/or [here](https://stackoverflow.com/questions/29239698/determine-if-the-program-is-called-from-a-script-in-python). – Atalajaka Feb 28 '22 at 15:20
  • yeah - my script in fact already does check if there are no arguments and gives an error if not, but seems like checking specifically if its being run from the file explorer seems pretty hard to do (even if possible). i'm still gonna be on the lookout for any answers here and there, though – Ceyhun Feb 28 '22 at 15:20
  • https://stackoverflow.com/questions/55172090/detect-if-python-program-is-executed-via-windows-gui-double-click-vs-command-p?noredirect=1&lq=1 just found out this question is literally the exact same as the one i was looking for, thanks related tab lol – Ceyhun Feb 28 '22 at 15:23
  • Check this question this may answer it https://stackoverflow.com/questions/55172090/detect-if-python-program-is-executed-via-windows-gui-double-click-vs-command-p? – Devil Ishere Feb 28 '22 at 15:35
  • yeah i just posted the same exact link, thanks though – Ceyhun Feb 28 '22 at 15:36

3 Answers3

1

The second answer provided in Detect if python program is executed via Windows GUI (double-click) vs command prompt functions correctly for the program to check if the program was run thru a GUI works. Thanks for the help guys!

Ceyhun
  • 39
  • 7
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 28 '22 at 16:51
1

Be sure to capture the user's operating system first before implementing a Windows specific approach.

bentindal
  • 11
  • 1
  • this could be made into a comment instead of an answer but my program isn't as big as to get people from over linux and macos to run the app so i don't really check for those things but nice idea! – Ceyhun Feb 28 '22 at 15:54
0

It appears the use of the parent process ID could be used to determine how the program has been executed, using some python libraries to do so.

This post and the other one here could provide with something useful for what you're looking for.

Atalajaka
  • 125
  • 1
  • 2
  • 14
  • both answers aren't quite functional - psutil always returns "py.exe" instead of something like "cmd.exe". First answer also doesn't seem to be working – Ceyhun Feb 28 '22 at 15:31