Do import statements need to be at the bottom in modules?

I’ve noticed that all of the Ansible modules have their import statements near the bottom of the file instead of the top:

from ansible.module_utils.basic import *

If I’m writing a new module, do I need to put the import near the bottom? Or is it safe to put it near the top like in a conventional Python script?

Lorin

Hi Lorin,

The imports are at the bottom because behind the scenes they are really doing code-insertion so Ansible can ship things without installing other files to the remotes.

More specifically, putting them at the bottom - because of this - does not upset the line numbers when the code is inserted, so any tracebacks have accurate, rather than shifted, line numbers, should they occur in the module.