PyCharm tries to use wrong Postgres version with packages updated.
I'm running PyCharm 2023.2.3 / Python 3.11 on an M2 Mac. My app uses Pyside6 and psycopg2. I recently installed Postgres 16 (via the Postgres.app for Mac). Previous rev was Postgres 14 which I deleted from my system. My app runs properly at the command line, but in PyCharm, the Pyside-based connection looks for the older Postgres version (14 while the psycopg2 connection works properly). I created a new project and downloaded the relevant packages but the problem persists. Any idea where PyCharm (Pyside6) is getting a reference to the old Postgres library?
Please sign in to leave a comment.
If the code is identical between the terminal and PyCharm, then the only difference I can assume is the environment variables.
You can run the following snippet from both the terminal and PyCharm, and compare the outputs with a diff: https://gist.github.com/aresler/f1286eca246f40b27c1027480c175c4d
Andrey, thanks for your response. I had previously checked the paths but I ran the script in your link to check again. My PATH environment variable includes ‘…/Postgres.app/Contents/Versions/latest/bin’ and ‘…/Postgres.app/Contents/Versions/latest/lib’. These point to the ‘16’ version of Postgres. I couldn't find any hard references to the folder ‘…/Postgres.app/Contents/Versions/14…’ which is where the error is cropping up. I set ‘export QT_DEBUG_PLUGINS=1’ (for debugging Pyside6) in .zshrc. The error occurs when libqsqlpsql.dylib looks for the Postgres library in ‘…/Postgres.app/Contents/Versions/14…’ I checked the directories in my PATH variable in front of the postgres paths and couldn't find anything suspicious there.
I know this sounds ridiculous. I searched for hours and hours yesterday and it made no sense to me. I don't know how my app works from the command line. I must have a difference in the environment somewhere.
I wanted to post what I found in case someone else winds up here. I had the DYLD_LIBRARY_PATH set in my .zshrc (which is probably the wrong place for this environment variable, but it has been there forever). PyCharm does not read this variable as far as I can tell. This environment variable was not set in PyCharm. By setting the DYLD_LIBRARY_PATH to path to the newer version of Postgres in the run configuration for my app solves the problem. I am still not sure how it was picking up the previous Postgres version.
E.g.., the Linux desktop session typically doesn't read the shell profile, so if you start PyCharm from desktop, it won't source it.
But if you open the terminal and start PyCharm from there, it will start from interactive session and will have .zshrc variables in it's environment.
So setting the env variable in the run/debug configuration is the valid solution.