# Importing Python Custom Modules for Ansible to run?

**URL:** <https://forum.ansible.com/t/importing-python-custom-modules-for-ansible-to-run/22271>\
**Category:** Ansible Project\
**Created:** [August 11, 2016, 7:26am UTC](https://forum.ansible.com/t/importing-python-custom-modules-for-ansible-to-run/22271 "2016-08-11T07:26:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SC1](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@SC1](https://forum.ansible.com/u/SC1)\
**Post date:** [August 11, 2016, 7:26am UTC](https://forum.ansible.com/t/importing-python-custom-modules-for-ansible-to-run/22271/1 "2016-08-11T07:26:31Z")

</div>

Probably a silly question, but is it possible to refer to another Python file with the script module?

For example, if I have abc.py and 123.py, if I import 123.py into the abc.py python script (if I need some functions or whatever), both of these files are not copied when I call the script module for abc.py in the playbook.

Obviously, I didn’t think ansible would check to make sure all the imports are valid inside a Python script, but how can I get it to work otherwise? Documentation ([http://docs.ansible.com/ansible/script\_module.html](http://docs.ansible.com/ansible/script_module.html)) doesn’t have any examples of this and I tried looking up: ‘ansible python import ansible’ and other keywords with no real relevant results.

The only way I can think of doing this is copying both files over, then running the script, then deleting the copied files after run?

Is there a way to copy 123.py to a default location, wherever the script module uses to copy abc.py so when ansible-playbook tries to run abc.py, 123.py will already be there and import correctly. Afterwards, I can have the playbook delete 123.py to clean itself up.

The other way to do it is to just copy the functions/stuff you need from 123.py to abc.py but I was looking for a modular way to do this if possible. Any thoughts are appreciated, thank you!

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 11, 2016, 8:03am UTC](https://forum.ansible.com/t/importing-python-custom-modules-for-ansible-to-run/22271/2 "2016-08-11T08:03:01Z")

</div>

> Probably a silly question, but is it possible to refer to another Python  
> file with the script module?

No, it has limited functionality.

> For example, if I have abc.py and 123.py, if I import 123.py into the  
> abc.py python script (if I need some functions or whatever), both of these  
> files are not copied when I call the script module for abc.py in the  
> playbook.
> 
> Obviously, I didn't think ansible would check to make sure all the imports  
> are valid inside a Python script, but how can I get it to work otherwise?  
> Documentation ([http://docs.ansible.com/ansible/script\_module.html](http://docs.ansible.com/ansible/script_module.html)) doesn't  
> have any examples of this and I tried looking up: 'ansible python import  
> ansible' and other keywords with no real relevant results.
> 
> The only way I can think of doing this is copying both files over, then  
> running the script, then deleting the copied files after run?

You also has one other possibility, the documentation for the Script module also say this[1]:  
"It is usually preferable to write Ansible modules than pushing scripts. Convert your script to an Ansible module for bonus points!"

[1] [https://docs.ansible.com/ansible/script\_module.html#notes](https://docs.ansible.com/ansible/script_module.html#notes)
