4

I have the same problem that was discussed here, but I haven't credit to comment an answer so I start new question.

I have in PATH way to libpq.dll (C:\PostgreSql\lib) but it doesn't solve this problem.

Using Python 2.7.9 32-bit, PostgreSQL 8.4, Win 8

Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
    import pg
  File "C:\Python27\lib\site-packages\pg.py", line 21, in <module>
from _pg import *
ImportError: DLL load failed: The specified module could not be found.
Community
  • 1
  • 1
Vena Vena
  • 67
  • 1
  • 5
  • P.S. If you've installed python 32bit, Postgres 64bit and receive this message. Then uninstalled python 32bit and installed python 64bit, you will need to delete `PyGreSQL-5.0.dist-info` from site-packages and re-install. This should fix the dll load failed message. – Chad Aug 07 '16 at 10:19

3 Answers3

3

I was also facing the same issue on Win 8. First time I had installed PostgreSQL in "C:\Program Files" and also set environment PATH to point to PostgreSQL folder. I was suspecting permission issue for "C:\Program Files" folder.

I was able to fix this issue by following the steps as mentioned below.

  • Uninstalled PostgreSQL
  • Re-installed PostgreSQL in "C:\PostgreSQL" Note that I have not installed PostgreSQL into "C:\Program Files" folder this time
  • Set the environment PATH C:\PostgreSQL\9.4;C:\PostgreSQL\9.4\bin
  • Also ensure that _pg.pyd exist in C:\Python27\Lib\site-packages
Vinkal
  • 2,964
  • 1
  • 19
  • 19
  • 1
    thx for reply, now it works. For my configuration I added C:\postgreSQL;C:\postgreSQL\bin;C:\Python27\Lib\site-packages; to PATH – Vena Vena Feb 23 '15 at 08:06
  • @Vena Vena - If it works for you, please mark as solved. Thanks! – Vinkal Feb 25 '15 at 06:43
  • 1
    In my case it wasn't enough with moving PostgreSQL to "C:\PostgreSQL", I had Python installed in "C:\Program Files\" and didn't get PyGreSQL working until I reinstalled Python in "C:\Python37". – UaT Sep 07 '19 at 14:49
  • Also had to reinstall Python as @UaT suggested. Now it works fine – michal-ko Nov 24 '20 at 18:22
2

I got the same error on Win10 with Python 3.5 and Python 3.8, both are 64bit, and Postresql 12, also 64bit. DLL locations "c:\Program Files\PostgreSQL\12\bin" and "c:\Program Files\PostgreSQL\psqlODBC\bin" were added to the PATH but it caused another error:

  • "from _pg import * ImportError: DLL load failed: The operating system cannot run %1."

Then I've checked what is going on with Process Monitor from Sysinternals and found that libpg.dll was looking for other DLLs. Finally, the following files:

  • libpq.dll
  • libssl-1_1-x64.dll
  • libcrypto-1_1-x64.dll

were copied from "c:\Program Files\PostgreSQL\12\bin" folder to:

  • "c:\Users...\AppData\Local\Programs\Python\Python35"
  • "c:\Users...\AppData\Local\Programs\Python\Python38"

folders and now "import pg" works fine in both versions of Python.

0

I faced just the same issue; just to try my chance before doing what you say, I changed the PATH environment variable: I directly specified the whole directory path of libpq.dll OF THE ODBC DRIVER OF PostgreSQL (there are other "libpqdll"s as well in other relevant directories) which takes place in my Windows10 here: C:\Program Files\PostgreSQL\psqlODBC\bin .. and the problem is solved.. before, my PATH specification was C:\Program Files\PostgreSQL and I think the driver looked for the dll files directly here and either 1) when couldnt find directly in this specific diretory (by disregarding the subdirectories), 2) or came cross other "libpq.dll"s which didnt work for the driver, it gave the error message.. so here I come to the conclusion that it looks for the dll file OF THE ODBC DRIVER in PATH env.varible, so I needed to specify in the PATH environment variable directly the directory of the PostgreSQL's ODBC driver I used, psqlODBC .. for future needs I wanted to write those details:)

babacim
  • 11
  • 1
  • 5