MycroftAI / mycroft-core

Mycroft Core, the Mycroft Artificial Intelligence platform.
https://mycroft.ai
Apache License 2.0
6.53k stars 1.27k forks source link

Importing python modules fail on skill load #2493

Closed DAMO238 closed 4 years ago

DAMO238 commented 4 years ago

Be clear about the software, hardware and version you are running

Running on Arch Linux, latest version.

Try to provide steps that we can use to replicate the Issue

  1. Create skill with mycroft-msk
  2. Change to fallback skill
  3. Create other python files in the same directory as the skill
  4. Import those files in init.py
  5. Restart mycroft with cli and note that python cannot find the module you made.
  6. Also note that any dependencies put in manifest.yaml are not automatically installed, and must be manually installed by entering the virtual environment and installing via pip there.

Sorry if problems with custom skills are not supposed to go here. If they are supposed to go elsewhere, please let me know and I will move this issue asap.

forslund commented 4 years ago

How do you import them?

To import skill files from the skills directory you need to do a relative import

from . import submodule

or

from .submodule import thing

See the spotify skill for an example.

DAMO238 commented 4 years ago

I was simply importing them, causing that issue, but now I get a new error that is 'most likely due to circular import', even though, in my non mycroft tests, the script worked just fine.

After some more testing, mycroft seems to have another seemingly unrelated error:

Traceback (most recent call last):
  File "/home/damien/mycroft-core/.venv/lib/python3.8/site-packages/msm/util.py", line 102, in __get__
    value, last_update = inst._cache[self.__name__]
AttributeError: 'MycroftSkillsManager' object has no attribute '_cache'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.8/runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.8/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/damien/mycroft-core/mycroft/skills/__main__.py", line 276, in <module>
    main()
  File "/home/damien/mycroft-core/mycroft/skills/__main__.py", line 188, in main
    skill_manager = _initialize_skill_manager(bus)
  File "/home/damien/mycroft-core/mycroft/skills/__main__.py", line 244, in _initialize_skill_manager
    skill_manager = SkillManager(bus)
  File "/home/damien/mycroft-core/mycroft/skills/skill_manager.py", line 102, in __init__
    self.skill_updater = SkillUpdater()
  File "/home/damien/mycroft-core/mycroft/skills/skill_updater.py", line 51, in __init__
    self.dot_msm_path = os.path.join(self.msm.skills_dir, '.msm')
  File "/home/damien/mycroft-core/mycroft/skills/skill_updater.py", line 104, in msm
    self._msm = create_msm(msm_config)
  File "/home/damien/mycroft-core/mycroft/skills/msm_wrapper.py", line 100, in create_msm
    msm_instance = MycroftSkillsManager(
  File "/home/damien/mycroft-core/.venv/lib/python3.8/site-packages/msm/mycroft_skills_manager.py", line 110, in __init__
    self._init_skills_data()
  File "/home/damien/mycroft-core/.venv/lib/python3.8/site-packages/msm/mycroft_skills_manager.py", line 245, in _init_skills_data
    del(self.device_skill_state['upgraded'])
  File "/home/damien/mycroft-core/.venv/lib/python3.8/site-packages/msm/mycroft_skills_manager.py", line 262, in device_skill_state
    self._sync_device_skill_state()
  File "/home/damien/mycroft-core/.venv/lib/python3.8/site-packages/msm/mycroft_skills_manager.py", line 306, in _sync_device_skill_state
    self._add_skills_to_state()
  File "/home/damien/mycroft-core/.venv/lib/python3.8/site-packages/msm/mycroft_skills_manager.py", line 313, in _add_skills_to_state
    for skill in self.local_skills.values():
  File "/home/damien/mycroft-core/.venv/lib/python3.8/site-packages/msm/mycroft_skills_manager.py", line 201, in local_skills
    s.name: s for s in self.all_skills if s.is_local
  File "/home/damien/mycroft-core/.venv/lib/python3.8/site-packages/msm/util.py", line 106, in __get__
    value = self.fget(inst)
  File "/home/damien/mycroft-core/.venv/lib/python3.8/site-packages/msm/mycroft_skills_manager.py", line 133, in all_skills
    self._all_skills = self._get_all_skills()
  File "/home/damien/mycroft-core/.venv/lib/python3.8/site-packages/msm/mycroft_skills_manager.py", line 141, in _get_all_skills
    all_skills = self._merge_remote_with_local(remote_skills)
  File "/home/damien/mycroft-core/.venv/lib/python3.8/site-packages/msm/mycroft_skills_manager.py", line 187, in _merge_remote_with_local
    skill = SkillEntry.from_folder(path.dirname(skill_file), msm=self,
  File "/home/damien/mycroft-core/.venv/lib/python3.8/site-packages/msm/skill_entry.py", line 212, in from_folder
    return cls(None, path, cls.find_git_url(path), msm=msm)
  File "/home/damien/mycroft-core/.venv/lib/python3.8/site-packages/msm/skill_entry.py", line 133, in __init__
    from_github = (url.rstrip("/").split("/")[-3] == 'github.com') if url else False
IndexError: list index out of range
forslund commented 4 years ago

The unrelated error is an issue with msm that I think has been fixed, try running ./bin/mycroft-pip install msm==0.8.7 from the mycroft-core folder to get the latest version.

DAMO238 commented 4 years ago

I fixed that now, but now I got the circular import error again:

Traceback (most recent call last):
  File "/home/damien/mycroft-core/mycroft/skills/skill_loader.py", line 204, in _load_skill_source
    skill_module = imp.load_module(
  File "/usr/lib/python3.8/imp.py", line 234, in load_module
    return load_source(name, filename, file)
  File "/usr/lib/python3.8/imp.py", line 171, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 702, in _load
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/opt/mycroft/skills/sanjeev-skill/__init__.py", line 3, in <module>
    from . import sanjeev_pb2, sanjeev_pb2_grpc
~~~~njeev_pb2_grpc' from partially initialized module 'sanjeev-skill' (most likely due to a circular import) (/opt/mycroft/skills/sanjeev-skill/__init__.py)

The source code is at: https://github.com/DAMO238/sanjeev-skill

forslund commented 4 years ago

It's hard to say since the files are symbolic links and don't have an ycontent.

I'll see if I can whip up an example during the weekend

DAMO238 commented 4 years ago

Ah, I finally found the problem... I had a typo which wasn't obvious grpc -> gprc. Thanks for all your help and for putting up with my shenanigans!

forslund commented 4 years ago

Glad you found the problem. Good luck with your skill