Psycopg2: Python 3.8: ImportError: DLL load failed while importing _psycopg: The specified module could not be found.

Created on 29 Oct 2019  ยท  27Comments  ยท  Source: psycopg/psycopg2

TL; DR: update pip. See this comment

I get this error when trying to import psycopg2 in Python 3.8 on Windows 10. I already tried reinstalling (both psycopg2 and psycopg2-binary) and even reinstalling Postgres. Seems to work fine on Python 3.7.

>>> import psycopg2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Documents\Programming\blokken\.venv\lib\site-packages\psycopg2\__init__.py", line 50, in <module>
    from psycopg2._psycopg import (                     # noqa
ImportError: DLL load failed while importing _psycopg: The specified module could not be found.

Most helpful comment

Appveyor has added Python 3.8 support. Could this please be a priority now?

All 27 comments

990

I think this is actually a different issue, this is a problem with importing the library, not with installation.

You just have installed Postgres in your os and because library installs successfully.

Even Im facing same issue. Any luck with this ?

Nope, looks like this won't get fixed until Appveyor adds Python 3.8 support. I had to just downgrade back to Python 3.7 for now. :/

So the fix is waiting for new release to support 3.8 and walkaround is switching to 3.7 ๏ผŸ

FYI, if someone wants to fix the appveyor script and install Python 3.8 there for the build, be my guest.

So correct me if i'm wrong, but what i got from comments in other related issue threads here, the appveyor change/update is required to build and provide the binary package of psycopg2 for Python 3.8.

The problem is that the issue reported here, contrary to #1004, doesn't seem to be related to the lack of the binary package.

Because of the lack of a binary version, the package seems to be built by pip from sources locally, and i can also build it myself by downloading it manually.
In both cases it doesn't work when imported (at runtime) in Python 3.8 while it does in Python 3.7.
I clearly do pass the build prerequisites and runtime requirements.
The same error appears in Python 3.7 when i remove psql bin folder from PATH.
Is there a way to get the exact dll file that was not found from the ImportError? maybe that would shine some light here.
Should the appveyor issue also affect building the package from sources locally?

To be honest, psycopg2's setup.py does a _VERY_ poor job of building from source on end user's Windows. If someone is interested in improving it...

The windows environment has several challenges when building from source vs LINUX/UNIX, some of which:

  • Need to have the correct Visual C compiler installed based upon Python version (how many of us have Visual C++ 14 installed?)
  • Detect the location of the PostgreSQL build dependencies (includes/libs)
  • Detect if we also need any OpenSSL dependencies based upon the PostgreSQL build and where those lib/include files are located
  • If linking against DLL version of libpq, make sure all the appropriate DLLs (and correct versions) are accessible via the path

Also, the PostgreSQL binaries were probably built with a newer version of the Visual C compiler, causing the potential for multiple LIBC libraries being included into the process space when pscopyg2 is imported (This shouldn't be the case, but past experience is why we build ALL dependencies in Appveyor).

Appveyor has added Python 3.8 support. Could this please be a priority now?

I'm having this same problem.

Traceback (most recent call last):
File "main.py", line 9, in
import kepo
ImportError: DLL load failed while importing kepo: The specified module could not be found.

how do i fix this problem

Hi folks, I had the same problem and using python 3.7.2, I don't know if it works on python 3.8 or not, but I just downgraded psycopg2-binary to 2.7.* and everything is good.

Need it +1

Febrary 2020 - still the same problem

@vsmelov what pip version are you using? What is the command output, possibly with a -v?

So, Smelov Vladimir @vsmelov: your observation is perfectly correct: it is February 2020. You know your calendar. Now, in this rainy month, what brings you to dump a passive-aggressive comment on a closed bug? Because this bug has been long closed and last psycopg release is compatible indeed with Python 3.8. Binary packages are tested after build and do work for me.

If you think you have a problem please report details of what you are trying to do and about your environment. Otherwise you are just someone who thinks he is on twitter and want to create toxic atmosphere, which is a disrespectful behaviour for people who have made available for your their work for free, and for which I have no sympathy in my project.

Waiting for your answer.

I created a project yesterday evening with 3.8.2 and ran into this problem. Error can be found below.
This morning I did a new venv with 3.7.6, exact same setup besides that, and there was no issue.

One remark... When installing the package on python38 (the -binary), it seems to compile it... But not on python37...
Installing psycopg2-binary on python37: https://gist.github.com/tobiasgardner/57ac84e45c8c5502ea5682852e300a31
Installing psycopg2-binary on python38: https://gist.github.com/tobiasgardner/7f93fcd22e413b525833adfe75c54d92

C:\Users\tobbe\AppData\Local\JetBrains\Toolbox\apps\PyCharm-P\ch-0\193.6494.30\bin\runnerw64.exe C:\Users\tobbe\Documents\git-repos\ecombooster\.venv\Scripts\python.exe C:/Users/tobbe/Documents/git-repos/ecombooster/manage.py runserver 8000 Watching for file changes with StatReloader Exception in thread django-main-thread: Traceback (most recent call last): File "C:\Program Files\Python38\lib\threading.py", line 932, in _bootstrap_inner self.run() File "C:\Program Files\Python38\lib\threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "C:\Users\tobbe\Documents\git-repos\ecombooster\.venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper fn(*args, **kwargs) File "C:\Users\tobbe\Documents\git-repos\ecombooster\.venv\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run autoreload.raise_last_exception() File "C:\Users\tobbe\Documents\git-repos\ecombooster\.venv\lib\site-packages\django\utils\autoreload.py", line 76, in raise_last_exception raise _exception[1] File "C:\Users\tobbe\Documents\git-repos\ecombooster\.venv\lib\site-packages\django\core\management\__init__.py", line 357, in execute autoreload.check_errors(django.setup)() File "C:\Users\tobbe\Documents\git-repos\ecombooster\.venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper fn(*args, **kwargs) File "C:\Users\tobbe\Documents\git-repos\ecombooster\.venv\lib\site-packages\django\__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "C:\Users\tobbe\Documents\git-repos\ecombooster\.venv\lib\site-packages\django\apps\registry.py", line 91, in populate app_config = AppConfig.create(entry) File "C:\Users\tobbe\Documents\git-repos\ecombooster\.venv\lib\site-packages\django\apps\config.py", line 116, in create mod = import_module(mod_path) File "C:\Program Files\Python38\lib\importlib\__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1014, in _gcd_import File "<frozen importlib._bootstrap>", line 991, in _find_and_load File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 671, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 783, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "C:\Users\tobbe\Documents\git-repos\ecombooster\.venv\lib\site-packages\django\contrib\postgres\apps.py", line 4, in <module> from psycopg2.extras import DateRange File "C:\Users\tobbe\Documents\git-repos\ecombooster\.venv\lib\site-packages\psycopg2\__init__.py", line 50, in <module> from psycopg2._psycopg import ( # noqa ImportError: DLL load failed while importing _psycopg: The specified module could not be found.

@tobiasgardner thank you for the report.

The psycopg packages are available here, this is where pip would fetch them from.

My questions are:

  • What is your pip version?
  • Pip should download a wheel file. What file does pip download psycopg2 download? The binary package (which I assume psycopg2-2.8.4-cp38-cp38-win_amd64.whl but let me know which) or the .tar.gz?
  • If it downloads the .tar.gz I wonder why, so what is the output of pip download -v psycopg2?
  • If it downloads the .whl, why it doesn't install it? So what is the output of pip install -v psycopg2-2.8.4-cp38-cp38-win_amd64.whl?

On windows the binary package and the non-binary package are the same, but please try the above with psycopg2-binary instead of psycopg2 too, thank you.

@dvarrazzo Hmm, there is something strange here... Noticed now that when I check pip version from PyCharm environment (settings), it says pip version 20.0.2 for Python 3.7 BUT when I run pip list in the terminal, it says 19.0.3... For the python 3.8 environment, it is the same... But when I try to upgrade from command prompt, it says that I already have 20.0.2...

(.venv38) C:\Users\tobbe\Documents\git-repos\ecombooster>pip list
pip                  19.0.3
(.venv38) C:\Users\tobbe\Documents\git-repos\ecombooster>python -m pip install --upgrade pip
Requirement already up-to-date: pip in c:\users\tobbe\documents\git-repos\ecombooster\.venv38\lib\site-packages (20.0.2)
(.venv38) C:\Users\tobbe\Documents\git-repos\ecombooster>pip --version
pip 19.0.3 from c:\users\tobbe\documents\git-repos\ecombooster\.venv38\lib\site-packages\pip-19.0.3-py3.8.egg\pip (python 3.8)

Staying in the same venv from command prompt...

(.venv38) C:\Users\tobbe\Documents\git-repos\ecombooster>pip download psycopg2
Collecting psycopg2
  Using cached https://files.pythonhosted.org/packages/84/d7/6a93c99b5ba4d4d22daa3928b983cec66df4536ca50b22ce5dcac65e4e71/psycopg2-2.8.4.tar.gz
  Saved c:\users\tobbe\documents\git-repos\ecombooster\psycopg2-2.8.4.tar.gz
Successfully downloaded psycopg2

(.venv38) C:\Users\tobbe\Documents\git-repos\ecombooster>pip download psycopg2-binary
Collecting psycopg2-binary
  Using cached https://files.pythonhosted.org/packages/00/7b/a623f49b3248957e7eaaac52eba1117209775d54e7a8501c460473a7ba4f/psycopg2-binary-2.8.4.tar.gz
  Saved c:\users\tobbe\documents\git-repos\ecombooster\psycopg2-binary-2.8.4.tar.gz
Successfully downloaded psycopg2-binary

I.e. your assumption that the .whl file is downloaded was wrong, I get the tar.gz files.

You can find the output of pip download -v psycopg2 here:
https://gist.github.com/tobiasgardner/6ded1f9259934f2c66fd51c7e4f16121

You can find the output of pip download -v psycopg2-binary here:
https://gist.github.com/tobiasgardner/14511457831d99480eee5a40d5ecc8b7

Since I did not get the .whl file, it is not possible to run this command: pip install -v psycopg2-2.8.4-cp38-cp38-win_amd64.whl, it results in an error message:

(.venv38) C:\Users\tobbe\Documents\git-repos\ecombooster>pip install -v psycopg2-2.8.4-cp38-cp38-win_amd64.whl
...
Requirement 'psycopg2-2.8.4-cp38-cp38-win_amd64.whl' looks like a filename, but the file does not exist
...

What python version is that? the one downloaded and installed from python.org or something else?

In your unedited message you had:

Requirement 'psycopg2-2.8.4-cp38-cp38-win_amd64.whl' looks like a filename, but the file does not exist

Does it exist? Isn't this a problem with pip?

Config variable 'Py_DEBUG' is unset, Python ABI tag may be incorrect
Config variable 'WITH_PYMALLOC' is unset, Python ABI tag may be incorrect

these seem relevant for pip to misidentify your Python version - or it's actually genuinely incompatible with the wheel package.

A quick googling brought me to https://github.com/pypa/pip/issues/3383 and report the same problem with several other packages.

Can you look into that more? It doesn't seem a problem with psycopg to me.

Python 3.8.2, should be official version:

(.venv38) C:\Users\tobbe\Documents\git-repos\ecombooster>python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32

No the file does not exists... I guess (as you stated) that the this pip download psycopg2 should download the .whl file but instead I get the .tar.gz file.

However, testing the same on the Python 3.7 environment, I do get the .whl file...

(.venv37) C:\Users\tobbe\Documents\git-repos\ecombooster>pip download psycopg2
Collecting psycopg2
  Downloading https://files.pythonhosted.org/packages/1a/85/853f11abfccfd581b099e5ae5f2dd807cc2919745b13d14e565022fd821c/psycopg2-2.8.4-cp37-cp37m-win_amd64.whl (1.1MB)
    100% |โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 1.1MB 11.4MB/s
  Saved c:\users\tobbe\documents\git-repos\ecombooster\psycopg2-2.8.4-cp37-cp37m-win_amd64.whl
Successfully downloaded psycopg2

@tobiasgardner and others who see the ImportError referenced in the issue name who are using a PyCharm administered venv on Windows 10. I followed these steps to reproduce the error and then to resolve it:

  • install psycopg2 from venv through PyCharm -> Settings -> Project Interpreter _with_ pip 19.0.2

  • Try to do whatever thing you do to get the ImportError, receive:
    ImportError: DLL load failed while importing _psycopg: The specified module could not be found.

  • upgrade pip to 20.0.2 through PyCharm -> Settings -> Project Interpreter

  • It will tell you that 'Upgrade packages failed', it's wrong it really did upgrade

  • Uninstall psycopg2 through PyCharm -> Settings -> Project Interpreter

  • Re-install psycopg2 through PyCharm -> Settings -> Project Interpreter

  • Do the thing from above second step again, no longer receive the ImportError

@dvarrazzo It seems like something wonky with pip 19.0.2 for my case, as you said it isn't anything on the psycopg2 end. Hopefully this helps people :)

@zthurman thank you immensely for straightening up this whole matter!

Others: can you verify?

Thank you.

Can any of you report a bug to Pycharm? I can't.

https://www.jetbrains.com/help/pycharm/reporting-issues.html#report-an-issue

Hi all.

I am new to Pyhton and im having this same problem when trying to import libraries Pandas and Tabula. I dont know why but i didnt have any problem when importing PyPDF2.

I see @zthurman found a solution but since im new to Python I dont have PyCharm and dont know how to use it, is there any other way to solve this problem without PyCharm?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

psycoteam picture psycoteam  ยท  4Comments

yamagoya picture yamagoya  ยท  5Comments

anarazel picture anarazel  ยท  3Comments

dvlhntr picture dvlhntr  ยท  6Comments

jasonrubenstein picture jasonrubenstein  ยท  5Comments