Python Forum
I try to import Vpython and I get a "ZeroDivisionError"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I try to import Vpython and I get a "ZeroDivisionError"
#11
(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.
   
Reply
#12
You could also try this
import atexit
import vpython
atexit.unregister(vpython.Exit)
Reply
#13
(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/1604...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().
Jon2222 and Gribouillis like this post
Reply
#14
(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
   
Reply
#15
(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/1604...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
Reply
#16
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.
Reply
#17
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ZeroDivisionError laurawoods 1 212 Apr-11-2024, 03:27 AM
Last Post: deanhystad
  Overcoming ZeroDivisionError: division by zero Error dgrunwal 8 5,053 Jun-12-2020, 01:52 PM
Last Post: dgrunwal
  ZeroDivisionError CIVILmath 2 2,524 Jan-17-2019, 02:38 AM
Last Post: CIVILmath
  Vpython Delay in plotting points SohaibAJ 0 2,073 Jul-30-2018, 08:44 PM
Last Post: SohaibAJ
  Anaconda / Spyder / VPython mwatson87 3 90,318 May-24-2018, 01:51 PM
Last Post: snippsat
  VPython - Trying to make orbit simulation totalnoob 2 5,636 Sep-13-2017, 03:59 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020