Python cannot load cx_Oracle on Windows 7 - python

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.

Related

Problem Creating One File exe with pyinstaller and pysqlcipher

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.

ver.2 PyGreSQL ERROR: from _pg import * ImportError: DLL load failed: The specified module could not be found

I have the same problem that was discussed here, but I haven't credit to comment an answer so I start new question.
I have in PATH way to libpq.dll (C:\PostgreSql\lib) but it doesn't solve this problem.
Using Python 2.7.9 32-bit, PostgreSQL 8.4, Win 8
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import pg
File "C:\Python27\lib\site-packages\pg.py", line 21, in <module>
from _pg import *
ImportError: DLL load failed: The specified module could not be found.
I was also facing the same issue on Win 8. First time I had installed PostgreSQL in "C:\Program Files" and also set environment PATH to point to PostgreSQL folder. I was suspecting permission issue for "C:\Program Files" folder.
I was able to fix this issue by following the steps as mentioned below.
Uninstalled PostgreSQL
Re-installed PostgreSQL in "C:\PostgreSQL"
Note that I have not installed PostgreSQL into "C:\Program Files" folder this time
Set the environment PATH C:\PostgreSQL\9.4;C:\PostgreSQL\9.4\bin
Also ensure that _pg.pyd exist in C:\Python27\Lib\site-packages
I got the same error on Win10 with Python 3.5 and Python 3.8, both are 64bit, and Postresql 12, also 64bit. DLL locations "c:\Program Files\PostgreSQL\12\bin" and "c:\Program Files\PostgreSQL\psqlODBC\bin" were added to the PATH but it caused another error:
"from _pg import * ImportError: DLL load failed: The operating system cannot run %1."
Then I've checked what is going on with Process Monitor from Sysinternals and found that libpg.dll was looking for other DLLs. Finally, the following files:
libpq.dll
libssl-1_1-x64.dll
libcrypto-1_1-x64.dll
were copied from "c:\Program Files\PostgreSQL\12\bin" folder to:
"c:\Users...\AppData\Local\Programs\Python\Python35"
"c:\Users...\AppData\Local\Programs\Python\Python38"
folders and now "import pg" works fine in both versions of Python.
I faced just the same issue; just to try my chance before doing what you say, I changed the PATH environment variable: I directly specified the whole directory path of libpq.dll OF THE ODBC DRIVER OF PostgreSQL (there are other "libpqdll"s as well in other relevant directories) which takes place in my Windows10 here: C:\Program Files\PostgreSQL\psqlODBC\bin .. and the problem is solved.. before, my PATH specification was C:\Program Files\PostgreSQL and I think the driver looked for the dll files directly here and either 1) when couldnt find directly in this specific diretory (by disregarding the subdirectories), 2) or came cross other "libpq.dll"s which didnt work for the driver, it gave the error message.. so here I come to the conclusion that it looks for the dll file OF THE ODBC DRIVER in PATH env.varible, so I needed to specify in the PATH environment variable directly the directory of the PostgreSQL's ODBC driver I used, psqlODBC .. for future needs I wanted to write those details:)

clr.AddReference('example_file') - unable to find assembly

