'Importing psycopg2 with QGIS-python in PyCharm fails

I've been using python 3.6 from QGIS 3.10 in PyCharm 2021.1.3 and had a script that uses

import psycopg2

After an update to QGIS 3.16.16. and python 3.9 the import fails with

File "C:\Program Files\QGIS 3.16.16\apps\Python39\lib\site-packages\psycopg2_init_.py", line 51, in from psycopg2._psycopg import ( # noqa ImportError: DLL load failed while importing _psycopg: The specified module could not be found.

There seems to have been an issue shortly after the release of Python 3.9, but that was in 2020 and the site in GitHub states it should work fine.

When I use the python console in QGIS directly, I can import psycopg2 just fine, but setting this version as the interpreter in PyCharm, I get the error above.

Unfortunately I do not have admin rights and cannot install anything on this machine, nor run pip or the like. QGIS-python comes with psycopg2-binary. Any subfolder I need to include or something like that?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source