# Cannot get python to see psycopg2 on OSx

**URL:** <https://forum.ansible.com/t/cannot-get-python-to-see-psycopg2-on-osx/29231>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [December 10, 2018, 7:36am UTC](https://forum.ansible.com/t/cannot-get-python-to-see-psycopg2-on-osx/29231 "2018-12-10T07:36:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [December 10, 2018, 7:36am UTC](https://forum.ansible.com/t/cannot-get-python-to-see-psycopg2-on-osx/29231/1 "2018-12-10T07:36:56Z")

</div>

I’ve been pounding my head on this for a few hours now.

Nothing I do can get python to import psycopg2.

Consider:

`

- name: Setup mysql databases  
hosts: localhost  
connection: local  
become: no  
tags: [database, postgres]  
gather\_facts: no

pre\_tasks:

- debug: var=ansible\_python\_interpreter

- name: Install psycopg2 for ansible to be able to create postgresql users  
become: yes  
pip:  
name: psycopg2  
register: \_psycopg2

- debug: var=\_psycopg2

- command: “{{ ansible\_python\_interpreter }} -c ‘import {{ module }}; print({{ module }}. **version** )’”  
vars:  
module: psycopg2  
register: test

- debug: var=test

`

But the results are:

`  
ansible❯ ap -i inventory/aws/ops playbooks/deploy/ops-alerta.yml --tags database ops/git/master !+

PLAY [Setup mysql databases] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {  
“ansible\_python\_interpreter”: “/usr/local/bin/python”  
}

TASK [Install psycopg2 for ansible to be able to create postgresql users] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1]

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {  
“\_psycopg2”: {  
“changed”: false,  
“cmd”: “/usr/local/bin/pip2 install psycopg2”,  
“failed”: false,  
“name”: [  
“psycopg2”  
],  
“requirements”: null,  
“state”: “present”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “Requirement already satisfied: psycopg2 in /Library/Python/2.7/site-packages (2.7.6.1)\n”,  
“stdout\_lines”: [  
“Requirement already satisfied: psycopg2 in /Library/Python/2.7/site-packages (2.7.6.1)”  
],  
“version”: null,  
“virtualenv”: null  
}  
}

TASK [command] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [127.0.0.1]: FAILED! =\> {“changed”: true, “cmd”: [“/usr/local/bin/python”, “-c”, “import psycopg2; print(psycopg2. **version** )”], “delta”: “0:00:00.051150”, “end”: “2018-12-09 23:33:22.335590”, “failed”: true, “msg”: “non-zero return code”, “rc”: 1, “start”: “2018-12-09 23:33:22.284440”, “stderr”: “Traceback (most recent call last):\n File "", line 1, in \nImportError: No module named psycopg2”, “stderr\_lines”: [“Traceback (most recent call last):”, " File "", line 1, in ", “ImportError: No module named psycopg2”], “stdout”: “”, “stdout\_lines”: }  
to retry, use: --limit @/Users/bkaplan/.ansible/retry-files/ops-alerta.retry

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
127.0.0.1 : ok=3 changed=0 unreachable=0 failed=1

Playbook run took 0 days, 0 hours, 0 minutes, 2 seconds

`

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [December 10, 2018, 12:44pm UTC](https://forum.ansible.com/t/cannot-get-python-to-see-psycopg2-on-osx/29231/2 "2018-12-10T12:44:13Z")

</div>

So you seem to mix a 3rd party python instance with the system one.  
Your python and pip reside in /usr/local/bin, but the modules it  
installs end up under /Library/Python.  
Those need to match. So either use all system ones, or all 3rd party ones.

Dick

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [December 10, 2018, 2:44pm UTC](https://forum.ansible.com/t/cannot-get-python-to-see-psycopg2-on-osx/29231/3 "2018-12-10T14:44:08Z")

</div>

Dick, you are saying that /usr/local/bin/pip is installing modules in the wrong place? Why would that be?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [December 10, 2018, 3:17pm UTC](https://forum.ansible.com/t/cannot-get-python-to-see-psycopg2-on-osx/29231/4 "2018-12-10T15:17:09Z")

</div>

Yes, but I'm not sure why this is happening on yoru system.  
I run a local version as well (as /usr/local/bin/pjp, from homebrew),  
but if I install modules they go into  
/usr/local/lib/python2.7/site-packages.

Dick
