This site is in read only mode. Please continue to browse, but replying, likes, and other actions are disabled for now.

⚠️ We've moved!

Hi there!

To reduce project dependency on 3rd party paid services the StackStorm TSC has decided to move the Q/A from this forum to Github Discussions. This will make user experience better integrated with the native Github flow, as well as the questions closer to the community where they can provide answers.

Use 🔗 Github Discussions to ask your questions.

ModuleNotFoundError: No module named 'lib'

I’ve been facing a ModuleNotFoundError: No module named ‘lib’ error from st2 while attempting to write my custom action.

The setup is as follows:

actions
| lib
| | __init__.py
| | alertmanager.py
| pythonactions
| | __init__.py
| | disk.py
| disk.yaml
  • With this directory structure, I am developing locally using st2-docker having mounted my custom pack inside the container for testing.
  • I have run st2ctl reload countless times to ensure that the code its picking up is correct.

My disk.py file imports a class from the alertmanager.py file and I import it as from lib.alertmanager import AlertmanagerAlert within disk.py.

This particular import is what raises the ModuleNotFoundError .

I have read the following:

  1. Packs — StackStorm 3.3.0 documentation
  2. Actions — StackStorm 3.3.0 documentation
    to ensure that I’m not missing something untoward, but the only related page I can find similar to my issue is Sharing code between Sensors and Python Actions — StackStorm 3.3.0 documentation which talks about sharing a lib between actions and sensors.

My code fails to even import the class internally within another action.

Some logs for reference:

File \"/opt/stackstorm/st2/lib/python3.6/site-packages/python_runner/python_action_wrapper.py\", line 238, in _get_action_instance
    actions_cls = action_loader.register_plugin(Action, self._file_path)
  File \"/opt/stackstorm/st2/lib/python3.6/site-packages/st2common/util/loader.py\", line 168, in register_plugin
    module = imp.load_source(module_name, plugin_abs_file_path)
  File \"/opt/stackstorm/virtualenvs/newpack/lib/python3.6/imp.py\", line 172, in load_source
    module = _load(spec)
  File \"<frozen importlib._bootstrap>\", line 684, in _load
  File \"<frozen importlib._bootstrap>\", line 665, in _load_unlocked
  File \"<frozen importlib._bootstrap_external>\", line 678, in exec_module
  File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed
  File \"/opt/stackstorm/packs.dev/newpack/actions/pythonactions/disk_handler.py\", line 6, in <module>
    from lib.alertmanager import AlertmanagerAlert
ModuleNotFoundError: No module named 'lib'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File \"/opt/stackstorm/st2/lib/python3.6/site-packages/python_runner/python_action_wrapper.py\", line 334, in <module>
    obj.run()
  File \"/opt/stackstorm/st2/lib/python3.6/site-packages/python_runner/python_action_wrapper.py\", line 192, in run
    action = self._get_action_instance()
  File \"/opt/stackstorm/st2/lib/python3.6/site-packages/python_runner/python_action_wrapper.py\", line 245, in _get_action_instance
    raise exc_cls(msg)
ModuleNotFoundError: Failed to load action class from file \"/opt/stackstorm/packs.dev/newpack/actions/pythonactions/disk_handler.py\" (action file most likely doesn't exist or contains invalid syntax): No module named 'lib'

Traceback (most recent call last):
  File \"/opt/stackstorm/st2/lib/python3.6/site-packages/python_runner/python_action_wrapper.py\", line 238, in _get_action_instance
    actions_cls = action_loader.register_plugin(Action, self._file_path)
  File \"/opt/stackstorm/st2/lib/python3.6/site-packages/st2common/util/loader.py\", line 168, in register_plugin
    module = imp.load_source(module_name, plugin_abs_file_path)
  File \"/opt/stackstorm/virtualenvs/newpack/lib/python3.6/imp.py\", line 172, in load_source
    module = _load(spec)
  File \"<frozen importlib._bootstrap>\", line 684, in _load
  File \"<frozen importlib._bootstrap>\", line 665, in _load_unlocked
  File \"<frozen importlib._bootstrap_external>\", line 678, in exec_module
  File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed
  File \"/opt/stackstorm/packs.dev/newpack/actions/pythonactions/disk_handler.py\", line 6, in <module>
    from lib.alertmanager import AlertmanagerAlert
ModuleNotFoundError: No module named 'lib'

Could it be related to Fix implicit relative imports in pack actions by blag · Pull Request #5127 · StackStorm/st2 · GitHub @blag @amanda11 ?

Updating as a possible fix for the immediate cause of the module/action not being able to run:

  1. Move the entrypoint of the action (as defined in disk.yaml) up 1 level to the actions/ dir.
  2. Refer to the actions/lib code in the actions/disk.py file in a relative manner: from .lib.alertmanager import AlertmanagerAlert.

This however means that the entrypoints in places other than actions/ are effectively unsupported. I’ll leave this open right now to get some clarity on the vision of supporting actions/<sub-dir> as entrypoints for python runners.

TLDR: Don’t use any dir apart from actions/ for your custom actions.

What’s the StackStorm version you’re running?

root@4d5087886f76:/opt/stackstorm# st2 --version
st2 3.4dev (71e0cdf41), on Python 3.6.9

Running st2:3.4dev.

Yeah, right. Most probably related to Packs that have actions in sub-directories importing modules in same sub-directory fail · Issue #5126 · StackStorm/st2 · GitHub and Fix implicit relative imports in pack actions by blag · Pull Request #5127 · StackStorm/st2 · GitHub