This question already has answers here:
Python module "cx_Oracle" module could not be found
(4 answers)
Closed 4 years ago.
Im using python 2.7 and cx_oracle ( Windows x86 Installer (Oracle 10g, Python 2.7) ) and 'm having a bad time to set this simple example bellow to work:
import cx_Oracle
connection = cx_Oracle.connect('user/pass#someserver:port')
cursor = connection.cursor()
cursor.execute('select sysdate from dual')
for row in cursor:
print row
connection.close()
Error Message:
Traceback (most recent call last):
File "C:\ORACON.py", line 1, in <module>
import cx_Oracle
ImportError: DLL load failed: The specified module could not be found.
For now, what i have done was:
1) installed the cx_oracle binary;
2) downloaded instantclient_10_2 from oracle website and exported the path to environment;
Anyone know what im missing?
Thank you for your time on reading this.
I was able to solve this problem with the following steps:
Download instantclient-basic-win32-10.2.0.5 from Oracle Website
unzipped the into my c:\ with the name oraclient
Created the directory structure C:\oraclient\network\admin to add the TNSNAMES.ORA
Added the TNS_ADMIN env var pointing to C:\oraclient\network\admin
Added the ORACLE_HOME env var pointing to C:\oraclient\
After that i used the following code:
import cx_Oracle
con = cx_Oracle.connect('theuser', 'thepass', 'your DB alias on your TNSNAMES.ORA file ')
cur = con.cursor()
if cur.execute('select * from dual'):
print "finally, it works!!!"
else:
print "facepalm"
con.close()
I hope it helps someone.
Related
I'm trying to create a one file .exe to run on any windows machine but I've hit a problem with pysqlcipher. I've gone back to some basic code that just creates a simple database with a key, on my dev machine all works fine whether I use the python file or the compiled exe. I seem to be missing a library, path or both? I've tried adding vaious items using --add-data but have spent hours and made no progress. Here is the basic bit of python:-
from pysqlcipher3 import dbapi2 as sqlite
import os
import sys
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
conn = sqlite.connect('test.db')
c = conn.cursor()
c.execute("PRAGMA key='password'")
c.execute('''create table stocks (date text, trans text, symbol text, qty real, price real)''')
c.execute("""insert into stocks values ('2006-01-05','BUY','RHAT',100,35.14)""")
conn.commit()
c.close()
When I run the exe on a different windows 10 PC I get this error
Traceback (most recent call last):
File "testdb.py", line 1, in
File "c:\users\xxx\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
File "site-packages\pysqlcipher3-1.0.3-py3.8-win-amd64.egg\pysqlcipher3\dbapi2.py", line 33, in
ModuleNotFoundError: No module named 'pysqlcipher3._sqlite3'
[9248] Failed to execute script testdb
The error references the path on my dev PC and also refers to line 33 in dbapi2.py which is:-
from pysqlcipher3._sqlite3 import *
I have tried adding various files when running pyinstaller but I am making no progress, I'm sure its nothing simple but need help please.
I encountered a similar problem a while back and one thing that worked for me is adding the sqlite3 dll in the spec file of pyinstaller. It is also possible to add it from command line. I had tried many other things before such as re-creating a conda environment and rebuilding my python package but that did not work. In your spec file where you add the pyinstaller binaries please try something like.
binaries=[('C:\\Users\\myname\\newfolder\\sqlite3.dll','.')]
You can download the DLL from https://www.sqlite.org/download.html depending on windows or linux platform . Once you add the dll and recompile it should be fine. I think this error is primarily happening as dbapi2 requests the sqlite3 dll which in my case atlease was missing from the DLL folder. I just downloaded and added it in my spec file, but you can also try to add it to the hookspath or ENV folder.
I am trying to connect my python code to sql server. But there is an error which shows:
import pyodbc
ModuleNotFoundError: No module named 'pyodbc'
import pyodbc
cnxn = pyodbc.connect("Driver={SQL Server};"
"Server=localhost;"
"Database=SCMS2;"
"uid=sa;pwd=tazbirul94")
cursor = cnxn.cursor()
cursor.execute('SELECT * FROM Suppliers')
for row in cursor:
print('row = %r' % (row,))
Here is the pip freeze command:
This shows my odbc is present:
Here is the error
Here is my pycharm environment path
The pycharm is using a python from a .../venv/... folder while what you type in the cmd does not. Make sure to use the same python version for both.
One way would be to go to:
File->Settings->Project->Project Interpreter and change to the python that you have installed the module for
I'm trying to connect to AWS RDS using AWS Lambda. I installed PyMySQL to the directory and built the package with the code below and the libraries
import sys
import pymysql
def lambda_handler(event, context):
string=""
try:
connection = pymysql.connect(host='',
user='',
password='',
db='',
charset='',
cursorclass=pymysql.cursors.DictCursor)
cur = connection.cursor(pymysql.cursors.DictCursor)
cur.execute("select * from table")
for row in cur:
print(row['col'])
string+=row['col']
except Exception as e:
print("MySQL error: %s" % (e.args[0]))
return string
print(lambda_handler("",""))
In my machine, the code above works, but in AWS, it displays
MySQL error: module 'pymysql' has no attribute 'connect'
I checked that pymysql is only available in the directory that has the code, so I don't know why I'm not able to use the connect method.
Both Python versions are the same.
EDIT:
Traceback (most recent call last):
File "/var/task/lambda.py", line 7, in lambda_handler
connection = pymysql.connect(host='',
AttributeError: module 'pymysql' has no attribute 'connect'
Try zip -r package.zip *
I suspect you are zipping only the top level of the pymysql module, not the contents of its subdirectories
The AWS documentation for uploading to lambda is pretty poor.
First create a directory in your local machine eg: "package-dir"
Now install pymlsql to your created directory path "pip install pymlsql -t path/to/package-dir"
Paste you python script to the same dirctory
Select all the items inside the directory and create a zip file. Do not zip the directory itself this is very important
Upload the zip file in lambda and it should work
Also see that the handler name is "python_script_name.lambda_handler".
Eg: if your script file name is "lambda_function.py" then your handler should be "lambda_function.lambda_handler"
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
I installed python-2.7.amd64.msi and cx_Oracle-5.1.2-11g.win-amd64-py2.7.msi.
I've poked around a lot with PATH and PYTHONPATH environment variables but nothing has helped loading the cx_Oracle module. Currently PYTHONPATH is set to
C:\Python27\Lib\site-packages
My exceedingly basic program is
import sys
print sys.path
import cx_Oracle
conn_str = u'xxx/xxx#server/XXX'
conn = cx_Oracle.connect(conn_str)
c = conn.cursor()
c.execute(u'select * from table')
conn.close()
The program output is:
['C:\\Users\\terry\\IdeaProjects\\PythonScripts', 'C:\\Python27\\Lib\\site-packages', 'C:\\WINDOWS\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27']
Traceback (most recent call last):
File "OracleTest.py", line 4, in <module>
import cx_Oracle
ImportError: DLL load failed: The specified module could not be found.
I have also added the Registry entries as detailed here
This works fine on Linux so it seems I have something wrong with the windows setup. But I've pretty much run out of ideas.
This problem turned out (I think) to be that I had not set the ORACLE_HOME environment variable in Windows. This must point to your Oracle instantclient directory e.g. ORACLE_HOME=C:\instantclient_11_2
The "I think" part of the story is that even after setting that it did not help. I uninstalled cx_Oracle and reinstalled it from scratch. This time I also used the cx_Oracle.EXE from the python web site NOT the cx_Oracle.MSI file from sourceforge. In theory they would do the same thing. But in theory it wouldn't have taken me over a day to get the environment set up.