When developing a role that has a local module with some code split into module_utils, how do you set up your pythonpath or venv so that pylint and unit testing resolves the imports correctly?

For example: https://github.com/linux-system-roles/network/

The module code is in https://github.com/linux-system-roles/network/blob/master/library/network_connections.py

The module_utils code is in https://github.com/linux-system-roles/network/tree/master/module_utils/network_lsr

The module code imports the module_utils code like this:

# pylint: disable=import-error, no-name-in-module
from ansible.module_utils.network_lsr import MyError

As you can see, we have to disable pylint checking, because there is no ansible.module_utils.network_lsr - there is a module_utils.network_lsr however. We have to do something similar in the unit test code: https://github.com/linux-system-roles/network/blob/master/tests/unit/test_nm_provider.py

sys.modules["ansible"] = mock.Mock()
sys.modules["ansible.module_utils.basic"] = mock.Mock()
sys.modules["ansible.module_utils"] = mock.Mock()
sys.modules["ansible.module_utils.network_lsr"] = __import__("network_lsr")

This seems pretty hackish, and I'm hoping there is a better or standard way to do this.

--
You received this message because you are subscribed to the Google Groups "Ansible 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-devel/d5f0c3ad-f7e5-09de-b5b4-3e9c741cd567%40redhat.com.

Reply via email to