Cannot get python to see psycopg2 on OSx

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

`

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

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

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