enthought / enthought/traitsui
Temporarily modify sys.path when executing example file in etsdemo
- Dominant language
- Python
- Stars
- 306
- Forks
- 99
- PR merge metrics
- No merged PRs in 30d
Description
Context: Often demo examples are written assuming they are run from the command line. Sometimes these examples require importing components from outside of that file, e.g. [here](https://github.com/enthought/enable/blob/6cf525cbeefdf34dc6eac5e54d7a73f755ea79b1/examples/enable/canvas_demo.py#L10), [here](https://github.com/enthought/envisage/blob/4e2c07a0aded7360639e379b99b6f05e4dd92210/envisage/examples/demo/plugins/tasks/run_attractor.py#L11-L12). However, when these examples are run from within the etsdemo, the `sys.path` is set for running the `etsdemo` application, and these imports would normally fail because they assume the local directory being on the path, which isn't true.
This inconsistency adds cognitive load to developers maintaining the examples: Developers need to keep in mind that the path is not what they typically expect.
The proposed solution would be to temporarily prepend to the `sys.path` the path of the directory containing the example file, similar to the behaviour when one runs the file from the command line.
This requires knowing exactly when the run context starts and ends, as the `sys.path` is a global state and it must be reverted back to how it was when the run context for an example terminates.
(Removing the demo tab will make determining the life-time of the run context a lot easier, see #1454)
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.