Connect Python to Teradata in mac with pyodbc - python

I successfully installed pyodbc module for python 2.7. However, when input the following code to connect to teradata,
import pyodbc
conn = pyodbc.connect('DRIVER={Teradata};DBCNAME=<tdwc>;UID=<UID>;PWD=<UID>;QUIETMODE=YES;')
I got the following error;
Traceback (most recent call last):
File "", line 1, in
pyodbc.connect('DRIVER={Teradata};DBCNAME=;UID=;PWD=;QUIETMODE=YES;')
Error: ('00000', '[00000] [iODBC][Driver Manager]dlopen(/Library/Application Support/teradata/client/ODBC/lib/tdata.dylib, 6): Library not loaded: libtdparse.dylib\n Referenced from: /Library/Application Support/teradata/client/ODBC/lib/tdata.dylib\n Reason: image not found (0) (SQLDriverConnect)')
What should I do to have this fixed? Any ideas?

Basically pyodbc is not straight forward and gives good enough debugging time for developers.
Follow the below steps,
You might have done them already, then just verify
Install iodbc for mac http://www.iodbc.org/dataspace/iodbc/wiki/iODBC/Downloads
Install Teradata ODBC Driver for Mac OS X, http://downloads.teradata.com/download/connectivity/teradata-odbc-driver-for-mac-os-x
Also install unixodbc for mac, “brew install unixodbc”
Download pyodbc source and change the setup.py file as below,
elif sys.platform == 'darwin':
# OS/X now ships with iODBC.
settings['libraries'].append('iodbc')
settings['libraries'].append('odbc')
you will be adding the last line of including “odbc” for build
Build and install pyodbc
Setup the traditional LD_LIBRARY_PATH for mac as below ( I used TD version 15.00, you have to point to the one as you installed)
export DYLD_LIBRARY_PATH=/Library/Application\
Support/teradata/client/ODBC/lib:/Library/Application\
Support/teradata/client/15.00/lib/
Export ODBCINI path
export ODBCINI=/Library/Application\
Support/teradata/client/15.00/odbc/odbc.ini
Now pyodbc cursor happily will fetch record sets for you

Related

Change where pyodbc expects libodbc.2.dylib to live (changing default odbc file locations)

When importing pyodbc
❯ python
>>> import pyodbc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/pcosta/Documents/test/myenv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so, 2): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
Referenced from: /Users/pcosta/Documents/test/myenv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so
Reason: image not found
I know why this is happening, as I don't have libodbc.2.dylib in the expected location. The reason is I do not have permission to write to /usr/local/, so I have Homebrew installing into ~/.brew. This mostly works fine. I am even able to get both tsql and isql working as expected by following the steps outlined here: https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Mac-OSX.
So I do have libodbc.2.dylib, it's just that it lives in /Users/pcosta/.brew/lib, not /usr/local/opt/unixodbc/lib.
The main questions is can I get pyodbc to look for libodbc.2.dylib (and other associated files) in another directory?
I have all the files needed and have configured them correctly, I just need to repoint pyodbc somehow.
Thanks!
Thanks in part to guidance from this GitHub issue I was able to come to some solution.
Assuming you have brew install unixodbc:
Add the following paths (to .zshrc, .bashrc, or .bash_profile):
export LDFLAGS="-L/Users/pcosta/homebrew/opt/unixodbc/lib $LDFLAGS"
export CPPFLAGS="-I/Users/pcosta/homebrew/opt/unixodbc/include $CPPFLAGS"
export PKG_CONFIG_PATH="/Users/pcosta/homebrew/opt/unixodbc/lib/pkgconfig $PKG_CONFIG_PATH"
Run pip install --no-binary pyodbc pyodbc to bypass the binary and build yourself

pyhive, sqlalchemy can not connect to hadoop sandbox

