'How determine Python script run from IDE or standalone

I've recently started learning python and am still a newbie. How can I determine if my code run from IDE or run standalone? My code is not imported so __name__ == "__main__" . Someone suggested checking sys.executable name but I'm looking for a solution independent of the file name.

P.S: I create a standalone file with pyinstaller.



Solution 1:[1]

Here's a link to the page pyinstaller has on their site giving information about how to check the Run-Time information of your file: https://pyinstaller.org/en/stable/runtime-information.html

Basically it says to add the following code

import sys
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
    print('running in a PyInstaller bundle')
else:
    print('running in a normal Python process')

This should print the 'running in a PyInstaller bundle' message if it's all self contained and properly bundled, and it should print 'running in a normal Python process' if it's still running from your source code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1