3

To turn on Python Development Mode, you can use this flag:

python3 -X dev example.py

or use this environment variable:

PYTHONDEVMODE=1 python3 example.py

How can I write code within example.py to detect whether Python is running in Python Development Mode or not?

Flimm
  • 136,138
  • 45
  • 251
  • 267
  • Does this answer your question? [Check if my application runs in development/editable mode](https://stackoverflow.com/questions/40530000/check-if-my-application-runs-in-development-editable-mode) – Simon Hostettler Oct 23 '20 at 14:01
  • the doc speak about the variable `__debug__` – rioV8 Oct 23 '20 at 14:06
  • 2
    if you use the environment var method you could check `os.environ` – rioV8 Oct 23 '20 at 14:09
  • 1
    @SimonHostettler No, that doesn't answer my question, as that question talks about editable mode, not Python Development Mode. – Flimm Oct 23 '20 at 14:23
  • 1
    @rioV8 The variable `__debug__` is True even when Python Development Mode is not on. I just tried it by running `python3` and then running `python3 -X dev` – Flimm Oct 23 '20 at 14:24
  • It's a bit of a hacky solution, but you should be able to do it by raising one of the exceptions that wouldn't be visible unless you're in development mode, like `DeprecationWarning`. – ades Jan 16 '22 at 13:51

1 Answers1

1

sys.flags.dev_mode is True, when running as python -X dev app.py

Syscall
  • 19,327
  • 10
  • 37
  • 52
Sameera K
  • 1,298
  • 1
  • 17
  • 26