I have installed,
pip install thrift
pip install PyHive
pip install thrift-sasl
and
since pip install sasl failed I downloaded sasl‑0.2.1‑cp27‑cp27m‑win_amd64.whl file and installed it in my Windows 8.1 PC.
Then I wrote this code,
from pyhive import hive
cursor = hive.connect('192.168.1.232', port=10000, auth='NONE')
cursor.execute('SELECT * from sample_07 LIMIT 5',async=True)
print cursor.fetchall()
this gives the error:
Traceback (most recent call last):
File "C:/DigInEngine/scripts/UserManagementService/fd.py", line 37, in <module>
cursor = hive.connect('192.168.1.232', port=10000, auth = 'NONE')
File "C:\Python27\lib\site-packages\pyhive\hive.py", line 63, in connect
return Connection(*args, **kwargs)
File "C:\Python27\lib\site-packages\pyhive\hive.py", line 104, in __init__
self._transport.open()
File "C:\Python27\lib\site-packages\thrift_sasl\__init__.py", line 72, in open
message=("Could not start SASL: %s" % self.sasl.getError()))
thrift.transport.TTransport.TTransportException: Could not start SASL: Error in sasl_client_start (-4) SASL(-4): no mechanism available: Unable to find a callback: 2
and this code gives,
from sqlalchemy import create_engine
engine = create_engine('hive://192.168.1.232:10000/default')
try:
connection = engine.connect()
except Exception, err:
print err
result = connection.execute('select * from sample_07;')
engine.dispose()
this error,
Could not start SASL: Error in sasl_client_start (-4) SASL(-4): no
mechanism available: Unable to find a callback: 2
I have downloaded Hortonworks sandbox from here and use it in a separate server.
NOTE: I went through this as well but the accepted answer is not working for me, because importing ThriftHive from hive gives Import error although I have pip installed hive. So I decided to use pyhive or sqlalchemy
How can I connect to hive and execute a query easily?
Here are steps to build SASL on Windows, but your mileage may vary: A lot of this depends on your particular system's paths and available libraries.
Please also note that these instructions are specific to Python 2.7 (which I see you are using from the paths in your question).
The high-level overview is that you're installing this project: https://github.com/cyrusimap/cyrus-sasl. In order to do that, you have to use the legacy C++ compiler that was used to build Python 2.7. There are a couple of other steps to getting this to work.
Pre-build Steps:
Install Microsoft Visual C++ Compiler for Python 2.7. Use the default installation paths - take note of where it got installed for the next 2 steps (2 options are included in the list below)
Copy this file to whichever of the include locations is appropriate for your install
Make a unistd.h file from this answer in the same include directory
Build steps:
git clone https://github.com/cyrusimap/cyrus-sasl
Open the "VS2013 x64 Native Tools Command Prompt" that's installed with the Compiler from step 1
Change directory to the directory created by step 4, then the lib sub-directory
nmake /f ntmakefile STATIC=no prefix=C:\sasl64
nmake /f ntmakefile prefix=C:\sasl64 STATIC=no install see note below
copy /B C:\sasl64\lib\libsasl.lib /B C:\sasl64\lib\sasl2.lib
pip install thrift_sasl --global-option=build_ext \
--global-option=-IC:\\sasl64\\include \
--global-option=-LC:\\sasl64\\lib
'Include' locations:
"C:\Program Files (x86)\Common Files\Microsoft\Visual C++ for Python\9.0\VC\include\stdint.h"
"%USERPROFILE%\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\include"
Here's a reference to these same steps, with some additional annotations and explanations: http://java2developer.blogspot.co.uk/2016/08/making-impala-connection-from-python-on.html.
Note
The referenced instructions also executed step (8) in the include and win32\include sub-directories, you may have to do that as well.
While using pyhive no authentication can be passed as auth="NOSASL", instead of "None", so your code should look like this:
from pyhive import hive
cursor = hive.connect('192.168.1.232', port=10000, auth='NOSASL')
cursor.execute('SELECT * from sample_07 LIMIT 5',async=True)
print cursor.fetchall()

