1

If I define my own debugging module with functions that should only get executed while developing, is there a way to completely disable those functions when I'm ready to release the final version?

I was hoping there was a clever way to have the environment completely skip over the function calls during the byte-code conversion? I searched for this, but may be using the wrong search inputs.

I'm developing an add-on for Blender, so I don't believe I have any control over the compilation or conversion.

4
  • Wat do yo mean by a "debugigng module" and skipping function calls. What sort of functions are you referring to that are only debugging? Like tests? Maybe offer some code examples. Commented Oct 19, 2019 at 12:46
  • Yeah, extensive testing calls. Making sure parameters have expected values, comparing data, trying to prevent myself from making dumb mistakes (a pattern I seem to repeat). Commented Oct 19, 2019 at 12:51
  • Maybe look into the Python unittest module. It probably has the capability to do what you need. Commented Oct 19, 2019 at 12:54
  • Sorry, by skipping function calls, I mean something such as converting the function calls into comments. Something like that would work. I will take a look at that. Thanks Commented Oct 19, 2019 at 12:55

1 Answer 1

3

Yes, Python has a -O flag, which means:

Remove assert statements and any code conditional on the value of __debug__

So basically if you write your debug code using either assert statements, or you check the value of __debug__ before running your debugging functions, you can use -O to switch on a production mode:

if __debug__:
    run_debug_function()

You can also enable optimization by setting the PYTHONOPTIMIZE environment variable to some non-empty string, e.g. export PYTHONOPTIMIZE=1 in your shell.

For more info refer to the Python command line documentation

Sign up to request clarification or add additional context in comments.

3 Comments

This would be great. I just need to figure out if I can turn __debug__ on or off in Blender. Thanks.
It seems that you might be able to set PYTHONOPTIMIZE in your shell before you run Blender, as in this answer: stackoverflow.com/a/46025811/2148718
Actually this is a more relevant answer: stackoverflow.com/a/44060884/2148718

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.