Import psycopg2 to establish a connection to postgres server, and interact with it in python - postgresql

I am on the Udacity Full Stack Nanodegree and the instructions are not very clear for installing psycopg2 on visual studio code for postgresql. I am using postgresql15
PLEASE HELP, i've spent 4 days trouble shooting instead of attempting my 1st project
I have installed the psycopg2 using the following commands and its says it has been satisfied, however it's still not allowing me to use psycopg2:
brew install openssl
Downloading https://formulae.brew.sh/api/cask.json
100.0%
Warning: openssl#3 3.0.8 is already installed and up-to-date.
To reinstall 3.0.8, run:
brew reinstall openssl#3
ada#adas-mbp ~ % export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
ada#adas-mbp ~ % pip3 install psycopg2
Requirement already satisfied: psycopg2 in ./.pyenv/versions/3.9.5/lib/python3.9/site-packages (2.9.5)
My visual studio code is not connecting to the database:
Exception has occurred: ImportError
dlopen(/Users/ada/.pyenv/versions/3.9.7/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so, 2): Library not loaded: #rpath/libssl.1.1.dylib
Referenced from: /Users/ada/.pyenv/versions/3.9.7/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so
Reason: image not found
File "/Users/ada/class-demos/demo.py", line 1, in <module>
import psycopg2
ImportError: dlopen(/Users/ada/.pyenv/versions/3.9.7/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so, 2): Library not loaded: #rpath/libssl.1.1.dylib
Referenced from: /Users/ada/.pyenv/versions/3.9.7/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so
Reason: image not found

Related

psycopg2 error: Library not loaded: #rpath/libssl1.1.dylib when running tox pytest

I am unable to run my unit tests in Python3.7/pytest. The app is a flask-restplus application, and I am using sqlalchemy and psycopg2 to interact with a PostgreSQL10 database. This code used to work before updating my system. After running brew update and brew upgrade awhile back, openssl was installed as openssl#1.1 and specifically openssl#1.1.1b. The exact error I receive is
E ImportError: dlopen(/Users/myuser/myproject/.tox/dev/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so, 2): Library not loaded: #rpath/libssl.1.1.dylib
E Referenced from: /Users/myuser/myproject/.tox/dev/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so
E Reason: image not found
Also, I am running OSX Mojave version 10.14.4.
I have tried symlinking the library and reinstalling openssl as well as the other methods as mentioned here: psycopg2 installation error - Library not loaded: libssl.dylib. Note that I changed the openssl version in the symlink to 1.1 instead of 1.0 but that still did not fix the situation.
I expect the tests to pass (they do on coworkers setup, which is the same MacBook, OS Version and homebrew setup), but any test that has any interaction with the database fails with the above error.

throwing error while importing pymysql in Python code

I am working in machine in which python 3.6 is installed on windows 7
I cross validated pymysql installation.
pip3 install PyMySQL
Requirement already satisfied: PyMySQL in c:\users\
But still in the code, its throwing error while importing pymysql.
import pymysql
error detail:
ModuleNotFoundError: No module named 'pymysql'
You would need to check if the installation was done correctly
If you are not able to install it properly you could download the gzip or the wheel and do a pip install of that gzip or the wheel
Both are available here:
https://pypi.python.org/pypi/PyMySQL

Can't install psycopg2 successfully

Requirement already satisfied: psycopg2 in/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
This is the error message I get when I run
pip install psycopg2
in my terminal on Mac. But when I try to import it in python (simply using import psycopg2) in either version 2.7 or 3.6 of python I get an error message:
No module named 'psycopg2'
Any suggestions?

iPython/jupyter notebook fails to start "image not found"

Trying to start iPython/jupyter notebook crashes giving the following error:
ImportError: dlopen(/Library/Python/2.7/site-packages/zmq/backend/cython/constants.so, 2): Library not loaded: /opt/local/lib/libzmq.3.dylib
Referenced from: /Library/Python/2.7/site-packages/zmq/backend/cython/constants.so
Reason: image not found
Tracked down a solution here:
https://github.com/zeromq/pyzmq/issues/493
Something had gone wrong with the install of pyzmq but uninstalling via macports or pip then reinstalling didn't get rid of the problem files.
Deleting the zmq directories from /Library/Python/2.7/site-packages/ then reinstalling using pip fixed this.

How to install psycopg2 on windows 7

