# Execute python module from ansible?

**URL:** <https://forum.ansible.com/t/execute-python-module-from-ansible/35315>\
**Category:** Ansible Project\
**Created:** [June 8, 2021, 9:06pm UTC](https://forum.ansible.com/t/execute-python-module-from-ansible/35315 "2021-06-08T21:06:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Menke](https://avatars.discourse-cdn.com/v4/letter/j/8491ac/32.png) [@John\_Menke](https://forum.ansible.com/u/John_Menke)\
**Post date:** [June 8, 2021, 9:06pm UTC](https://forum.ansible.com/t/execute-python-module-from-ansible/35315/1 "2021-06-08T21:06:12Z")

</div>

in python i can do  
python -m packagename.modulename

I am trying this from ansible script and its not finding python

---

<div class="post-metadata">

**Author:** ![Robert\_Egloff](https://avatars.discourse-cdn.com/v4/letter/r/e274bd/32.png) [@Robert\_Egloff](https://forum.ansible.com/u/Robert_Egloff)\
**Post date:** [June 9, 2021, 4:15am UTC](https://forum.ansible.com/t/execute-python-module-from-ansible/35315/2 "2021-06-09T04:15:38Z")

</div>

Are you using the shell module to send the command line? Perhaps try using the full path to the python interpreter you want to use.  
Often ‘/usr/bin/python’ is a link to /usr/bin/python2 or /usr/bin/python3

There may be a more elegant way as well, just a thought though. I’m a bit curious also, I haven’t had time, but I have had a couple use case ideas using some python scripts in Ansible.

I did find this post, where it’s mentioned to ‘cd’ first, but personally - I’d just use the full path to python.

**/usr/bin/python -m packagename.modulename** (If it complains there’s no ‘python’ use /usr/bin/python2 or /usr/bin/python3 or create a symlink from /usr/bin/python to your chosen version)

---

<div class="post-metadata">

**Author:** ![Robert\_Egloff](https://avatars.discourse-cdn.com/v4/letter/r/e274bd/32.png) [@Robert\_Egloff](https://forum.ansible.com/u/Robert_Egloff)\
**Post date:** [June 9, 2021, 4:16am UTC](https://forum.ansible.com/t/execute-python-module-from-ansible/35315/3 "2021-06-09T04:16:24Z")

</div>

Ehh - this post - I meant to link above 🙂

[https://serverfault.com/questions/769898/running-python-script-on-anisble](https://serverfault.com/questions/769898/running-python-script-on-anisble)
