# Setting PYTHONPATH during playbook execution

**URL:** <https://forum.ansible.com/t/setting-pythonpath-during-playbook-execution/25513>\
**Category:** AWX Project\
**Created:** [September 18, 2017, 5:36pm UTC](https://forum.ansible.com/t/setting-pythonpath-during-playbook-execution/25513 "2017-09-18T17:36:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cody\_John](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@Cody\_John](https://forum.ansible.com/u/Cody_John)\
**Post date:** [September 18, 2017, 5:36pm UTC](https://forum.ansible.com/t/setting-pythonpath-during-playbook-execution/25513/1 "2017-09-18T17:36:35Z")

</div>

I have some lookup plugins that import some custom python modules, and I’m gettting python errors during execution because it can’t find them. I need a way to set the PYTHONPATH environment variable prior to playbook execution, or somehow achieve the same result. Any ideas?

---

<div class="post-metadata">

**Author:** ![matburt](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/matburt/32/3612_2.png) [@matburt](https://forum.ansible.com/u/matburt)\
**Post date:** [September 18, 2017, 5:43pm UTC](https://forum.ansible.com/t/setting-pythonpath-during-playbook-execution/25513/2 "2017-09-18T17:43:46Z")

</div>

This is going to be super tricky because we also isolate the process using bubblewrap, so your chance of gaining access to another interpreter is pretty small unless you disable that. This is generally not something we’ve supported in any way in the past.

If you need to run some lookup plugins you can put them alongside your playbooks and install what dependencies you need in the /var/lib/awx/venv/awx virtualenv.

---

<div class="post-metadata">

**Author:** ![Cody\_John](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@Cody\_John](https://forum.ansible.com/u/Cody_John)\
**Post date:** [September 18, 2017, 8:34pm UTC](https://forum.ansible.com/t/setting-pythonpath-during-playbook-execution/25513/3 "2017-09-18T20:34:28Z")

</div>

Hmm, that doesn’t really work in this case because we manage our custom python code in RPMs using yum, not pip, so it’s not possible to install them in the virtualenv.

I noticed under Settings → Jobs, there is a field named “EXTRA ENVIRONMENT VARIABLES” but it’s disabled because it says its managed in a config file somewhere. Would adding PYTHONPATH there help in this case?

---

<div class="post-metadata">

**Author:** ![matburt](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/matburt/32/3612_2.png) [@matburt](https://forum.ansible.com/u/matburt)\
**Post date:** [September 18, 2017, 9:00pm UTC](https://forum.ansible.com/t/setting-pythonpath-during-playbook-execution/25513/4 "2017-09-18T21:00:46Z")

</div>

maybe, you can find the settings files in the container. You are definitely on your own for that one though… I wouldn’t expect it to work very well at all 🙂

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [September 20, 2017, 6:43pm UTC](https://forum.ansible.com/t/setting-pythonpath-during-playbook-execution/25513/5 "2017-09-20T18:43:17Z")

</div>

So … here’s what I’ve done:

1. Modified installer/local\_docker/tasks/main.yml to include an optional set of volumes.
2. Created installer/group\_vars/all to specify my custom settings.

I’m then taking advantage of [site .pth](https://docs.python.org/2/library/site.html) files to point python at the right set of libs.

This should allow someone to install python packages on a host machine that can be used by the AWX container.

Custom vars:

> $ cat installer/group\_vars/all.yml
> 
> * * *
> 
> custom\_ansible\_dir: “/opt/ansible”
> 
> task\_volumes:
> 
> - “/opt/pypath.pth:/usr/lib/python2.7/site-packages/pypath.pth”
> 
> - “/opt/custom\_pylib:/opt/custom\_pylib”

pth file:

> $ cat /opt/pypath.pth
> 
> /opt/custom\_pylib

Custom tasks:
