I'm running a python script from inside a different software (it provides a python interface to manipulate its data structures).
I'm optimizing my code for speed and would like to see what impact on performance my asserts have.
I'm unable to use python -O
. What other options do I have, to programatically disable all asserts in python code? The variable __debug__
(which is cleared by -O
flag) cannot be assigned to :(
Using the -O flag (capital O) disables all assert statements in a process.
Steps to avoid Assertion Error in Python We can use the -O flag to disable all the assert statements present in the process. By disabling the assert statements in the process, the statements following the assert statements will also be not executed.
Assert statements are used to debug code and handle errors. You should not use an assert statement in a production environment. Debugging is a crucial part of programming in any language. When debugging programs in Python, there may be times when you want to test for a certain condition.
The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError.
The docs say,
The value for the built-in variable [
__debug__
] is determined when the interpreter starts.
So, if you can not control how the python interpreter is started, then it looks like you can not disable assert.
Here then are some other options:
If all your assert statements occur on lines by themselves, then perhaps you could remove them with
sed -i 's/assert /pass #assert /g' script.py
Note that this will mangle your code if other code comes after the assert. For example, the sed command above would comment-out the return
in a line like this:
assert x; return True
which would change the logic of your program.
If you have code like this, it would probably be best to manually remove the asserts.
There might be a way to remove them programmatically by parsing your
script with the tokenize
module, but writing such a program to
remove asserts may take more time than it would take to manually
remove the asserts, especially if this is a one-time job.
If the other piece of software accepts .pyc files, then there is a
dirty trick which seems to work on my machine, though note a Python
core developer warns against this (See Éric Araujo's comment on 2011-09-17). Suppose your script is called script.py
.
Make a temporary script called, say, temp.py:
import script
python -O temp.py
. This creates script.pyo
.script.py
and script.pyc
(if it exists) out of your PYTHONPATH
or whatever directory the other software is reading to find your
script.script.pyo
--> script.pyc
.Now when the other software tries to import your script, it will
only find the pyc
file, which has the asserts removed.
For example, if script.py
looks like this:
assert False
print('Got here')
then running python temp.py
will now print Got here
instead of raising an AssertionError.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With