I am trying to add reference to "example_file.dll" using AddReference('example_file') method from clr module:
>>>import clr
>>>clr.AddReference('example_file')
and in result I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
System.IO.FileNotFoundException: Unable to find assembly 'example_file'.
at Python.Runtime.CLRModule.AddReference(String name)
All files are located in current working directory and sys.path looks like this:
>>> sys.path
['', 'C:\\Python27\\lib\\site-packages\\pip-1.2.1-py2.7.egg', 'C:\\Python27', 'C
:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27\\lib\\
site-packages', 'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\']
Additionally, in result of:
>>>clr.FindAssembly('example_file')
I get
u'example_file.dll
The problem arose from day to day. I am confused because it worked fine before - I don't know what could affect on this.
I am working with Windows 7 x64, python 2.73 and .Net framework 4
Not really enought info here, but there are (at least) 2 things to think about:
That you are not having additional Python related environment variables, already set. For example using a vritual environment or having Python pointing to some other place. Please check if PYTHONHOME and PYTHONPATH are set.
The the DLL you are trying to load have the same bit size as the Python interpreter. I.e . That if you have a 64-bit Python, you may need to load (but not always) a 64-bit DLL.
That the path specification is being read correctly, in your character set.
sys.path.append(r"C:\Program Files\SomeApi")

Embedded Python search modules in a wrong directory

I have installed Python 2.6.7 in $HOME/local of a machine which already has a default Python in /usr (I don't have admin access on this machine). The default Python is compiled in 32bits and my local installation is a 64bits. For some unknown reasons my local Python library (which I call as an embedded python interpreter from within a C program) search for the modules in the default (wrong) installation.
This is the result of "import random"
Traceback (most recent call last):
File "test.py", line 3, in <module>
import random
File "/tmp/work/mdorier1/local/lib/python2.6/random.py", line 45, in <module>
from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
ImportError: /usr/lib/python2.6/lib-dynload/math.so: wrong ELF class: ELFCLASS32
As you can see, the import statement correctly search "random.py" in the local installation of Python, but the import statement in random.py go search for math.so in the wrong location, which ends in an error since the default location has 32 bits modules.
I guessed there is a problem with an environment variable, and I tried
import sys
sys.path
to get
['/tmp/work/mdorier1/local/lib/python26.zip',
'/tmp/work/mdorier1/local/lib/python2.6',
'/tmp/work/mdorier1/local/lib/python2.6/plat-linux2',
'/tmp/work/mdorier1/local/lib/python2.6/lib-tk',
'/tmp/work/mdorier1/local/lib/python2.6/lib-old',
'/usr/lib/python2.6/lib-dynload',
'/tmp/work/mdorier1/local/lib/python2.6/site-packages']
I noticed that on of the paths indeed points to the default installation of Python. My questions thus are:
- Why this path shows up here, as the local installation has nothing to do with the default one?
- How do I change it (in a clean and permanent way)? This path should be the path to lib-dynload in the local installation instead.
Thanks
You probably don't have your Python's bin directory in the PATH variable before the system Python.
Or perhaps you simply compiled your Python incorrectly and did not use:
./configure --prefix=/tmp/work/mdorier1/local
so now it thinks that its files are somewhere else.

How can I get sqlite working on a shared hosting server?

I'm trying to run a python script using python 2.6.4. The hosting company has 2.4 installed so I compiled my own 2.6.4 on a similar server and then moved the files over into ~/opt/python. that part seems to be working fine.
anyhow, when I run the script below, I am getting ImportError: No module named _sqlite3 and I'm not sure what to do to fix this.
Most online threads mention that sqlite / sqlite3 is included in python 2.6 - so I'm not sure why this isn't working.
-jailshell-3.2$ ./pyDropboxValues.py
Traceback (most recent call last):
File "./pyDropboxValues.py", line 21, in
import sqlite3
File "/home/myAccount/opt/lib/python2.6/sqlite3/__init__.py", line 24, in
from dbapi2 import *
File "/home/myAccount/opt/lib/python2.6/sqlite3/dbapi2.py", line 27, in
from _sqlite3 import *
ImportError: No module named _sqlite3
I think I have everything set up right as far as the directory structure.
-jailshell-3.2$ find `pwd` -type d
/home/myAccount/opt
/home/myAccount/opt/bin
/home/myAccount/opt/include
/home/myAccount/opt/include/python2.6
/home/myAccount/opt/lib
/home/myAccount/opt/lib/python2.6
/home/myAccount/opt/lib/python2.6/distutils
/home/myAccount/opt/lib/python2.6/distutils/command
/home/myAccount/opt/lib/python2.6/distutils/tests
/home/myAccount/opt/lib/python2.6/compiler
/home/myAccount/opt/lib/python2.6/test
/home/myAccount/opt/lib/python2.6/test/decimaltestdata
/home/myAccount/opt/lib/python2.6/config
/home/myAccount/opt/lib/python2.6/json
/home/myAccount/opt/lib/python2.6/json/tests
/home/myAccount/opt/lib/python2.6/email
/home/myAccount/opt/lib/python2.6/email/test
/home/myAccount/opt/lib/python2.6/email/test/data
/home/myAccount/opt/lib/python2.6/email/mime
/home/myAccount/opt/lib/python2.6/lib2to3
/home/myAccount/opt/lib/python2.6/lib2to3/pgen2
/home/myAccount/opt/lib/python2.6/lib2to3/fixes
/home/myAccount/opt/lib/python2.6/lib2to3/tests
/home/myAccount/opt/lib/python2.6/xml
/home/myAccount/opt/lib/python2.6/xml/parsers
/home/myAccount/opt/lib/python2.6/xml/sax
/home/myAccount/opt/lib/python2.6/xml/etree
/home/myAccount/opt/lib/python2.6/xml/dom
/home/myAccount/opt/lib/python2.6/site-packages
/home/myAccount/opt/lib/python2.6/logging
/home/myAccount/opt/lib/python2.6/lib-dynload
/home/myAccount/opt/lib/python2.6/sqlite3
/home/myAccount/opt/lib/python2.6/sqlite3/test
/home/myAccount/opt/lib/python2.6/encodings
/home/myAccount/opt/lib/python2.6/wsgiref
/home/myAccount/opt/lib/python2.6/multiprocessing
/home/myAccount/opt/lib/python2.6/multiprocessing/dummy
/home/myAccount/opt/lib/python2.6/curses
/home/myAccount/opt/lib/python2.6/bsddb
/home/myAccount/opt/lib/python2.6/bsddb/test
/home/myAccount/opt/lib/python2.6/idlelib
/home/myAccount/opt/lib/python2.6/idlelib/Icons
/home/myAccount/opt/lib/python2.6/tmp
/home/myAccount/opt/lib/python2.6/lib-old
/home/myAccount/opt/lib/python2.6/lib-tk
/home/myAccount/opt/lib/python2.6/hotshot
/home/myAccount/opt/lib/python2.6/plat-linux2
/home/myAccount/opt/lib/python2.6/ctypes
/home/myAccount/opt/lib/python2.6/ctypes/test
/home/myAccount/opt/lib/python2.6/ctypes/macholib
/home/myAccount/opt/share
/home/myAccount/opt/share/man
/home/myAccount/opt/share/man/man1
And finally the contents of the sqlite3 directory:
-jailshell-3.2$ find `pwd`
/home/myAccount/opt/lib/python2.6/sqlite3
/home/myAccount/opt/lib/python2.6/sqlite3/__init__.pyo
/home/myAccount/opt/lib/python2.6/sqlite3/dump.pyc
/home/myAccount/opt/lib/python2.6/sqlite3/__init__.pyc
/home/myAccount/opt/lib/python2.6/sqlite3/dbapi2.pyo
/home/myAccount/opt/lib/python2.6/sqlite3/dbapi2.pyc
/home/myAccount/opt/lib/python2.6/sqlite3/dbapi2.py
/home/myAccount/opt/lib/python2.6/sqlite3/dump.pyo
/home/myAccount/opt/lib/python2.6/sqlite3/__init__.py
/home/myAccount/opt/lib/python2.6/sqlite3/dump.py
I feel like I need to add something into the sqlite3 directory - maybe sqlite3.so? But I don't know where to get that.
What am I doing wrong here? Please remember that I'm using a shared host so that means installing / compiling on another server and then copying the files over. Thanks! :)
Update
Just wanted to confirm that the answer from #samplebias did work out very well. I needed to have the dev package installed on the machine I was compiling from to get it to add in sqlite3.so and related files. Also found the link in the answer very helpful. Thanks #samplebias !
Python's build system uses a setup.py file to compile all of the native extensions, including sqlite3. It searches common operating system paths for the sqlite3 include and library dirs. If the sqlite3 development package is not installed Python will skip compiling the _sqlite3.so extension, but the pure Python portion of the sqlite3 package will still be installed.
You would need to have the operating system's sqlite3 development package installed when you compile Python and at runtime: sqlite3-devel on Centos, both libsqlite3-0 and libsqlite3-dev on Ubuntu.
Here's an example of the _sqlite3.so extension linkage on my Ubuntu system:
% ldd /usr/lib/python2.6/lib-dynload/_sqlite3.so | grep sqlite3
libsqlite3.so.0 => /usr/lib/libsqlite3.so.0 (0x00007f29ef3be000)
% dpkg -S /usr/lib/libsqlite3.so.0
libsqlite3-0: /usr/lib/libsqlite3.so.0
In general, the first thing to do is to ask your host. I seems a bit odd that SQLite is not installed (or installed properly). So they'll likely fix it quite fast if you ask them.
For python 2.4, you need sqlite and bindings, pysqlite 2 or aspw
None of the files listed in the sqlite folder is the _sqlite3.pyd Python shared library. Are you sure you compiled it when compiling Python? What does the build log say? I think there's a configure flag that needs to be passed.
Alternatively, just install pysqlite

Categories