Twittersearch module is not getting imported to api file

I am trying to make twitter search api everything was working fine but suddenly twittersearch() module is not getting imported. I am using python 3.4.2 with windows 8.1 64-bit. I have tried easy_install twittersearch it successfully installs packages and everything is fine but when I run this code
from TwitterSearch import *
import pyodbc
cnxn = pyodbc.connect(driver='{SQL Server}', server='localhost', database='capstone',trusted_connection='yes')
cursor = cnxn.cursor()
cursor.execute("select word from dbo.search where sl in (select max(sl) from dbo.search)")
for row in cursor.fetchall():
print (row)
print("This is positive data ")
term = row[0]
try:
tso=TwitterSearchOrder()
tso.set_keywords([term])
tso.set_language('en')
When i execute this it shows errors like below
Traceback (most recent call last):
File "C:\Python34\search_test.py", line 18, in <module>
tso=TwitterSearchOrder()
NameError: name 'TwitterSearchOrder' is not defined
But it is actually there I don't know why it is not recognizing the module. till 2 days back it was running successfully with IDLE but not in commandprompt and I have reinstalled the python and added all tools, now it is showing this error in both IDLE and command prompt
TIA
Make sure that TwitterSearch is in your lib\site-packages path. If it is you must've installed it right. If not, a simple python -m pip install TwitterSearch from command prompt will do.
However, there seems to be an issue(bug) with TwitterSearch wrt its functions. You might consider using an alternate API in the meantime. I would suggest using Tweepy

Why can't macports pyodbc find a data source name or driver on MacOSX (Snow Leopard)?

I've installed the mac ports package for py27-odbc on Snow Leopard. Since OSX no longer supplies the ODBC Administrator application, I've installed the OpenLink ODBC manager. After installing, I created a DSN for my remote database, and verified that the configuration works using the iodbctest utility. I then attempt to connect to the database using pyodbc:
import pyodbc
cnxn = pyodbc.connect('DSN=my-dsn-name;UID=joe;PWD=secret')
and I get the following lovely error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name
not found, and no default driver specified (0) (SQLDriverConnectW)')
Why can't pyodbc find the ODBC drivers and/or my DSN configurations?
Updated (since I can't answer my own question yet)
It appears as if the system stores ODBC configuration data in /Library/ODBC, but the Mac Ports stores configuration in /opt/local/etc. This explains why iodbctest works, but the pyodbc chokes. The cleanest fix seems to be simply symlinking from the Mac Ports location to the system location, so that a common set of configuration files will work for any tool, regardless of where it's looking for this information:
sudo ln -s /Library/ODBC/odbc.ini /opt/local/etc/odbc.ini
sudo ln -s /Library/ODBC/odbcinst.ini /opt/local/etc/odbcinst.ini

psycopg2 on cygwin: no such process

I am trying to install postgrepsql to cygwin on a windows 7 machine and want it to work with django.
After built and installed postgrepsql in cygwin, I built and installed psycopg2 in cygwin as well and got no error, but when use it in python with cygwin, I got the "no such process" error:
import psycopg2
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python2.5/site-packages/psycopg2/init.py", line 60, in
from _psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: No such process
any clues?
Thanks!
Jerry
The no such process error comes about because psycopg2 is looking for cygpq.dll and can't find it.
When you install postgres from source, it puts this .dll in /usr/local/pgsql/lib/
Evidently, cygpq.dll is resolved by the win32 process loader, NOT cygwin or python, so you have to put it somewhere on your Windows path. A good spot is /usr/local/bin.
cp /usr/local/pgsql/lib/cygpq.dll /usr/local/bin/
In my case, I had to reinstall libpq5.
Why? There is native psycopg2 for Win.
you could try these:
http://www.stickpeople.com/projects/python/win-psycopg/

Categories