# Ansible in virtualenv mixing python with os

**URL:** <https://forum.ansible.com/t/ansible-in-virtualenv-mixing-python-with-os/31323>\
**Category:** Ansible Project\
**Tags:** azure\
**Created:** [September 20, 2019, 10:35am UTC](https://forum.ansible.com/t/ansible-in-virtualenv-mixing-python-with-os/31323 "2019-09-20T10:35:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mr\_Decisive](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@Mr\_Decisive](https://forum.ansible.com/u/Mr_Decisive)\
**Post date:** [September 20, 2019, 10:35am UTC](https://forum.ansible.com/t/ansible-in-virtualenv-mixing-python-with-os/31323/1 "2019-09-20T10:35:34Z")

</div>

Hi,

I have this error when running a playbook to connect to azure to update virtual machines.

“Failed to import the required Python library (packaging) on Python /usr/bin/python. Please read module documentation and install in the appropriate location”

I can see it is using /usr/bin/python but I’m running ansible in a virtualenv with python3 and the python path is /home//py3/bin/python. I know the packaging library is installed in the virtualenv.

So my question is: Why is ansible trying to use the os python and how do I make it use the virtualenv version?

Cheers  
Phill

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 20, 2019, 11:43am UTC](https://forum.ansible.com/t/ansible-in-virtualenv-mixing-python-with-os/31323/2 "2019-09-20T11:43:37Z")

</div>

> Hi,
> 
> I have this error when running a playbook to connect to azure to update virtual machines.
> 
> "Failed to import the required Python library (packaging) on \<compname\> Python /usr/bin/python. Please read module  
> documentation and install in the appropriate location"
> 
> I can see it is using /usr/bin/python but I'm running ansible in a virtualenv with python3 and the python path is  
> /home/\<me\>/py3/bin/python. I know the packaging library is installed in the virtualenv.
> 
> So my question is: Why is ansible trying to use the os python and how do I make it use the virtualenv version?
> 
> Cheers  
> Phill

Hello Phill,

it makes total sense to use the Python interpreter installed by the OS by default. If you want to use a different  
interpreter on the target, you can set it via the ansible\_python\_interpreter variable (playbook, commandline, host, ...).

Something like that should do the trick:

- hosts: azure

&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;ansible\_python\_interpreter: /home/\<me\>/py3/bin/python

&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- vm-updates

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Mr\_Decisive](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@Mr\_Decisive](https://forum.ansible.com/u/Mr_Decisive)\
**Post date:** [September 20, 2019, 2:28pm UTC](https://forum.ansible.com/t/ansible-in-virtualenv-mixing-python-with-os/31323/3 "2019-09-20T14:28:08Z")

</div>

Hi Stefan,

Thanks for the info.

I’d tried putting

ansible\_python\_interpreter: /home//py3/bin/python

in the ansible.cfg file in /etc/ansible/ but whilst it read the file as I could tell from the -vvv switch I was still seeing parts of the run falling back to /usr/bin/python??

Anyway I subsequently (prior to you replying) installed the missing packages in the system os python version (cheating I know bu I wanted to make sure it really was a problem with the path and this was the easiest way to test it). It worked. but this didn’t fix the original problem. Sooooo I saw your response and put the variable definition in the playbook and this worked. I could see via the -vvv switch that all the python paths were correct.

Thanks for your help.

Cheers  
Phill

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 20, 2019, 2:45pm UTC](https://forum.ansible.com/t/ansible-in-virtualenv-mixing-python-with-os/31323/4 "2019-09-20T14:45:31Z")

</div>

> Hi Stefan,
> 
> Thanks for the info.
> 
> I'd tried putting
> 
> ansible\_python\_interpreter: /home/\<me\>/py3/bin/python
> 
> in the ansible.cfg file in /etc/ansible/ but whilst it read the file as I could tell from the -vvv switch I was still  
> seeing parts of the run falling back to /usr/bin/python??
> 
> Anyway I subsequently (prior to you replying) installed the missing packages in the system os python version (cheating I  
> know bu I wanted to make sure it really was a problem with the path and this was the easiest way to test it). It worked.  
> but this didn't fix the original problem. Sooooo I saw your response and put the variable definition in the playbook and  
> this worked. I could see via the -vvv switch that all the python paths were correct.
> 
> Thanks for your help.

You are welcome. According to the docs the correct name for ansible.cfg is "python\_interpreter" in the [defaults]  
section ([https://docs.ansible.com/ansible/latest/reference\_appendices/interpreter\_discovery.html](https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html)).

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 20, 2019, 2:50pm UTC](https://forum.ansible.com/t/ansible-in-virtualenv-mixing-python-with-os/31323/5 "2019-09-20T14:50:24Z")

</div>

> > Hi Stefan,
> > 
> > Thanks for the info.
> > 
> > I'd tried putting
> > 
> > ansible\_python\_interpreter: /home/\<me\>/py3/bin/python
> > 
> > in the ansible.cfg file in /etc/ansible/ but whilst it read the file as I could tell from the -vvv switch I was still  
> > seeing parts of the run falling back to /usr/bin/python??
> > 
> > Anyway I subsequently (prior to you replying) installed the missing packages in the system os python version (cheating I  
> > know bu I wanted to make sure it really was a problem with the path and this was the easiest way to test it). It worked.  
> > but this didn't fix the original problem. Sooooo I saw your response and put the variable definition in the playbook and  
> > this worked. I could see via the -vvv switch that all the python paths were correct.
> > 
> > Thanks for your help.
> 
> You are welcome. According to the docs the correct name for ansible.cfg is "python\_interpreter" in the [defaults]  
> section ([https://docs.ansible.com/ansible/latest/reference\_appendices/interpreter\_discovery.html](https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html)).
> 
> Regards  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

Sorry, looks I wrote to soon. This only allows to set the mode of discovery despite the variable name :-(.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Mr\_Decisive](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@Mr\_Decisive](https://forum.ansible.com/u/Mr_Decisive)\
**Post date:** [September 20, 2019, 3:19pm UTC](https://forum.ansible.com/t/ansible-in-virtualenv-mixing-python-with-os/31323/6 "2019-09-20T15:19:41Z")

</div>

Hi Stefan,

Actually after trying your fix I went back to figure out why the config file was not working and it was then I discovered the environment name and the config ini name is different!!!

Anyway I set the variable in the config file (using the correct name) and disabled it in the playbook and it still worked and I could see with the -vvv it was using the correct paths.

I’ve been going round and round with this all day (on and off) so i’m pleased it is working now and I understand why.

Thanks again  
Phill
