# Cannot import module from ~/.ansible/plugins/module\_utils

**URL:** <https://forum.ansible.com/t/cannot-import-module-from-ansible-plugins-module-utils/30506>\
**Category:** Ansible Developer\
**Created:** [June 6, 2019, 6:18pm UTC](https://forum.ansible.com/t/cannot-import-module-from-ansible-plugins-module-utils/30506 "2019-06-06T18:18:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Modest\_Sokolov](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@Modest\_Sokolov](https://forum.ansible.com/u/Modest_Sokolov)\
**Post date:** [June 6, 2019, 6:18pm UTC](https://forum.ansible.com/t/cannot-import-module-from-ansible-plugins-module-utils/30506/1 "2019-06-06T18:18:30Z")

</div>

Hello,  
I develop the modules for comware5.

They run, when are in ansible standard directoriу, for my installation it is /usr/local/Cellar/ansible/2.8.0/libexec/lib/python3.7/site-packages/ansible/

I’ve tried to move them in other directories - in ~/.ansible

at running time of ansible-playbook now I’ve got error:

File “/usr/local/Cellar/ansible/2.8.0/libexec/lib/python3.7/imp.py”, line 171, in load\_source  
module = \_load(spec)  
File “”, line 696, in \_load  
File “”, line 677, in \_load\_unlocked  
File “”, line 728, in exec\_module  
File “”, line 219, in \_call\_with\_frames\_removed  
File “/Users/modest/.ansible/plugins/action/comware5\_config.py”, line 18, in   
from ansible.module\_utils.network.comware5.comware5 import comware5\_provider\_spec  
**ModuleNotFoundError: No module named ‘ansible.module\_utils.network.comware5’**

In ansible module I’ve got the string:

from ansible.module\_utils.network.comware5.comware5 import comware5\_provider\_spec

The ansible.conf file is empty.

bob@Mac:~/.ansible$ ansible-config dump| grep MODULE\_UTILS\_PATH  
DEFAULT\_MODULE\_UTILS\_PATH(default) = [‘/Users/bob/.ansible/plugins/module\_utils’, ‘/usr/share/ansible/plugins/module\_utils’]

bob@Mac:~/.ansible$ uname -a  
Darwin Mac.local 18.6.0 Darwin Kernel Version 18.6.0: Thu Apr 25 23:16:27 PDT 2019; root:xnu-4903.261.4~2/RELEASE\_X86\_64 x86\_64

bob@Mac:~/.ansible$ tree  
.  
├── cp  
├── modules  
│ └── network  
│ └── comware5  
│ ├── **init**.py  
│ ├── comware5\_command.py  
│ └── comware5\_config.py  
├── pc  
├── **plugins**  
│ ├── action  
│ │ ├── comware5.py  
│ │ ├── comware5\_config.py  
│ │ └── comware5\_template.py  
│ ├── cliconf  
│ │ └── comware5.py  
│ ├── doc\_fragments  
│ │ └── comware5.py  
│ ├── **module\_utils**  
│ │ ├── **init**.py  
│ │ ├── comware5.py  
│ │ └── network  
│ │ ├── **init**.py  
│ │ └── **comware5**  
│ │ ├── **init**.py  
│ │ └── comware5.py  
│ ├── netconf  
│ │ └── comware5.py  
│ └── terminal  
│ └── comware5.py  
└── tmp

15 directories, 15 files  
bob@Mac:~/.ansible$

Could you please give me advice, why ansible can not import module from DEFAULT\_MODULE\_UTILS\_PATH?  
Where can be my mistake?

Regards,  
Modest

---

<div class="post-metadata">

**Author:** ![Modest\_Sokolov](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@Modest\_Sokolov](https://forum.ansible.com/u/Modest_Sokolov)\
**Post date:** [June 7, 2019, 2:05pm UTC](https://forum.ansible.com/t/cannot-import-module-from-ansible-plugins-module-utils/30506/2 "2019-06-07T14:05:54Z")

</div>

bob@Mac:~/.ansible$ ansible --version  
ansible 2.8.0  
config file = /Users/bob/Work/Ansible/lab/ansible.cfg  
configured module search path = [‘/Users/bob/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]  
ansible python module location = /usr/local/Cellar/ansible/2.8.0/libexec/lib/python3.7/site-packages/ansible  
executable location = /usr/local/bin/ansible  
python version = 3.7.3 (default, Mar 27 2019, 09:23:15) [Clang 10.0.1 (clang-1001.0.46.3)]

In the pdb I’ve got:

(Pdb) sys.meta\_path  
[\<ansible.utils.collection\_loader.AnsibleCollectionLoader object at 0x11010b518\>, \<class ‘\_frozen\_importlib.BuiltinImporter’\>, \<class ‘\_frozen\_importlib.FrozenImporter’\>, \<class ‘\_frozen\_importlib\_external.PathFinder’\>, \<ansible.module\_utils.six.\_SixMetaPathImporter object at 0x10f677438\>, \<six.\_SixMetaPathImporter object at 0x11014cc50\>, \<urllib3.packages.six.\_SixMetaPathImporter object at 0x110e9c780\>]

(Pdb) p sys.path  
[‘/usr/local/Cellar/ansible/2.8.0/libexec/bin’, ‘/usr/local/Cellar/ansible/2.8.0/libexec/lib/python37.zip’, ‘/usr/local/Cellar/ansible/2.8.0/libexec/lib/python3.7’, ‘/usr/local/Cellar/ansible/2.8.0/libexec/lib/python3.7/lib-dynload’, ‘/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7’, ‘/usr/local/Cellar/ansible/2.8.0/libexec/lib/python3.7/site-packages’]

Regards,  
Modest

---

<div class="post-metadata">

**Author:** ![Modest\_Sokolov](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@Modest\_Sokolov](https://forum.ansible.com/u/Modest_Sokolov)\
**Post date:** [June 7, 2019, 3:56pm UTC](https://forum.ansible.com/t/cannot-import-module-from-ansible-plugins-module-utils/30506/3 "2019-06-07T15:56:24Z")

</div>

If in the code I change

**from ansible.module\_utils.network.comware5.comware5 import comware5\_provider\_spec**

to

**from ansible.module\_utils.network import comware5\_provider\_spec**

The ansible-playbook ends with:

File “/Users/bob/.ansible/plugins/action/comware5\_config.py”, line 18, in   
from ansible.module\_utils.network import comware5\_provider\_spec  
**ImportError** : cannot import name ‘comware5\_provider\_spec’ from ‘ansible.module\_utils.network’ ( **/usr/local/Cellar/ansible/2.8.0/libexec/lib/python3.7/site-packages/ansible/module\_utils/network/init.py** )

Does this mean, that ansible doesn’t use $DEFAULT\_MODULE\_UTILS\_PATH? that is

DEFAULT\_MODULE\_UTILS\_PATH(default) = [‘/Users/bob/.ansible/plugins/module\_utils’, ‘/usr/share/ansible/plugins/module\_utils’]

---

<div class="post-metadata">

**Author:** ![Modest\_Sokolov](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@Modest\_Sokolov](https://forum.ansible.com/u/Modest_Sokolov)\
**Post date:** [June 27, 2019, 8:41pm UTC](https://forum.ansible.com/t/cannot-import-module-from-ansible-plugins-module-utils/30506/4 "2019-06-27T20:41:01Z")

</div>

Hello.

I was able to solve problem.

It seems, **the rule** from documentation "The `ansible.module_utils` namespace is not a plain Python package: it is constructed dynamically for each task invocation, by extracting imports and resolving those matching the namespace against a [search path](https://docs.ansible.com/ansible/latest/dev_guide/overview_architecture.html#ansible-search-path) derived from the active configuration. " **is not working for action plugins.**

It will be very useful, if information about this point was in ansible documentation too.

Regards,  
Modest Sokolov

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [June 27, 2019, 10:20pm UTC](https://forum.ansible.com/t/cannot-import-module-from-ansible-plugins-module-utils/30506/5 "2019-06-27T22:20:49Z")

</div>

Action plugins can reference ansible.module\_utils but only for ones in the base Ansible package. Module can take advantage of the lookup path as it is built for you automatically and sent across to the remote host.

Feel free to raise a PR to the Ansible docs at [https://github.com/ansible/ansible/tree/devel/docs/docsite/rst](https://github.com/ansible/ansible/tree/devel/docs/docsite/rst) if you want to clarify it further [https://github.com/ansible/ansible/tree/devel/docs/docsite/rst](https://github.com/ansible/ansible/tree/devel/docs/docsite/rst).

Thanks

Jordan

---

<div class="post-metadata">

**Author:** ![Modest\_Sokolov](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@Modest\_Sokolov](https://forum.ansible.com/u/Modest_Sokolov)\
**Post date:** [June 28, 2019, 8:14am UTC](https://forum.ansible.com/t/cannot-import-module-from-ansible-plugins-module-utils/30506/6 "2019-06-28T08:14:58Z")

</div>

Jordan, thanks a lot.

[https://github.com/ansible/ansible/issues/58490](https://github.com/ansible/ansible/issues/58490)

пт, 28 июн. 2019 г. в 01:20, Jordan Borean \<[jborean93@gmail.com](mailto:jborean93@gmail.com)\>:
