Custom Module with Custom Python Package - Module not found error

Viewed 35

I wrote a custom python package for Ansible to handle business logic for some servers I manage. I have multiple files and they reference each other by re-importing the package.

So my package named <MyCustomPackage> has functions <Function1> <Function2> <Function3>, etc all in their own files... Some of these functions reference functions in the same package, so to do that the file has:

import MyCustomPackage

at the top. I did it this way instead of a relative import because I'm also unit testing these and mocking would not work with relative paths because of a __init__ file in the test directory which was needed for test discovery. The only way I could mock was through importing the package itself. Seemed simple enough.

The problem is with Ansible. These packages are in module_utils. I import them with:

from ansible.module_utils.MyCustomPackage import MyCustomPackage

but when I use the commands I get module not found errors - and traced it back to the import MyCustomPackage statement in the package itself.

So - how should I be structuring my package? Should I try again with relative file imports, or have the package modify the path so it's found with the friendly name?

Any tips would be helpful! Or if someone has a module they've written with Python modules in module_utils and unit tests that they'd be willing to share, that'd be great also!

1 Answers

Many people have problems with relative imports and imports in general in Python because they are ambiguous and surprisingly depend on your current working directory (and other things).

Thus I've created an experimental, new import library: ultraimport

It gives you more control over your imports and lets you do file system based, relative imports.

Given that you have a file function1.py, to import a function from function2.py, you would then write:

import ultraimport
Function2 = ultraimport('__dir__/function2.py', 'Function2')

This will always work, no matter how you run your code. It also does not force you to a specific package structure. You can just have any files you like.

Related