ImportError "no pq wrapper available" when importing psycopg3
Asked Answered
U

2

38

I installed pyscopg3 on my venv using pip install psycopg[binary] as per the documentation but I still get an import error:

Exception has occurred: ImportError
no pq wrapper available.
Attempts made:
- couldn't import psycopg 'c' implementation: No module named 'psycopg_c'
- couldn't import psycopg 'binary' implementation: DLL load failed while importing pq: The specified module could not be found.
- couldn't import psycopg 'python' implementation: libpq library not found

I'm running a Windows 10 machine. How can I solve this error?

Upali answered 4/5, 2022 at 12:11 Comment(1)
I'm not qualified to assess whether the answers to the duplicate #72246461 add anything useful. If they do, please flag for mod attention and request th two questions to be merged.Maltreat
P
47

I followed the psycopg installation steps from https://pypi.org/project/psycopg/

pip install --upgrade pip           # to upgrade pip

pip install "psycopg[binary,pool]"  # to install package and dependencies

it worked for me

Polariscope answered 7/3, 2023 at 15:35 Comment(2)
For macbook intel this worked for meZibet
was trying to get it working in intellij without much success. I was able to run this in the venv that was setup for the project and things seem better. Thanks!Proficient
G
15

You need to install the command line tools on PostgreSQL on your Windows machine. Download the full server installer here: https://www.enterprisedb.com/downloads/postgres-postgresql-downloads

You don't need to install the full server package, only command line tools in the installer options will be necessary:

enter image description here

After the installation, you need to add the PostgreSQL bin folder in your PATH environnement variable:

hit Windows Key+R at the same time to get command prompt. Then type sysdm.cpl, go to advanced and select "Environment Variables", in PATH add the path to : C:\Program Files\PostgreSQL\13\bin\ folder (or whatever folder you choose to install the PostreSQL commande line tools).

IMPORTANT: do not forget to close and restart your dev environnement (ie: VSCode, PyCharm, ...) to take the new environnement variable into account.

Note : This answer is related to Windows machine. For Linux the installation of a at least one postgresql-client-<version> package will be enough.

related to: https://mcmap.net/q/186862/-how-do-i-install-just-the-client-tools-for-postgresql-on-windows

Grous answered 2/7, 2022 at 16:39 Comment(4)
Did we need to install PostgreSQL commandline tools to use psycopg2 as well? I don't remember needing to install PostgreSQL commandline tools on my prod machine to run pyinstaller-compiled apps that used psycopg2, but now that I've refactored them to use psycopg 3, I needed to follow your advice to make them work again.Courlan
@NickMuise I didn't use psycopg v2 in the past, so I cannot confirm. But to be honnest I don't think psycopg2 dev ships the DLL with their python package... So for me on you prod server you should have the package already installed or a postgresql database already setuped.Grous
Thanks SuperPoney. Installing the command line tools of PostgreSQL helped me to fix the psycopg[binary] install correctly :)Khorma
I had to restart Windows for it to work. Just reloading the environment was not enough.Towill

© 2022 - 2024 — McMap. All rights reserved.