How to get rid of warning-->RuntimeWarning: divide by zero encountered in true_divide

I have this sample code:

x = 5
y = 0

z = x/y

But I got warning -->RuntimeWarning: divide by zero encountered in true_divide
Is there any way how to get rid of this warning while having the same output?
Please help…thanks

You only get a warning for that? Not an error? What Python implementation/version are you using?

1 Like

Ah ok…i using pycharm…by the way, can we still get rid this warning?

I don’t know. I’m not familiar with that warning and I can’t reproduce it with your code. I googled the warning and only found it for NumPy, but you’re not using NumPy, just regular Python ints.

Yes you are right…I have googled the solution but only numpy have solution…thanks

Ah ok…i using pycharm…by the way, can we still get rid this
warning?

A RuntimeError? In Python 3.8 (on a Mac, and not using PyCharm) I get
this:

 Python 3.8.13 (default, Aug 11 2022, 15:46:53)
 [Clang 12.0.0 (clang-1200.0.32.29)] on darwin
 Type "help", "copyright", "credits" or "license" for more 
 information.
 >>> x=5
 >>> y=0
 >>> z=x/y
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 ZeroDivisionError: division by zero
 >>>

Cheers,
Cameron Simpson cs@cskk.id.au

This is a PyCharm feature: it runs a linter over your code before running it, looking for problems.

PyCharm calls these “inspections”, and you can turn them off either globally or just for a single line.

@steven.daprano Interesting, I wonder how powerful those inspections are. Why is it called a “RuntimeWarning”, though, if that happens before running it? And can you actually reproduce this?

That’s a good question!

No I haven’t reproduced the warning. I should have been more explicit that I am just guessing that it’s PyCharm’s linter, sorry.

Hopefully the OP will respond with either confirmation that disabling the inspection in PyCharm fixed the issue or more information.