psycopg2 – ImportError: DLL load failed while importing _psycopg: The operating system cannot run %1

Question:

I installed psycopg2 using conda on Windows 10.

https://anaconda.org/anaconda/psycopg2

I did it in a clean new conda environment (named wr).

I then tried to run this sample app but I am getting this error (see below).
I have no idea what I might be doing wrong because it was all straightforward and I did it in a clean way.

Any ideas how to solve this?

import psycopg2
try:
    connection = psycopg2.connect(user = "***",
                                  password = "***",
                                  host = "***",
                                  port = "5432",
                                  database = "***")


    cursor = connection.cursor()
    # Print PostgreSQL Connection properties
    print ( connection.get_dsn_parameters(),"n")

    # Print PostgreSQL version
    cursor.execute("SELECT version();")
    record = cursor.fetchone()
    print("You are connected to - ", record,"n")

except (Exception, psycopg2.Error) as error :
    print ("Error while connecting to PostgreSQL", error)
finally:
    #closing database connection.
        if(connection):
            cursor.close()
            connection.close()
            print("PostgreSQL connection is closed")

Error in VS code:

PS C:WorkWRRgittoolsJTunnelTestApp>  cd 'c:WorkWRRgittoolsJTunnelTestApp'; & 'C:ProgramsAnaconda3envswrpython.exe' 'c:Userspetrop01.vscodeextensionsms-python.python-2020.9.114305pythonFileslibpythondebugpylauncher' '56143' '--' 'c:WorkWRRgittoolsJTunnelTestAppmain.py'
Traceback (most recent call last):
  File "c:WorkWRRgittoolsJTunnelTestAppmain.py", line 1, in <module>
    import psycopg2
  File "C:ProgramsAnaconda3envswrlibsite-packagespsycopg2__init__.py", line 51, in <module>
    from psycopg2._psycopg import (                     # noqa
ImportError: DLL load failed while importing _psycopg: The operating system cannot run %1.
PS C:WorkWRRgittoolsJTunnelTestApp>

EDIT: Seems they had a bug open for this 2 years ago and they just closed it, ignoring it completely.

https://github.com/psycopg/psycopg2/issues/734

Asked By: peter.petrov

||

Answers:

you can use psycopg2-binary library instead of psycopg2. after installation the usage is the same.

Answered By: Mahdi Sadeghi

for me updating to psycopg2 and psycopg2-binary to 2.8.6 worked in python 3.8

Answered By: Vaibhav singh

For Windows when using Anaconda I have found that installing from the VS Code/Windows terminal just doesn’t work for all cases. Instead install from the Anaconda terminal. I have no idea why this is the case, but it has been the fix on multiple computers.

  1. Open Anaconda navigator

  2. Environments

  3. Select the environment you want to install psycopg2/psycopg2-binary to and Open Terminal

  4. Uninstall any pervious installs

    pip uninstall psycopg2

    pip uninstall psycopg2-binary

  5. Install again

    pip install psycopg2

    pip install psycopg2-binary

Now it should work.

Particularly found this useful to get standalone scripts that make use of Django ORM to work with Postgresql. Django was working fine, but without this fix the standalone scripts don’t. Very strange.

Answered By: Levitybot

I found the solution in this reddit post, all credit to u/brianckeegan

If you’re using conda to manage psycopg2 for Python 3.9+, the wheels point to an old version (v2.8.6) which causes this error. If you install via pip, you’ll get a more up-to-date version (v2.9.1) that supports Python 3.9. Until the conda wheels are updated:

conda remove psycopg2
pip install psycopg2
Answered By: Cristobal Sarome

For me, updating psycopg2 to 2.9.1 works in Python 3.10

Answered By: ryanhex53
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.