![]() |
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
|
I try to import Vpython and I get a "ZeroDivisionError" - Jon2222 - Jul-23-2023 I'm new on Python, maybe my question is simple. I want to simulate some physical situations. I learned that Vpython makes it easy to create simple visualizations, allowing programmers to focus more on the computational aspect of their programs. So I want to learn Vpython. But I get a "ZeroDivisionError" Exception when I try to import vpython(the module I used is 7.6.4). I find the source code of Vpython and there is a "Exit" function:def Exit(): zero = 0. print('exit') a = 1.0/zeroThere is a 1.0/zero, it desn't make any sense. I think it is a very confusing behaviour. RE: I try to import Vpython and I get a "ZeroDivisionError" - snippsat - Jul-23-2023 (Jul-23-2023, 02:10 PM)Jon2222 Wrote: There is a 1.0/zero, it desn't make any sense. I think it is a very confusing behaviour.Commit 5051943 so there they add it,maybe not the best way to solve this. But it should have no impact on import of vpython library. Quick test. (dl_env) G:\div_code\dl_env λ python Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import vpython >>> >>> vpython.version ['7.6.4', 'jupyter'] >>> vpython.box() <vpython.vpython.box object at 0x000001C70A3BB610> >>> vpython.cylinder() <vpython.vpython.cylinder object at 0x000001C721AC3640> >>> RE: I try to import Vpython and I get a "ZeroDivisionError" - Jon2222 - Jul-24-2023 (Jul-23-2023, 07:24 PM)snippsat Wrote: But it should have no impact on import of vpython library.I use PyCharm and the IDE raises exception even if there is just one line of code to import vpython. from vpython import *[attachment=2468] RE: I try to import Vpython and I get a "ZeroDivisionError" - Gribouillis - Jul-24-2023 I suggest a dirty trick: import atexit _register, atexit.register = atexit.register, (lambda func, *args, **kwargs: func) try: from vpython import * finally: atexit.register = _register del _register RE: I try to import Vpython and I get a "ZeroDivisionError" - Jon2222 - Jul-24-2023 (Jul-24-2023, 10:05 AM)Gribouillis Wrote: I suggest a dirty trick: It worked!!! ![]() But I don't understand why did you modify a function to be executed at termination? I find out the source code of function register: def register(func, *args, **kwargs): # real signature unknown; restored from __doc__ """ register(func, *args, **kwargs) -> func Register a function to be executed upon normal program termination func - function to be called at exit args - optional arguments to pass to func kwargs - optional keyword arguments to pass to func func is returned to facilitate usage as a decorator. """ passI still have two problems even if I successfully imported Vpython with your suggestion.
RE: I try to import Vpython and I get a "ZeroDivisionError" - Gribouillis - Jul-24-2023 (Jul-24-2023, 12:54 PM)Jon2222 Wrote: I find out the source code of function register:This is not the actual code of function register. For CPython, the actual code is written in C in file atexitmodule.c (see the C function atexit_register() in this file). So I actually replaced a function that does something by a function that does nothing.We could replace by a function that targets more precisely the problem, for example import atexit import functools import contextlib def register(): _atexit_register = atexit.register def register(func, *args, **kwargs): if getattr(func, '__name__', '') == 'Exit': @functools.wraps(func) def wrapper(*args, **kwargs): with contextlib.suppress(ZeroDivisionError): return func(*args, **kwargs) _atexit_register(wrapper, *args, **kwargs) else: _atexit_register(func, *args, **kwargs) return func register.old = _atexit_register return register register = register() atexit.register = register try: from vpython import * finally: atexit.register = register.old del register RE: I try to import Vpython and I get a "ZeroDivisionError" - deanhystad - Jul-24-2023 @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. RE: I try to import Vpython and I get a "ZeroDivisionError" - Gribouillis - Jul-24-2023 (Jul-24-2023, 02:10 PM)deanhystad Wrote: My guess is the divide by zero function is a default exit function that you are meant to override.Isnt'it too late to override it after it has been registered by atexit, which seems to happen when the module is imported? In all cases, it should be documented somewhere. It looks so ridiculous a "feature". RE: I try to import Vpython and I get a "ZeroDivisionError" - deanhystad - Jul-24-2023 Also found this in the documentation. Quote:When running from a terminal, if the program does not end with a loop RE: I try to import Vpython and I get a "ZeroDivisionError" - Jon2222 - Jul-25-2023 (Jul-24-2023, 01:54 PM)Gribouillis Wrote:(Jul-24-2023, 12:54 PM)Jon2222 Wrote: I find out the source code of function register:This is not the actual code of function register. For CPython, the actual code is written in C in file atexitmodule.c (see the C function I understand this, thank you. |