# about having top-level scope for main() call in modules

**URL:** <https://forum.ansible.com/t/about-having-top-level-scope-for-main-call-in-modules/16251>\
**Category:** Ansible Developer\
**Created:** [September 16, 2014, 10:36pm UTC](https://forum.ansible.com/t/about-having-top-level-scope-for-main-call-in-modules/16251 "2014-09-16T22:36:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhishek\_Kumar](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@Abhishek\_Kumar](https://forum.ansible.com/u/Abhishek_Kumar)\
**Post date:** [September 16, 2014, 10:36pm UTC](https://forum.ansible.com/t/about-having-top-level-scope-for-main-call-in-modules/16251/1 "2014-09-16T22:36:01Z")

</div>

Hi,

What if the call to main() in python-based core modules gets changed to just

```auto
if __name__ == " __main__":
    main()

```

it will import all required modules, and still not call main() if imported.

Opinions?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 24, 2014, 2:31am UTC](https://forum.ansible.com/t/about-having-top-level-scope-for-main-call-in-modules/16251/2 "2014-09-24T02:31:56Z")

</div>

I’m ok with that main() change being made more or less wholesale, yes.

The patch must be very careful to make it everywhere, and not do anything else.

---

<div class="post-metadata">

**Author:** ![Abhishek\_Kumar](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@Abhishek\_Kumar](https://forum.ansible.com/u/Abhishek_Kumar)\
**Post date:** [September 24, 2014, 3:17pm UTC](https://forum.ansible.com/t/about-having-top-level-scope-for-main-call-in-modules/16251/3 "2014-09-24T15:17:23Z")

</div>

Hi Micahel,

Thanks for that.  
I've submitted a PR as [defining top-level env 'just' for main(), import extensible modules from new lib by abhishekkr · Pull Request #9120 · ansible/ansible · GitHub](https://github.com/ansible/ansible/pull/9120)  
and the shortened diff is attached here as well just showing it's just  
meddling with main() call.

Also, I had a related query that I've commented here... (for when you have time)

> <https://github.com/ansible/ansible/pull/9017#issuecomment-56685439>
>
> @jimi-c hey, thanks for the info... I noticed the temporary python files pushed …to remotes machines being appended core module logic and pieces it depends upon (I suppose).
> Are you mentioning the same behavior being done by module templating?
> Can you please guide me to section of that module templating logic, I'd like to have a look at it please.
> 
> Also had a query, if my one custom module imports some other custom python libraries (or a custom module)... is the templating engine currently capable of tracing those as well and appending them? Is there a specific convention around it, like right before calling main or so?

> **(attachments)**
>
> [file.diff.log](https://forum.ansible.com/uploads/short-url/5niKjv1HYX6JbTiUBPWobWcI33Z.log) (25.6 KB)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 24, 2014, 4:10pm UTC](https://forum.ansible.com/t/about-having-top-level-scope-for-main-call-in-modules/16251/4 "2014-09-24T16:10:37Z")

</div>

The one thing that doesn’t really make sense though is “modules plainly extensible via import”… this isn’t actually true, due to the way variables coming into the module are processed - which is why this wasn’t done this way originally.

Curious on background if you care to elaborate?
