-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Switch from the deprecated imp to importlib #2327
Conversation
If we are going to do this, I want it to include the ability to load the code from a file other than |
Isn't that already possible? The only thing that's needed is that |
I tried moving the skill class to a file called I can test against the code in this PR and see if there is different behavior. |
Ah, right imported submodules might not be reloaded...let me do some tests here |
Well the spotify skill has the majority of the spotify logic in a separate file so it has worked before at least... let me try with your case with the entire skill in a skill.py file... |
I would be happy if we just didn't hardcode the |
Do you mean like search all files for Looking at a skill as a module it would require the |
using |
OOPS. Hit the close button accidentally. |
Gotcha. In any case you're query highlighted an issue here. with this implementation relative imports no longer works in skills so I guess I need to look into that at least. Will look at loading the skill from any file while I'm at it. |
relative imports haven't worked ever, i remember in the old times the usual suggestion in slack was to add the skill directory to the python path, i usually started from MYCROFT_ROOT_PATH (remember when skill where part of core?) but i remember seeing all sorts of ugly stuff with os.path and some approaches with skill variables |
In case there was any question, manipulating the Python path should be avoided at all costs. |
I'm using relative imports in a skill I'm developing right now, and it seems to be working. Aside from this PR, is that going to break later? There seem to be conflicting experiences here. |
With the current There is an issue with the relative imports not reloading when the |
Pushed a WIP update supporting relative imports again and adding reloading for them as well... |
Separate python 3.4 and 3.5+ implementation
902de98
to
a8b1888
Compare
This has been updated to use importlib and a nice and easy fix to allow skill submodules to be reloaded. |
Voight Kampff Integration Test Succeeded (Results) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Working great!
One tiny wording nitpick (probably just a personal preference thing) and one clarification.
mycroft/skills/skill_loader.py
Outdated
@@ -29,6 +34,49 @@ | |||
SKILL_MAIN_MODULE = '__init__.py' | |||
|
|||
|
|||
def remove_submodule_refs(module_name): | |||
"""Ensure submodules being reloaded by removing the refs from sys.modules. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#nitpick...
Ensure submodule is reloaded by removing the refs from sys.modules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you ok with
Ensure submodules are reloaded by removing the refs from sys.modules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep that reads great.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment updated
mycroft/skills/skill_loader.py
Outdated
module_name: name of skill module. | ||
""" | ||
submodules = [] | ||
LOG.info('Skill module'.format(module_name)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this intended to be an info level log message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right that should be a debug level, will fix on my train ride home
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to debug
a8b1888
to
3e5e2b4
Compare
Voight Kampff Integration Test Succeeded (Results) |
Submodule reload is enforced by removing sys.modules reference. This will make imports do the full uncached importing
3e5e2b4
to
58608e6
Compare
Voight Kampff Integration Test Failed (Results) |
1 similar comment
Voight Kampff Integration Test Failed (Results) |
Voight Kampff Integration Test Succeeded (Results) |
Description
Replaces the current imp based skill loader with an importlib based solution. This also adds a small change allowing better use of submodules. Before submodules were never reloaded forcing the over-use of the
__init__.py
file in skills. The change makes the submodules reload together with the main module by removing the refs fromsys.modules
, allowing better structure of skills.How to test
Make sure skills load. Make a change to a skill and ensure that the skill is reloaded and that the change is accessible.
Add a log statement to the Spotify skill's spotify.py and check that it's included when the skill reloads.
Contributor license agreement signed?
CLA [ Yes ]