Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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:\Work\WRR\git\tools\JTunnelTestApp>  cd 'c:\Work\WRR\git\tools\JTunnelTestApp'; & 'C:\Programs\Anaconda3\envs\wr\python.exe' 'c:\Users\petrop01\.vscode\extensions\ms-python.python-2020.9.114305\pythonFiles\lib\python\debugpy\launcher' '56143' '--' 'c:\Work\WRR\git\tools\JTunnelTestApp\main.py'
Traceback (most recent call last):
  File "c:\Work\WRR\git\tools\JTunnelTestApp\main.py", line 1, in <module>
    import psycopg2
  File "C:\Programs\Anaconda3\envs\wr\lib\site-packages\psycopg2\__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:\Work\WRR\git\tools\JTunnelTestApp>

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

like image 921
peter.petrov Avatar asked Oct 12 '20 08:10

peter.petrov


2 Answers

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

like image 154
Mahdi Sadeghi Avatar answered Sep 30 '22 17:09

Mahdi Sadeghi


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.

like image 20
Levitybot Avatar answered Sep 30 '22 19:09

Levitybot