I have tried pip install psycopg2 and following errors i got:
Error:
(ScrapeProj) C:\Users\e2sn7cy\Documents\GitHub\scraper>pip install psycopg2==2.5.4
Collecting psycopg2==2.5.4
Using cached psycopg2-2.5.4.tar.gz
Complete output from command python setup.py egg_info:
running egg_info
creating pip-egg-info\psycopg2.egg-info
writing pip-egg-info\psycopg2.egg-info\PKG-INFO
writing top-level names to pip-egg-info\psycopg2.egg-info\top_level.txt
writing dependency_links to pip-egg-info\psycopg2.egg-info\dependency_links.txt
writing manifest file 'pip-egg-info\psycopg2.egg-info\SOURCES.txt'
warning: manifest_maker: standard file '-c' not found
Error: pg_config executable not found.
Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:
python setup.py build_ext --pg-config /path/to/pg_config build ...
or with the pg_config option in 'setup.cfg'.
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in c:\users\e2sn7cy\appdata\local\temp\pi
p-build-joij3x\psycopg2
Then I tried using easy_install by downloading the .exe file from Stickpole and I got following errors :
(ScrapeProj) C:\Users\e2sn7cy\Envs\ScrapeProj\Scripts>easy_install psycopg2-2.6.1.win-amd64-py2.7-pg9
.4.4-release.exe
Processing psycopg2-2.6.1.win-amd64-py2.7-pg9.4.4-release.exe
psycopg2.tests.test_module: module references __file__
psycopg2.tests.test_types_basic: module references __file__
creating 'c:\users\e2sn7cy\appdata\local\temp\easy_install-sa7asq\psycopg2-2.6.1-py2.7-win32.egg' and
adding 'c:\users\e2sn7cy\appdata\local\temp\easy_install-sa7asq\psycopg2-2.6.1-py2.7-win32.egg.tmp'
to it
creating c:\users\e2sn7cy\envs\scrapeproj\lib\site-packages\psycopg2-2.6.1-py2.7-win32.egg
Extracting psycopg2-2.6.1-py2.7-win32.egg to c:\users\e2sn7cy\envs\scrapeproj\lib\site-packages
Adding psycopg2 2.6.1 to easy-install.pth file
Installed c:\users\e2sn7cy\envs\scrapeproj\lib\site-packages\psycopg2-2.6.1-py2.7-win32.egg
Processing dependencies for psycopg2==2.6.1
Finished processing dependencies for psycopg2==2.6.1
But when I again tried to install through pip I am getting a new message that its already there :
(ScrapeProj) C:\Users\e2sn7cy\Envs\ScrapeProj>pip install psycopg2
Requirement already satisfied (use --upgrade to upgrade): psycopg2 in c:\users\e2sn7cy\envs\scrapepro
j\lib\site-packages\psycopg2-2.6.1-py2.7-win32.egg
Then I thought ok I should try to import and then I got this error:
(ScrapeProj) C:\Users\e2sn7cy\Envs\ScrapeProj>python
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\e2sn7cy\Envs\ScrapeProj\lib\site-packages\psycopg2-2.6.1-py2.7-win32.egg\psycopg2\__
init__.py", line 50, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: DLL load failed: %1 is not a valid Win32 application.
Now I am not able to understand what to do. Thanks
Edit 1:
I have also added my PostgreSQL path to system path variables
path:C:\Program Files\PostgreSQL\9.4
path:C:\Program Files\PostgreSQL\9.4\bin
The correct answer was given by Craig Ringer, I am just adding this because I came to know that we should first check which version of 64 bit or 32bit Python installed on our system. If you have 32 bit version of Python and if you will try to install 64bit, then you will get the above error on importing.
For checking the version:
import platform
platform.architecture()
and you will get an output like this:
('32bit','WindowsPE')
Download the pre-packaged binary release of psycopg2 for Windows rather than compiling it yourself. It's a pain to compile on Windows. This is explained in the install documentation for psycopg2.
You might need to remove your half-installed attempts.
Choose the appropriate version from this page:
http://www.stickpeople.com/projects/python/win-psycopg/
Right click and select copy link address
Back at home, use easy_install <<Paste URL Here>>
Freeze your new requirements: pip freeze > requirements.txt
Now that psycopg is in PyPI (as of March 2017 it appears), just fire up a command prompt with administrator privileges and install like so:
python -m pip install -U pip
python -m pip install psycopg2
If you don't have pip installed, follow the instructions here to do that first:
https://stackoverflow.com/a/12476379/2540707
pip install psycopg2-binary
This worked for me
Step 1: Compilers Installation and configuration
https://wiki.python.org/moin/WindowsCompilers
Before do anything, install or upgrade the Setuptools Python package. It contain compatibility improvements and add automatic use of compilers:
pip install --upgrade setuptools
Step 2: Download and install PostgreSQL
Before you continue to install python packages inside you virtualenvs download postgres itself. It contains files that are needed when compiling the psycopg2 python package. Just use the PostgreSQL installer(version 10 for example).
Important: add the postgres C:\Program Files\PostgreSQL\10\bin folder to your path. It contains the .dll needed for psycopg2.
Step 3: Install psycopg2
pip install psycopg2
Tested on Windows 7 + python 3.7
The best/easiest way of installing packages (including this problematic psycopg2 ) for python on windows platform is to:
1) install anaconda and ...
2) use Anaconda Navigator GUI to install/update packages.
By this one avoids many inconsistencies and conflicts that occurs among different packages versions and installations.