KDE Developer
|
Hi,
my problem is that code completion is not working for the pytorch module.
gives only ZeroDivisionError, but not the torch.zeros() function, which is in the torch module. Strangely, torch.tensor works (torch.tensor is a class, while torch.zeros is a function afaik). but after creating a torch.tensor object, i can't complete on its methods. I can't complete on method parameters either. I believe that it has worked on my machine before, but i might be mistaken. my environment: I'm using kdevelop 5.4.2 from the kubuntu repositories, python 3.7 and anaconda. anaconda is installed into ~/bin/anaconda.
what I have tried so far: * code completion works on standard modules like math * starting via PYTHONPATH=/home/me/bin/anaconda3/lib/python3.7/site-packages/ kdevelop * setting the python interpreter path in the project configuration to /home/me/bin/anaconda3/bin/python * in kdevelop settings / python documentation generate docs for the torch module generating the documentation module looks successful, but i'm not sure it runs correctly as the return type is 'None' for most (or all) functions:
some suspicious shell outputs while running Kdevelop are:
(but those didn't appear every time i started kdevelop) |
Registered Member
|
I could reproduce this locally.
Because pytorch is mostly not written in Python, kdev-python can't parse it directly to find functions or types and relies on the stubfile produced by the 'Generate Documentation' tool. There appears to be a bug in the documentation generator, which causes a syntax error in the generated Python file. If you edit `~/.local/share/kdevpythonsupport/documentation_files/torch.py`, and edit line 13609 to replace `def torch.ops()` with just `def ops()`it should work. torch.tensor works anyway because that's one of the few parts of Torch that actually *does* exist in Python code - there's a `site-packages/torch/tensor.py` file, and kdev-python parses it fine. EDIT: The return types of the generated stubs are indeed `None`, which is an expected limitation of the generator. At some point, kdev-python might be able to use the `.pyi` stubfile shipped with pytorch instead of trying to generate one itself, which would solve both of these issues. |
KDE Developer
|
Thanks a lot!
flherne posted a workaround in the irc chat, namely copying the .pyi files into documentation_files/torch.py this gives not perfect, but okayish completion. Certainly much better than before not sure this is overkill or not, but i wrote a short script to do that for all torch modules:
use at your own risk! not perfect: torch.tensor doesn't auto complete for instance
|
Registered Member
|
The super class of ZeroDivisionError is ArithmeticError. This exception raised when the second argument of a division or modulo operation is zero. In Mathematics, when a number is divided by a zero, the result is an infinite number. It is impossible to write an Infinite number physically. Python interpreter throws “ZeroDivisionError: division by zero” error if the result is infinite number. While implementing any program logic and there is division operation make sure always handle ArithmeticError or ZeroDivisionError so that program will not terminate.
Or check before you do the division:
The latter can be reduced to:
|
Registered users: bartoloni, Bing [Bot], Google [Bot], Sogou [Bot], Yahoo [Bot]