Reputation: 39457
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
Upvotes: 20
Views: 40295
Reputation: 926
I had error: "ImportError: DLL load failed while importing _psycopg: The specified module could not be found."
The problem was that psycopg2 doesn't work with Python ver. 3.12 and above,
I chose to downgrade Python to version 3.11.9, then install psycopg2-binary, and it worked for me.
Upvotes: 4
Reputation: 733
You can use psycopg2-binary
library instead of psycopg2
. After installation the usage is the same.
psycopg2 requires some dll file to be installed on your operating system. You can either install it manually or you can install psycopg2-binary instead which packs both the library and dll files together.
The documentation resides in psycopg2 library docs: https://psycopg.org/docs/install.html
Upvotes: 18
Reputation: 1
I had the same issue with Python version 3.13. I downgraded my version of psycopg from 2.9.10 to 2.9.9, and that fixed it for me.
You can do this with pip install --upgrade psycopg2==2.9.9
. Although it says upgrade
, all that means is change the version number. So you can 'upgrade' it to a lower version.
Upvotes: 0
Reputation: 190
I was running python 3.8 and so installing psycopg2==2.8.4 was necessary. (psycopg2-binary-2.9.10 didnt work)
Upvotes: 0
Reputation: 1
This worked for me
python -m pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org --trusted-host pypi.python.org psycopg2
Upvotes: 0
Reputation: 726
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
Upvotes: 7
Reputation: 657
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.
Open Anaconda navigator
Environments
Select the environment you want to install psycopg2/psycopg2-binary to and Open Terminal
Uninstall any pervious installs
pip uninstall psycopg2
pip uninstall psycopg2-binary
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.
Upvotes: 14
Reputation: 81
for me updating to psycopg2 and psycopg2-binary to 2.8.6 worked in python 3.8
Upvotes: 2