4

How do I disable assertions and docstrings, respectively, in an IPython session or a Jupyter Notebook?

After the official python documentation, this stackoverflow question and this answer to a similar question, I may “set the PYTHONOPTIMIZE environment variable to 1”. Well, if I set the environment variable following this blog post via

%env PYTHONOPTIMIZE=1

I can successfully list the value along all other variables with %env, but still unexpectedly __debug__ == True and AssertionErrors are raised. Different values like 2 or a string "1" weren't successful, either.

Bonus question: Is it possible to disable assert only for a specific (local) library, like after using import myfancylib, myfancylib.eggs() won't raise?

1 Answer 1

0

I am not aware of a way to disable assert only for the currently active session. However, there are a couple of other options.

Non volatile disable assert for current conda environment:

_disable_assert = True
_disable_assert_msg = "To make your changes take effect please Save and restart Jupyter Notebook\n"
if _disable_assert:
    try:
        assert False
        print("OK - assert disabled")
    except AssertionError:
        import os
        import sys
        
        os.system("conda env config vars set PYTHONOPTIMIZE=1")
        print(_disable_assert_msg, file=sys.stderr)
        assert False, _disable_assert_msg
else:
    try:
        assert False

        import os
        import sys

        os.system("conda env config vars unset PYTHONOPTIMIZE")
        print(_disable_assert_msg, file=sys.stderr)
        _zero_division = 1/0
    except AssertionError:
        print("OK - assert enabled")

Disable assert for cell:

%%python -O 
# Only for one cell. This cell is run in a separate process.

import sys
import os

print(__debug__)
print(sys.argv)
print(os.environ)

try:
    assert False
    print("Assert disable")
except AssertionError:
    print("Assert enable")
Sign up to request clarification or add additional context in comments.

4 Comments

You mean just setting a global variable _disable_assert = True works?
@aaron, Sorry, I didn't understand your question. _disable_assert and _disable_assert_msg are internal variables of this code snippet. They start with _ to reduce the likelihood of conflict with other parts of the code.
@aaron, There are several ways to set the PYTHONOPTIMIZE variable before starting python: command-line utilities, editing configuration files, etc. Personally, I find this method of change convenient, directly in notepad. It seems that the author of the question was also looking for something similar to %env.
Ah, it would be clearer to define that in a function def set_disable_alert(disable_assert):. Your code snippet as-is appears to imply that setting the global variable _disable_assert = True disables assert.

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.