Posts: 73
Joined: Aug 2016
Reputation:
0
Hello,
I thought it may be a good idea to group out some code into small modules. So I created a "resources/lib" directory, added it to "sys.path" and now import the small .py modules.
But for every import a new ".pyo" file is created every time Kodi starts.
I've disabled this using "sys.dont_write_bytecode", but why are they created? Why are they created every time? Is it recommended to use "sub-modules" at all and if so, what are the recommendations when doing so?
Posts: 17,859
Joined: Jul 2011
Reputation:
371
why would you need to compile them each time over and over again? If nothing changes it's faster to already have the .py files compiled
Posts: 73
Joined: Aug 2016
Reputation:
0
You are right. They are, in fact, only created once.
But what would I do if this Addon is meant to be installed globally? Meaning that the user itself can't write the "pyo" files.
Is there some "build command" to create the files in advance or is it OK if the files just can't be written?
Posts: 1,660
Joined: Jun 2011
Reputation:
119
This is how a Python interpreter works. Source code is compiled into bytecode which, in its turn, executed by the Python interpreter. Normally, bytecode for imported modules is saved for future use and the next time imports modules from bytecode files unless the source files have been changed. Only then the changed files are re-compiled.
From Kodi addon perspective, it's better to divide your code into logical parts and put those parts into separate modules, leaving in the main script only some minimal code needed to launch your addon. Such approach not only makes your code more maintainable but also speeds up addon start on slower systems, like Pi.
Posts: 73
Joined: Aug 2016
Reputation:
0
Thanks for this information. I'll keep some basic code in the main file but try to separate logical parts into new modules.
But what about the "pyo" files if the Addon is installed system-global? Should I pre-create them to install them to the same system-global place or is it better to not do that? If this is recommeded: How to directly convert ".py" to ".pyo" in, for example, a Makefile?
Posts: 17,859
Joined: Jul 2011
Reputation:
371
What do mean with "global"?
Posts: 1,660
Joined: Jun 2011
Reputation:
119
Normally, you don't have to deal with bytecode files because they are maintained automatically by a Python interpreter. If you don't know what they are for, then you can safely forget about them.
Posts: 73
Joined: Aug 2016
Reputation:
0
Every architecture may be possible so I keep this to the distributor. That's why I want to install to /usr/share/kodi/addons.
But I still don't know if I have to handle the pyo files in some way if the .py files are placed at a location not writeable by the user. Or is it not supported to install Python addons "system-wide" at all? At least for my Addon this seems to work well...
Posts: 15,261
Joined: Jan 2009
Reputation:
726
ronie
Team-Kodi Member
Posts: 15,261
hmm.... interesting question.
addons that ship with kodi (service.xbmc.versioncheck for instance) are installed globally.
bytecode for those modules can't be written to disk, as that would require root privileges.