Opening a postgres connection in psycopg2 causes python to crash - python

I'm getting the following error message when I try to open up a connection to a postgres database. Perhaps it's related to OpenSSL, but I can't understand the error message. Can anyone help?
>>> import psycopg2
>>> conn = psycopg2.connect(host = '', port = , dbname
= '', user = '', password = '')
Auto configuration failed
12848:error:02001015:system library:fopen:Is a directory:.\crypto\bio\bss_file.c
:169:fopen('D:/Build/OpenSSL/openssl-1.0.1h-vc9-x64/ssl/openssl.cnf','rb')
12848:error:2006D002:BIO routines:BIO_new_file:system lib:.\crypto\bio\bss_file.
c:174:
12848:error:0E078002:configuration file routines:DEF_LOAD:system lib:.\crypto\co
nf\conf_def.c:199:

One problem that I can think of is that your installation may not have been linked/built properly to use openssl. If you haven't tried the packages listed in the docs yet, maybe you could give it a try.
When I look at the docs:
Microsoft Windows:
Jason Erickson maintains a packaged Windows port of Psycopg with installation executable. Download. Double click. Done.
So you could try to install it from there. Or you can try the pip-friendly windows-friendly (note: I didn't try it myself) psycopg2-windows package.

Related

cx_Oracle error. DPI-1047: Cannot locate a 64-bit Oracle Client library

I installed the library and when trying to access SQL in jupyter notebook with my credentials the following error appears:
DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "The specified module could not be found". See https://oracle.github.io/odpi/doc/installation.html#windows for help
The easiest solution is as follows:
Download 64-bit version of oracle instantClient from: https://www.oracle.com/database/technologies/instant-client/winx64-64-downloads.html
Copy the dll files in the instantclient directory to the python directory, as shown below
That is it!
The short answer is: cx_Oracle.init_oracle_client(lib_dir= r"c:\path_to_libraries")
Here are the steps that I followed to solve this same issue:
If you have not already installed cx_Oracle you can do so with the following command:
python -m pip install cx_Oracle --upgrade
The cx_Oracle documentation can be found here.
Use the following commands to verify that everything is installed and recognized:
import sqlalchemy as sqla
import pandas as pd
import cx_Oracle
# Test to see if it will print the version of sqlalchemy
print(sqla.__version__) # this returns 1.2.15 for me
# Test to see if the cx_Oracle is recognized
print(cx_Oracle.version) # this returns 8.0.1 for me
# This fails for me at this point but will succeed after the solution described below
cx_Oracle.clientversion()
At this point I get errors saying the libraries cannot be located. Here is the solution:
import os
import platform
# This is the path to the ORACLE client files
lib_dir = r"C:\put_your_path_here\instantclient-basic-windows.x64- 19.9.0.0.0dbru\instantclient_19_9"
# Diagnostic output to verify 64 bit arch and list files
print("ARCH:", platform.architecture())
print("FILES AT lib_dir:")
for name in os.listdir(lib_dir):
print(name)
Be sure to update the lib_dir path specific to your installation. If you have the correct path, you should see a listing of all the Oracle files like: (adrci.exe, oci.dll, oci.sym, etc). This is the location that Python needs to be able to find the Oracle drivers.
The current (Nov 2020) standard way for passing the location of the Oracle libraries for Windows is cx_Oracle.init_oracle_client(lib_dir= r"c:\path_to_libraries"). Here is an example:
lib_dir = r"C:\put_your_path_here\instantclient-basic-windows.x64- 19.9.0.0.0dbru\instantclient_19_9"
try:
cx_Oracle.init_oracle_client(lib_dir=lib_dir)
except Exception as err:
print("Error connecting: cx_Oracle.init_oracle_client()")
print(err);
sys.exit(1);
At this point I can run the following code without any errors:
# This works after passing the lib_dir path
cx_Oracle.clientversion() # For me it returns: (19, 9, 0, 0, 0)
DEPRECATED Here is how to update the PATH variable temporarily:
The following works, but using cx_Oracle.init_oracle_client(lib_dir= r"c:\path_to_libraries") is now the preferred way.
import os
# Manually append the location of the ORACLE libraries to the PATH variable
os.environ["PATH"] = lib_dir + ";" + os.environ["PATH"]
As per documentation accessed on cx_Oracle page.
Step 1: install cx_Oracle
python -m pip install cx_Oracle --upgrade
Step 2: Download and extract Oracle Basic Client
For Windows download and extract Oracle Basic Instatnt client instantclient-basic-windows.x64-19.9.0.0.0dbru.zip file.
Step 3: Inform cx_Oracle module about the Instatnt Client location.
If you stick to documentation and extract them in c:\oracle folder then your script may look like this.
import cx_Oracle
cx_Oracle.init_oracle_client(lib_dir=r"C:\oracle\instantclient_19_9")
Now you will be free from the error.
I faced this error in Anconda Spyder.
This is how I fixed it.
Get the basic package instantClient from: https://www.oracle.com/database/technologies/instant-client/winx64-64-downloads.html
Extract and copy all the *.dll files and paste it to Anaconda3 folder where you have python.exe.
For MAC
After you do : python -m pip install cx_Oracle --upgrade
try:
import cx_Oracle
# Test to see if the cx_Oracle is recognized
print(cx_Oracle.version) # this returns 8.0.1 for me
# This fails for me at this point but will succeed after the solution described below
cx_Oracle.clientversion()
if you encounter issues like :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "dlopen(libclntsh.dylib, 1): image not found". See https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html for help
Follow the step highlighted here: https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html#manual-installation
Manual Installation
Download the Basic 64-bit DMG from Oracle.
(https://www.oracle.com/database/technologies/instant-client/macos-intel-x86-downloads.html)
Mount the dng
Open the mounted dng, and run ./install_ic.sh (via terminal)
e.g. : cd /Volumes/instantclient-basic-macos.x64-19.8.0.0.0dbru/ && ./install_ic.sh
In Finder, eject the mounted Instant Client package.
Re-run the oracle connection.
import cx_Oracle
cx_Oracle.init_oracle_client(lib_dir="/Users/priyank.pathak/Downloads/instantclient_19_8")
## cx_Oracle.init_oracle_client(lib_dir="/Users/your_username/Downloads/instantclient_19_8")
cx_Oracle.clientversion()
Make sure you have installed the correct oracle client (you can find the oracle client package from here "https://www.oracle.com/in/database/technologies/instant-client/winx64-64-downloads.html" and add the downloaded folder inside the folder where python is installed and then add this location (location of your client package folder) to system's environment variable.
Hope that will work.
I suggest you to first check the compatibility of your OS, Python and Oracle Instant Client architecture:
import platform
platform.architecture()
Then, I definitely advise you to set the Oracle Instant Client inside your jupyter notebook:
import os
os.environ["PATH"] = "Complete Location of Instant Client Folder" + ";" + os.environ["PATH"]
Probably you have installed cx_Oracle Python library. In order to execute the DB connectivity through jupyter notebook, you need to install the Oracle Client and that is what missing in your scenario. Please follow the steps from below link and and install Oracle Client and this will solve your problem:
https://oracle.github.io/odpi/doc/installation.html#windows
I went into the same problem but on Arch Linux, running in NodeJS.
Some of the answers here helped me in searching for the official installation guide.
Then I followed these steps and they worked for me:
https://oracle.github.io/node-oracledb/INSTALL.html#instzip , hope it could help someone else too.
Download Instant Client Basic Light Package (ZIP) (Linux)
https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html
Create folder and unzip into it
mkdir /opt/oracle
unzip ~/Downloads/instantclient-basiclite-linux.x64-21.4.0.0.0dbru.zip -d /opt/oracle/
Then, according to the docs, execute these commands below:
If there is no other Oracle software on the machine that will be impacted, then permanently add Instant Client to the run-time link path. For example, if the Basic package unzipped to /opt/oracle/instantclient_19_11, then run the following using sudo or as the root user
sudo sh -c "echo /opt/oracle/instantclient_19_11 > /etc/ld.so.conf.d/oracle-instantclient.conf"
sudo ldconfig
OR
Alternatively, every shell running Node.js will need to have the link path set:
export LD_LIBRARY_PATH=/opt/oracle/instantclient_19_11:$LD_LIBRARY_PATH
This might help someone:
I also had the same error and I read through the installation documentation https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html (I was using a zip for installation so I focused on section: Oracle Instant Client Zip Files)
And I was missing a "yum install -y libaio" (used 'centos:7' base image) after unzipping the oracle client to /opt/oracle and also added the following envs:
ENV ORACLE_HOME /opt/oracle/instantclient_19_3
ENV DYLD_LIBRARY_PATH /opt/oracle/instantclient_19_3
ENV LD_LIBRARY_PATH /opt/oracle/instantclient_19_3
ENV TNS_ADMIN /opt/oracle/instantclient_19_3
I just installed Sql Developer from Oracle and ran it for the first time, and the install added what was needed and I was able to get through that error. This was on windows. Install below, just extract it when it installs and run the .exe in the main folder. Much simpler than the other solutions!
https://www.oracle.com/database/sqldeveloper/technologies/download/
You just need to put the Oracle Instant Client folder in your PATH environment variable. Remember to restart whatever application that needs the instant client after updating the PATH variable.
If you installed the instant client at: c:\oracle\instantclient_21_8, simply put this folder in your PATH environment variable.
Copying the instant client files to Python folder is a very bad idea, since if you have other python versions installed in the future you will have to remember duplicating de instant client files. If you upgrade the instant client, you will need to keep these duplicated files updated too.

pyzabbix Zabbix sender

I am looking to send a few items to zabbix using Zabbix-Sender function of pyzabbix. As a test I am running the below code -
from pyzabbix import ZabbixMetric, ZabbixSender, ZabbixResponse
metrics = []
m= ZabbixMetric('mme01', 'TEST', 20)
metrics.append(m)
ZabbixSender('10.46.224.5').send(metrics)
I made this snippet after reading the document - https://py-zabbix.readthedocs.io/en/latest/sender.html
When I run the snippet I get the error -
AttributeError: 'ConnectionRefusedError' object has no attribute 'msg'
I have verified IP connectivity
Can Anyone help ?
there is a mess in modules names.
it seems you call 'other' pyzabbix module who has not needed methods.
first, remove all zabbix-related modules:
pip list | grep zabbix; pip uninstall ...
and then install pyzabbix:
pip install py-zabbix.
this should help.
UPDATED:
I dug deeper and figured out that the AttributeError: 'ConnectionRefusedError' object has no attribute 'msg' exception caused by old module version bug, which has been fixed here. update it with pip or manually.
new bug I have faced is [Errno 8] nodename nor servname provided, or not known caused by socket lib and can be fixed pointing to zabbix server/proxy IP address instead of DNS name
I decided to bypass the module - pyzabbix and decided to use the raw zabbix sender utility. Works great.
For other folks my solution relies on a file with values that need to be sent to zabbix.
Sample file -
"mme01" TEST 1544729668 44
The use the utility -
/opt/zabbix-proxy/bin/zabbix_sender -vv -z 10.43.X.X -T -i mme_file.txt
Replace with path of your zabbix_sender and the zabbix server IP.
I had the same issue on some machines of mine. At that time I refactored my code to use zabbix_sender and it worked great.
After some time I found out the issue was related to pip repository corruption. I ended up reinstalling all the packages with this option:
pip install --ignore-installed <package>
apparently the issue was fixed, but I still have no idea why it occurred on some manchines and not in anothers.

speedtest-cli works in console, but not as script

I am trying to use the speedtest-cli api. Copied part of the code from official wiki (and removed unused stuff):
import speedtest
s = speedtest.Speedtest()
s.get_best_server()
s.download()
In python console I get everything ok:
>>> import speedtest
>>> s = speedtest.Speedtest()
>>> s.get_best_server()
{HIDDEN}
>>> s.download()
37257579.09084724
But when I create .py file and run it I get:
AttributeError: module 'speedtest' has no attribute 'SpeedTest'
Thanks
As mentioned in the comments, you have a file with the same name and it is conflicting with the import. Since you have moved the file, restarting the console should work.
The code below will also extract the results into a dictionary and make it possible to access the results.
import speedtest
s = speedtest.Speedtest()
s.get_best_server()
s.download()
s.upload()
res = s.results.dict()
print(res["download"], res["upload"], res["ping"])
I faced the same issue because I had installed both speedtest and speedtest-cli.
Using pip uninstall speedtest worked for me.
I faced the same issue because the name of my file was speedtest. When I change the name to something new. It works fine for me.
import speedtest
wifi = speedtest.Speedtest()
print("Wifi Download Speed is ", wifi.download())
print("Wifi Upload Speed is ", wifi.upload())
Ok Here is the Solution to this Problem
1-uninstall speedtest and speedtest-cli if you have both installed
2- install only speedtest-cli pip install speedtest-cli this will install the package in the main python environment
3-CTRL+P select interpreter then select your main python not any environment
4-it will work fine
Reason for this error
(you install the package in your main python environment and you usually open your IDE and run a virtual environment)
Try checking speedtest is imported properly
import speedtest
print(dir(speedtest))
it should display properties of speedtest

not able to work with cx-oracle

I am new to python and oracle, I have written the code for the connection to oracle database 11g but it gives an error:
import cx_Oracle
con=cx_Oracle.connect('sys/Satyam123#localhost/xe')
con.close(
)
It gives the following error in pycharm:
C:\Users\DELL\venv\module2\Scripts\python.exe
C:/Users/DELL/Desktop/PYTHON/module2/check.py Traceback (most recent
call last): File "C:/Users/DELL/Desktop/PYTHON/module2/check.py",
line 2, in
con=cx_Oracle.connect('sys/Satyam123#localhost/xe') cx_Oracle.DatabaseError: DPI-1047: 32-bit Oracle Client library cannot
be loaded: "The specified module could not be found". See
https://oracle.github.io/odpi/doc/installation.html#windows for help
Please download and install Oracle Client. (There are several editions of Oracle Client, but the instant one will do):
http://download.oracle.com/otn/nt/instantclient/122010/instantclient-basic-nt-12.2.0.1.0.zip
Once it is installed, the cx_Oracle python module will look for the Oracle libs (OCI) and load them.
I had the same issue. Please follow the link https://oracle.github.io/odpi/doc/installation.html and install Oracle Instant Client 64-bit or 32-bit as per your system version. Once this is installed python would automatically be able to find Oracle Client libraries and you can successfully connect to the database.
It seems like,there is issue related to PATH.You can try to install package with the IDE terminal.In your case just try to install a package with pycharm terminal.
After that try to execute below script:
import cx_Oracle
import db_config
user="test"
pw="test"
dsn="localhost:port/TEST" #here TEST is service id
con = cx_Oracle.connect(user, pw, dsn)
cur = con.cursor()
cur.execute("select * from test_table")
res = cur.fetchall()
for row in res:
print(row)
Still having an issue then you can refer :
[https://oracle.github.io/python-cx_Oracle/samples/tutorial/Python-and-Oracle-Database-Scripting-for-the-Future.html]

Python ldap AttributeError

I have an python error AttributeError: 'module' object has no attribute 'initialize'
I am running Python 2.6.2 on Solaris 10 UNIX and recently installed the pythonldap 2.3.9. The script is very basic, only has these 2 lines. Can anyone tell me why?? Traceback error below.
#!/usr/local/bin/python
import ldap, sys
con = ldap.initialize('ldap://localhost')
Traceback (most recent call last):
File "./myldap.py", line 5, in
con = ldap.initialize('ldap://localhost')
AttributeError: 'module' object has no attribute 'initialize'
Regards,
Jenny
Did you name a file in the current directory ldap.py that is shadowing the one that you want?
Many people are giving much more complicated solutions... Simply put, the pip installation of the ldap module doesn't work. You need to install the python-ldap package from apt or yum. Note that the deb package is now named python3-ldap, after the deprecation of python 2.
An easy way to tell if the ldap you're importing is the right one is to print ldap.__file__, which prints the full path to the module file (usually a '.pyc'). If it's not the one installed in the location you are expecting, this is your problem, as Mike Graham suggested.
I did the ldap connection successfully. How to go:
1.I have python v 3.7.2
2.Install python-ldap:For this I tried "pip install python-ldap" but it not worked
for me on windows machine so I use the alternate below.
3.For installing ldap go here:https://www.lfd.uci.edu/~gohlke/pythonlibs/#python-ldap
and download python_ldap‑3.1.0‑cp37‑cp37m‑win_amd64.whl
4.Now move to the download directory and run "pip install
python_ldap‑3.1.0‑cp37‑cp37m‑win_amd64.whl"
Now open python shell and check "import ldap" if you are getting no error means you are done.
This is the sample code:
#Resource of code :https://gist.github.com/ibeex/1288159
import ldap
def check_credentials(username, password):
"""Verifies credentials for username and password.
Returns None on success or a string describing the error on failure
# Adapt to your needs
"""
LDAP_SERVER = 'xxx'
# fully qualified AD user name
LDAP_USERNAME = '%s#spi.com' % username
# your password
LDAP_PASSWORD = password
base_dn = 'DC=spi,DC=com'
ldap_filter = 'userPrincipalName=%s#spi.com' % username
attrs = ['memberOf']
try:
# build a client
ldap_client = ldap.initialize(LDAP_SERVER)
# perform a synchronous bind
ldap_client.set_option(ldap.OPT_REFERRALS,0)
ldap_client.simple_bind_s(LDAP_USERNAME, LDAP_PASSWORD)
except ldap.INVALID_CREDENTIALS:
#print("wron")
ldap_client.unbind()
return 'Wrong username or password'
except ldap.SERVER_DOWN:
#print("down")
return 'AD server not awailable'
# all is well
# get all user groups and store it in cerrypy session for future use
ab = str(ldap_client.search_s(base_dn,
ldap.SCOPE_SUBTREE, ldap_filter, attrs)[0][1]['memberOf'])
#print("ab"+ab)
ldap_client.unbind()
return 'success'
if __name__ == "__main__":
u="chirag"
p="secred"
print(check_credentials(u,p))
You can get that error if you're somehow picking up the "ldap.py" from sos/plugins/ instead of the ldap package itself. Make sure the "python-ldap" package is actually installed...
I guess you have installed "pip install ldap"! In this module "initialize" or "open" are not present.
Uninstall that "ldap" by "pip uninstall ldap" and then try "yum install python-ldap". And run the same code.
Print the "con".
open does not exist anymore in version 3.x of python-ldap.
I fixed it by forcing the installation of an older version :
pip install python-ldap==2.4.13

Categories

Resources