Python 2.7 and "ValueError: Attempted relative import in non-package"
#1
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/
----resources/
--------__init__.py
--------lib/
------------__init__.py
------------common.py
------------contextmenu.py

In contextmenu.py I need to import common.py, so in Python 2 I had just:
python:
import common

Now, in order to adjust it also to Python 3, all sources say that this should be changed to:
python:
from __future__ import absolute_import
from . import common
but after changing it, I get an error in Kodi log saying:
Code:

2019-04-14 18:15:58.775 T:140410114529024   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.ValueError'>
                                            Error Contents: Attempted relative import in non-package
                                            Traceback (most recent call last):
                                              File "/home/kiziuk/.kodi/addons/service.subsmangler/resources/lib/contextmenu.py", line 9, in <module>
                                                from . import common
                                            ValueError: Attempted relative import in non-package
                                            -->End of Python script error report<--

Is there anything that I'm still missing?
RPi4; LibreElec
Reply
#2
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.
Reply
#3
Try with
python:

from resources.lib import common
Kodi 21RC2 Windows 10 and 11 | 21RC2 Xbox One X | 21RC2 Linux Mint Virginia XFCE | CoreELEC NO 21 nightly S905X4 aarch64
Reply
#4
(2019-04-14, 22:56)gujal Wrote: Try with
python:

from resources.lib import common

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<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.ImportError'>
                                            Error Contents: No module named resources.lib
                                            Traceback (most recent call last):
                                              File "/home/kiziuk/.kodi/addons/service.subsmangler/resources/lib/contextmenu.py", line 9, in <module>
                                                from resources.lib import common
                                            ImportError: No module named resources.lib
                                            -->End of Python script error report<--

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  Confused
RPi4; LibreElec
Reply
#5
@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.
Reply

Logout Mark Read Team Forum Stats Members Help
Python 2.7 and "ValueError: Attempted relative import in non-package"0