Python Forum
I try to import Vpython and I get a "ZeroDivisionError" - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: I try to import Vpython and I get a "ZeroDivisionError" (/thread-40405.html)

Pages: 1 2


RE: I try to import Vpython and I get a "ZeroDivisionError" - Jon2222 - Jul-25-2023

(Jul-24-2023, 02:10 PM)deanhystad Wrote: @snippsat is correct. You do not get the error when importing the library. It only has an effect when the program exits, which happens immediately if your program consists solely of "import vpython". Try this:
import vpython
import time

time.sleep(10)
No error until 10 seconds has passed.

My guess is the divide by zero function is a default exit function that you are meant to override.

I tried it and it doesn't work. The error is ZeroDivisionError as before.
Error:
D:\pythonProject\OpenCVLearning\venv\Scripts\python.exe D:\pythonProject\vpython\main.py Exception ignored in atexit callback: <function Exit at 0x0000027DBAADB420> Traceback (most recent call last): File "D:\pythonProject\OpenCVLearning\venv\Lib\site-packages\vpython\vpython.py", line 22, in Exit a = 1.0/zero ~~~^~~~~ ZeroDivisionError: float division by zero exit Process finished with exit code 0
This is screenshot of the error.
[attachment=2470]


RE: I try to import Vpython and I get a "ZeroDivisionError" - Gribouillis - Jul-25-2023

You could also try this
import atexit
import vpython
atexit.unregister(vpython.Exit)



RE: I try to import Vpython and I get a "ZeroDivisionError" - deanhystad - Jul-25-2023

(Jul-25-2023, 08:28 AM)Gribouillis Wrote: You could also try this
import atexit
import vpython
atexit.unregister(vpython.Exit)
Exit is not exposed through the module interface.

I did find this neat trick to get a list of registered atexit functions.

https://stackoverflow.com/questions/16042463/how-can-i-get-the-list-of-registered-atexit-functions-in-python3

So you could write this:
import vpython
import atexit

def unregister_by_name(name):
    funs = []

    class Capture:
        def __eq__(self, other):
            funs.append(other)
            return False

    c = Capture()
    atexit.unregister(c)
    for func in funs:
        if func.__name__ == name:
            atexit.unregister(func)

unregister_by_name("Exit")
It is easier to edit the vpython.py file. I wish I could fine something about the intention behind Exit().


RE: I try to import Vpython and I get a "ZeroDivisionError" - Jon2222 - Jul-25-2023

(Jul-25-2023, 08:28 AM)Gribouillis Wrote: You could also try this
import atexit
import vpython
atexit.unregister(vpython.Exit)

I doesn't work. Huh

Error:
D:\pythonProject\OpenCVLearning\venv\Scripts\python.exe D:\pythonProject\vpython\main.py Traceback (most recent call last): File "D:\pythonProject\vpython\main.py", line 3, in <module> atexit.unregister(vpython.Exit) ^^^^^^^^^^^^ AttributeError: module 'vpython' has no attribute 'Exit' exit Exception ignored in atexit callback: <function Exit at 0x0000026DC110B420> Traceback (most recent call last): File "D:\pythonProject\OpenCVLearning\venv\Lib\site-packages\vpython\vpython.py", line 22, in Exit a = 1.0/zero ~~~^~~~~ ZeroDivisionError: float division by zero Process finished with exit code 1
[attachment=2471]


RE: I try to import Vpython and I get a "ZeroDivisionError" - Jon2222 - Jul-25-2023

(Jul-25-2023, 11:04 AM)deanhystad Wrote:
(Jul-25-2023, 08:28 AM)Gribouillis Wrote: You could also try this
import atexit
import vpython
atexit.unregister(vpython.Exit)
Exit is not exposed through the module interface.

I did find this neat trick to get a list of registered atexit functions.

https://stackoverflow.com/questions/16042463/how-can-i-get-the-list-of-registered-atexit-functions-in-python3

So you could write this:
import vpython
import atexit

def unregister_by_name(name):
    funs = []

    class Capture:
        def __eq__(self, other):
            funs.append(other)
            return False

    c = Capture()
    atexit.unregister(c)
    for func in funs:
        if func.__name__ == name:
            atexit.unregister(func)

unregister_by_name("Exit")
It is easier to edit the vpython.py file. I wish I could fine something about the intention behind Exit().


Your code runs successfully on my computer. But I would like to know if is there something wrong with my computer. I think developers of vpython must never upload a library that doesn't work. Undecided


RE: I try to import Vpython and I get a "ZeroDivisionError" - deanhystad - Jul-25-2023

The library works. It just has an ugly exit behavior. You'll be able to use it just fine, making all kinds of pretty pictures and animations, and all weill be well until your program quits. That is when Exit gets called. When used in a notebook you wouldn't see this until you closed the notebook. It a web page you wouldn't see it at all.


RE: I try to import Vpython and I get a "ZeroDivisionError" - Jon2222 - Jul-26-2023

(Jul-25-2023, 12:42 PM)deanhystad Wrote: The library works. It just has an ugly exit behavior. You'll be able to use it just fine, making all kinds of pretty pictures and animations, and all weill be well until your program quits. That is when Exit gets called. When used in a notebook you wouldn't see this until you closed the notebook. It a web page you wouldn't see it at all.

Sure, thank you!!! Smile