enthought / enthought/traitsui
etsdemo 'Run' button does not execute script in __main__ block
- Dominant language
- Python
- Stars
- 306
- Forks
- 99
- PR merge metrics
- No merged PRs in 30d
Description
If one has a script that contains a block like this:
```
if __name__ == "__main__":
...
```
Trying to run this script from the `etsdemo` application by pressing the 'Run' button will not run that block, which is unexpected.
To reproduce, create a file like this:
```
if __name__ == "__main__":
print("Hello")
```
Then run this:
```
from etsdemo.main import main
main(
[
{
"version": 1,
"name": "Some name",
"root": ".", # or a path to a directory that contains the above test file.
},
]
)
```
That block is not run when one presses the 'Run' button.

But if one changes the `"__main__"` to `"___main___"` (three underscores), then it would run:

From the snapshot of an old version of the application, this `___main___` is not a typo: It looks deliberate.
https://github.com/enthought/traitsui/blob/96f45ff9c51ff55b5724cbed2361b933f468c183/enthought/traits/ui/extras/demo.py
Looks like the [`run_code`](https://github.com/enthought/traitsui/blob/f7da6909ad1cecf457f61df5970c9281575ec61f/ets-demo/etsdemo/app.py#L462) method is serving two purposes:
(1) When the file is clicked, it loads the file to find this specially named variable 'demo'.
(2) When the Run button is clicked, it runs the file too.
For the first purpose, we don't want the block in `if __name__ == "__main__"` to run. For the second purpose, we do.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.