Super basic question on python packages, apologies in advance!
#1
Sorry sorry sorry, I'm very new to Python and only using it in the context of trying to develop an addon for my Kodi skin. trying to get my head around importing of packages.

In VSCode I see certain warning for xbmc packages with Pylance saying they can't be resolved because they are missing from my environment (e.g. import xbmcgui). I understand this not to be a problem because Kodi includes these packages so the addon can use them fine within the kodi environment. Anyone who installs the addon will also have Kodi so they will always have access to them. However other simple packages I have used like json, urlparse and sys do not come with warnings. Does this mean they are installed in my local environment already? Do I need to make sure that they are also included in Kodi environment? I assume they are as lots of people use them...

I want to try and import a third-party package to be able to manipulate images from the Kodi library (Pillow for cropping clearlogos). I get the same warnings with 'From PIL import Image'. I read (https://kodi.wiki/view/Category:Add-on_l...es/modules) I can access some third-party packages as other addons and include missing ones in my addon. But I couldn't find any examples of this to follow. Would this need to be done for PIL or would this already be in Kodi environment?

Is there a way to point my VSCode environment to Kodi somehow so it knows what's available and what's not?

Apologies again. The more I read the more confused I'm getting.
Reply
#2
Yea, this is always a little hard for someone new to Kodi (and/or Python) to wrap their head around.  By default a Kodi addon can access the standard core Python imports.  So things like json and urlparse are easy.  You just import them.  Any additional packages have to come as a Kodi add-on.  There are lots of those available, but not every package in Python necessarily has a Kodi add-on since someone has to repackage it and submit it to one of the Kodi repos.  As you've found, PIL is one that has a Kodi package.

Now to add the next complication.  Your VSCode environment is accessing the Python installed on your local machine, not the Kodi one.  So it has no idea what Kodi python add-ons you might have.  So it's going to complain if you are referencing PIL in your addon but don't have PIL in your local Python environment.  I am not a VSCode expert, so hopefully someone else can give a better answer, but I think you have two choices:
  1. ignore the warnings from VSCode
  2. install PIL in your local python environment so VSCode won't complain.
#2 doesn't mean those python packages magically become available to Kodi, it just keeps VSCode from complaining.  When you test your addon in Kodi, you still have to make sure the add-on packages are installed for it to work.
Reply
#3
(2023-02-23, 11:33)pkscout Wrote: Yea, this is always a little hard for someone new to Kodi (and/or Python) to wrap their head around.  By default a Kodi addon can access the standard core Python imports.  So things like json and urlparse are easy.  You just import them.  Any additional packages have to come as a Kodi add-on.  There are lots of those available, but not every package in Python necessarily has a Kodi add-on since someone has to repackage it and submit it to one of the Kodi repos.  As you've found, PIL is one that has a Kodi package.

Now to add the next complication.  Your VSCode environment is accessing the Python installed on your local machine, not the Kodi one.  So it has no idea what Kodi python add-ons you might have.  So it's going to complain if you are referencing PIL in your addon but don't have PIL in your local Python environment.  I am not a VSCode expert, so hopefully someone else can give a better answer, but I think you have two choices:
  1. ignore the warnings from VSCode
  2. install PIL in your local python environment so VSCode won't complain.
#2 doesn't mean those python packages magically become available to Kodi, it just keeps VSCode from complaining.  When you test your addon in Kodi, you still have to make sure the add-on packages are installed for it to work.
Thanks @pkscout - you set my mind at ease that I didn't have completely the wrong end of the stick. 

While I was figuring out an error the error tracing pointed me to this location, which I assume is the packages that come with Kodi as standard (PIL was in 'site-packages'): C:\Program Files\Kodi\system\python\Lib\
Reply
#4
(2023-02-23, 12:25)realcopacetic Wrote:
(2023-02-23, 11:33)pkscout Wrote: Yea, this is always a little hard for someone new to Kodi (and/or Python) to wrap their head around.  By default a Kodi addon can access the standard core Python imports.  So things like json and urlparse are easy.  You just import them.  Any additional packages have to come as a Kodi add-on.  There are lots of those available, but not every package in Python necessarily has a Kodi add-on since someone has to repackage it and submit it to one of the Kodi repos.  As you've found, PIL is one that has a Kodi package.

Now to add the next complication.  Your VSCode environment is accessing the Python installed on your local machine, not the Kodi one.  So it has no idea what Kodi python add-ons you might have.  So it's going to complain if you are referencing PIL in your addon but don't have PIL in your local Python environment.  I am not a VSCode expert, so hopefully someone else can give a better answer, but I think you have two choices:
  1. ignore the warnings from VSCode
  2. install PIL in your local python environment so VSCode won't complain.
#2 doesn't mean those python packages magically become available to Kodi, it just keeps VSCode from complaining.  When you test your addon in Kodi, you still have to make sure the add-on packages are installed for it to work.
Thanks @pkscout - you set my mind at ease that I didn't have completely the wrong end of the stick. 

While I was figuring out an error the error tracing pointed me to this location, which I assume is the packages that come with Kodi as standard (PIL was in 'site-packages'): C:\Program Files\Kodi\system\python\Lib\
That is where the Kodi Python interpreter is on Windows.  I'm pretty sure that's only the core Python.  Addons will be in a userdata directory, and I never remember the exact path on Windows.  but it should end in something like addons\<packagename>.
Reply
#5
Quote:Is there a way to point my VSCode environment to Kodi somehow so it knows what's available and what's not?
There is a python package named Kodistubs. It contains all xbmc modules with the definition of all functions, classes, etc, but without any real functionality.
If you install that in your local python environment you will have code completion, documentation and everything VS code could bring with a normal package.
Kodistubs is on pypi, so it's very easy to  install it with pip.
Reply
#6
Nice one thanks @kereltje !
Reply

Logout Mark Read Team Forum Stats Members Help
Super basic question on python packages, apologies in advance!0