Python 2.7 and "ValueError: Attempted relative import in non-package" - Printable Version +- Kodi Community Forum (https://forum.kodi.tv) +-- Forum: Development (https://forum.kodi.tv/forumdisplay.php?fid=32) +--- Forum: Add-ons (https://forum.kodi.tv/forumdisplay.php?fid=26) +--- Thread: Python 2.7 and "ValueError: Attempted relative import in non-package" (/showthread.php?tid=343005) |
Python 2.7 and "ValueError: Attempted relative import in non-package" - bkiziuk - 2019-04-14 I've been trying to adjust my code to be both Python 2&3 friendly, based mainly on kodi.wiki but also Googling around as it doesn't work even if everybody says that it should. I have structure: Code: service.subsmangler/ In contextmenu.py I need to import common.py, so in Python 2 I had just:
Now, in order to adjust it also to Python 3, all sources say that this should be changed to: but after changing it, I get an error in Kodi log saying: Code:
Is there anything that I'm still missing? RE: Python 2.7 and "ValueError: Attempted relative import in non-package" - pkscout - 2019-04-14 I'll preface this by saying that I'm a Python amateur that has learned as I went. That said, with the exception of my Kodi addons, all the Python work I do is in Python3, so hopefully this advice will be right (if not the most Pythonic way). I'm pretty sure from contextmenu.py you can just use the same format you did for Python2. At least I have and haven't run into any problems. RE: Python 2.7 and "ValueError: Attempted relative import in non-package" - gujal - 2019-04-14 Try with
RE: Python 2.7 and "ValueError: Attempted relative import in non-package" - bkiziuk - 2019-04-15 (2019-04-14, 22:56)gujal Wrote: Try with I tried it as well: Code: 2019-04-15 18:01:25.901 T:139931410380544 ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<-- This is the thing I don't really understand. If there's __init__.py file, the whole directory should be treated as a package. I believe I simply don't understand how it works under the hood, but all tutorials say "make __init__.py file" and apparently for majority of the people this just works RE: Python 2.7 and "ValueError: Attempted relative import in non-package" - Roman_V_M - 2019-04-16 @bkiziuk Let me guess, contextmenu.py is your entry-point script. Entry-point scripts are not considered a part of a package (their own location path prevails when resolving module/package names) so relative intra-package import syntax does not apply here. |