Cannot get python to see psycopg2 on OSx

13 views
Skip to first unread message

Barry Kaplan

unread,
Dec 10, 2018, 2:36:56 AM12/10/18
to Ansible Project
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 \"<string>\", line 1, in <module>\nImportError: No module named psycopg2", "stderr_lines": ["Traceback (most recent call last):", "  File \"<string>\", line 1, in <module>", "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



Dick Visser

unread,
Dec 10, 2018, 7:44:39 AM12/10/18
to ansible...@googlegroups.com
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
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b1ecaad7-e46f-423f-8dd1-db47060985c3%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Barry Kaplan

unread,
Dec 10, 2018, 9:44:09 AM12/10/18
to Ansible Project
Dick, you are saying that /usr/local/bin/pip is installing modules in the wrong place? Why would that be?

Dick Visser

unread,
Dec 10, 2018, 10:17:34 AM12/10/18
to ansible...@googlegroups.com
On Mon, 10 Dec 2018 at 15:44, Barry Kaplan <mem...@gmail.com> wrote:
>
> 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
Reply all
Reply to author
Forward
0 new messages