# Failed to import the required Python library (psycopg2)

**URL:** <https://forum.ansible.com/t/failed-to-import-the-required-python-library-psycopg2/35351>\
**Category:** Ansible Project\
**Tags:** windows, ubuntu\
**Created:** [June 15, 2021, 3:27pm UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library-psycopg2/35351 "2021-06-15T15:27:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [June 15, 2021, 3:27pm UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library-psycopg2/35351/1 "2021-06-15T15:27:53Z")

</div>

On Ubuntu Linux node

$ ansible --version

ansible [core 2.11.1]

config file = /home/jenkins/.ansible.cfg

configured module search path = [‘/home/jenkins/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]

ansible python module location = /home/jenkins/.local/lib/python3.8/site-packages/ansible

ansible collection location = /home/jenkins/.ansible/collections:/usr/share/ansible/collections

executable location = /home/jenkins/.local/bin/ansible

python version = 3.8.0 (default, Feb 25 2021, 22:10:10) [GCC 8.4.0]

jinja version = 2.10

libyaml = False

On my Macbook

$ ansible --version

ansible 2.10.8

config file = /Users/user/.ansible.cfg

configured module search path = [‘/Users/user/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]

ansible python module location = /usr/local/Cellar/ansible/3.3.0/libexec/lib/python3.9/site-packages/ansible

executable location = /usr/local/bin/ansible

python version = 3.9.4 (default, Apr 5 2021, 01:50:46) [Clang 12.0.0 (clang-1200.0.32.29)]

I have installed postgresql on BOTH ny Linux node and my Mac.

I’m testing this task, where I want to run the query on a Windows host.

- **name** : Run a query

**postgresql\_query** :

**login\_host** : “{{ ansible\_hostname }}”

**login\_password** : “{{ pg\_db\_password }}”

**db** : “pg\_user”

**query** : “SELECT \* from pg\_user”

**delegate\_to** : localhost

And I get the following when running the playbook either on my Macbook, or on a Linux node.

TASK [qa-db-backup : Run a query] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* fatal: [10.227.xx.xx → localhost]: FAILED! =\> {“changed”: false, “msg”: “Failed to import the required Python library (psycopg2) on linux-node-0’s Python /usr/local/bin/python. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible\_python\_interpreter”}

Any clues?

Thanks,

Chris

---

<div class="post-metadata">

**Author:** ![Wei-Yen\_Tan](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wei-yen_tan/32/4111_2.png) [@Wei-Yen\_Tan](https://forum.ansible.com/u/Wei-Yen_Tan)\
**Post date:** [June 15, 2021, 3:30pm UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library-psycopg2/35351/2 "2021-06-15T15:30:12Z")

</div>

Install that python module psycopg2 on the local machine. You haven’t got the dependency installed

---

<div class="post-metadata">

**Author:** ![Wei-Yen\_Tan](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wei-yen_tan/32/4111_2.png) [@Wei-Yen\_Tan](https://forum.ansible.com/u/Wei-Yen_Tan)\
**Post date:** [June 15, 2021, 8:42pm UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library-psycopg2/35351/3 "2021-06-15T20:42:08Z")

</div>

Pip install pyscopg2 --user. I think

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [June 15, 2021, 8:43pm UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library-psycopg2/35351/4 "2021-06-15T20:43:18Z")

</div>

Per [https://stackoverflow.com/questions/33866695/error-installing-psycopg2-on-macos-10-9-5](https://stackoverflow.com/questions/33866695/error-installing-psycopg2-on-macos-10-9-5), installing Postgres should’ve installed it? That’s why I mentioned that I did install it.

Chris

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [June 15, 2021, 9:06pm UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library-psycopg2/35351/5 "2021-06-15T21:06:07Z")

</div>

Hi,

> Per  
> [https://stackoverflow.com/questions/33866695/error-installing-psycopg2-on-macos-10-9-5](https://stackoverflow.com/questions/33866695/error-installing-psycopg2-on-macos-10-9-5),  
> installing Postgres should've installed it? That's why I mentioned  
> that I did install it.

I cannot find that information on that link. If you are referencing the  
first answer ([https://stackoverflow.com/a/33866865](https://stackoverflow.com/a/33866865)), it says "pg\_config  
should come with postgres and psycopg2 is trying to find it." - that  
does NOT say that psycopg2 comes with it, but that it needs pg\_config,  
which in turn comes with postgres.

Cheers,  
Felix

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [June 15, 2021, 9:11pm UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library-psycopg2/35351/6 "2021-06-15T21:11:29Z")

</div>

Hmmm, I did tis on both Mac and Linux node. It helped on my Linux node but not on the Mac, that is, I don’t the Failed to import the required Python library (psycopg2) on the Linux node, but I still get it on the Mac.

$ python3 -m pip install psycopg2-binary

Defaulting to user installation because normal site-packages is not writeable

Collecting psycopg2-binary

Using cached psycopg2\_binary-2.8.6-cp38-cp38-macosx\_10\_9\_x86\_64.macosx\_10\_9\_intel.macosx\_10\_10\_intel.macosx\_10\_10\_x86\_64.whl (1.5 MB)

Installing collected packages: psycopg2-binary

Successfully installed psycopg2-binary-2.8.6

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [June 15, 2021, 9:12pm UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library-psycopg2/35351/7 "2021-06-15T21:12:25Z")

</div>

Ok my bad, I misead…
