cx_Oracle error: ImproperlyConfigured

3,691 views
Skip to first unread message

kamalp...@gmail.com

unread,
Apr 21, 2011, 6:07:20 AM4/21/11
to Django users
Hi,

I have installed cx_Oracle module in one of my Solaris box and then
trying to create a django page to read data from oracle db. But I am
getting following errors.


mod_wsgi (pid=2600): Exception occurred processing WSGI script '/opt/
www/ui/foo/web/app.wsgi'.
Traceback (most recent call last):
File "/opt/www/ui/foo/web/app.wsgi", line 30, in application
return _application(environ, start_response)
File "/usr/local/packages/python/2.6.6/lib/python2.6/site-packages/
django/core/handlers/wsgi.py", line 230, in __call__
self.load_middleware()
File "/usr/local/packages/python/2.6.6/lib/python2.6/site-packages/
django/core/handlers/base.py", line 42, in load_middleware
raise exceptions.ImproperlyConfigured('Error importing middleware %s:
"%s"' % (mw_module, e))
ImproperlyConfigured: Error importing middleware web.web.framework:
"ld.so.1: httpd: fatal: libclntsh.so.11.1: open failed: No such file
or directory"


I have tried from python shell and i can able to import cx_Oracle
module as follows:

bash-3.00$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib
bash-3.00$ python
Python 2.4.4 (#1, Jan 10 2007, 01:25:01) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>>

But from the web I am getting the ImproperlyConfigured: configured
error. Can someone please help me to resolve this issue.

FYI,

I have followed this link to install cx_oracle module.
http://agiletesting.blogspot.com/2005/05/installing-and-using-cxoracle-on-unix.html

Please let me know if you need more info.

Thanks,
Kamal

David Markey

unread,
Apr 21, 2011, 6:57:31 AM4/21/11
to django...@googlegroups.com
At the top of the WSGI script, set the LD_LIBRARY_PATH environment variable.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


kamal sharma

unread,
Apr 21, 2011, 10:18:40 AM4/21/11
to django...@googlegroups.com, ad...@dmarkey.com
Thanks David for quick response. I have already tried that option, but it does not work.

Here is the diff which i have added.


Index: app.wsgi
===================================================================
+os.environ["LD_LIBRARY_PATH"] = "/opt/app/oracle/products/11.2.0/lib"

 # figure out where on disk this gnatatui install is hiding
 base_path = __file__.split('/web/app.wsgi')[0]

Am i missing something?

Thanks,
Kamal

brad

unread,
Apr 21, 2011, 10:53:38 AM4/21/11
to django...@googlegroups.com, ad...@dmarkey.com
This may be related to Oracle's shared libraries not being in the path recognized by your web server. I created hard links to the Oracle shared libraries in /user/local/lib to get cx_oracle working.

I have a blog post that outlines what I did, here:
http://bradmontgomery.net/blog/gahhh-django-virtualenv-and-cx_oracle/

The comment by Graham Dumpleton is worth reading, as he mentions a few other techniques to make this work.

kamal sharma

unread,
Apr 21, 2011, 11:39:13 AM4/21/11
to django...@googlegroups.com, ad...@dmarkey.com
Here is the error I am getting now:

cd /usr/local/lib

/usr/local/lib> sudo ln /opt/app/oracle/products/11.2.0/lib/libclntsh.so.10.1
ln: ./libclntsh.so.10.1 is on a different file system

/usr/local/lib> cd /usr/lib/
/usr/lib> sudo ln /opt/app/oracle/products/11.2.0/lib/libclntsh.so.10.1
ln: ./libclntsh.so.10.1 is on a different file system

Also set this in .cshrc file

setenv LD_LIBRARY_PATH "$ORACLE_HOME/lib32:$ORACLE_HOME/lib"
setenv LD_LIBRARY_PATH /usr/X11R6/lib:/usr/local/lib

echo $LD_LIBRARY_PATH
/usr/X11R6/lib:/usr/local/lib
echo $ORACLE_HOME/
/opt/app/oracle/products/11.2.0/


Thanks

--

Ian

unread,
Apr 21, 2011, 11:51:49 AM4/21/11
to Django users
On Apr 21, 9:39 am, kamal sharma <kamalp.sha...@gmail.com> wrote:
> Here is the error I am getting now:
>
> cd /usr/local/lib
>
> /usr/local/lib> sudo ln
> /opt/app/oracle/products/11.2.0/lib/libclntsh.so.10.1
> ln: ./libclntsh.so.10.1 is on a different file system
>
> /usr/local/lib> cd /usr/lib/
> /usr/lib> sudo ln /opt/app/oracle/products/11.2.0/lib/libclntsh.so.10.1
> ln: ./libclntsh.so.10.1 is on a different file system

Right, it's not possible to create hard links across file systems.
Create a soft link instead using "ln -s" (soft links are usually
preferable anyway).

> Also set this in .cshrc file
>
> setenv LD_LIBRARY_PATH "$ORACLE_HOME/lib32:$ORACLE_HOME/lib"
> setenv LD_LIBRARY_PATH /usr/X11R6/lib:/usr/local/lib
>
> echo $LD_LIBRARY_PATH
> /usr/X11R6/lib:/usr/local/lib
> echo $ORACLE_HOME/
> /opt/app/oracle/products/11.2.0/

Is this in your .cshrc file or the WSGI user's .cshrc? And are you
sure it's using csh and not bash?

kamal sharma

unread,
Apr 21, 2011, 12:01:57 PM4/21/11
to django...@googlegroups.com, ad...@dmarkey.com
After creating soft link, still it is giving the same error. Do i need to create soft link for any other files? My response are inline with KS:

On Thu, Apr 21, 2011 at 9:21 PM, Ian <ian.g...@gmail.com> wrote:
On Apr 21, 9:39 am, kamal sharma <kamalp.sha...@gmail.com> wrote:
> Here is the error I am getting now:
>
> cd /usr/local/lib
>
> /usr/local/lib> sudo ln
> /opt/app/oracle/products/11.2.0/lib/libclntsh.so.10.1
> ln: ./libclntsh.so.10.1 is on a different file system
>
> /usr/local/lib> cd /usr/lib/
> /usr/lib> sudo ln /opt/app/oracle/products/11.2.0/lib/libclntsh.so.10.1
> ln: ./libclntsh.so.10.1 is on a different file system

Right, it's not possible to create hard links across file systems.
Create a soft link instead using "ln -s" (soft links are usually
preferable anyway).

KS: Ok i have created soft link and it is fine.
 sudo ln -s /opt/app/oracle/products/11.2.0/lib/libclntsh.so.10.1 

> Also set this in .cshrc file
>
> setenv LD_LIBRARY_PATH "$ORACLE_HOME/lib32:$ORACLE_HOME/lib"
> setenv LD_LIBRARY_PATH /usr/X11R6/lib:/usr/local/lib
>
> echo $LD_LIBRARY_PATH
> /usr/X11R6/lib:/usr/local/lib
> echo $ORACLE_HOME/
> /opt/app/oracle/products/11.2.0/

Is this in your .cshrc file or the WSGI user's .cshrc?  And are you
sure it's using csh and not bash?

KS: Yes it  .cshrc file.

kamal sharma

unread,
Apr 21, 2011, 1:03:21 PM4/21/11
to django...@googlegroups.com, ad...@dmarkey.com
Thanks a lot. Now that issue is resolved after i execute below command.

sudo ln -s /opt/app/oracle/products/11.2.0/lib/libclntsh.so.11.1

But now i am getting databse error. Any pointer please?

Exception Value:
Error while trying to retrieve text for error ORA-01804

Here is my code to fetch the data from database.

def cases(request, dbname, prnum=None, message=''):

    connection = cx_Oracle.Connection("%s/%s@%s" % ('foo', 'bar', 'xyz'))
    cursor = cx_Oracle.Cursor(connection)
    sql = "SELECT fielname FROM tablename where rownum < 10"
    cursor.execute(sql)
    names = [row[0] for row in cursor.fetchall()]
    cursor.close()
    connection.close()

Thanks,
Kamal

Ian

unread,
Apr 21, 2011, 3:31:21 PM4/21/11
to Django users
On Apr 21, 11:03 am, kamal sharma <kamalp.sha...@gmail.com> wrote:
> Error while trying to retrieve text for error ORA-01804
>
> Here is my code to fetch the data from database.
>
> def cases(request, dbname, prnum=None, message=''):
>
>     connection = cx_Oracle.Connection("%s/%s@%s" % ('foo', 'bar', 'xyz'))
>     cursor = cx_Oracle.Cursor(connection)
>     sql = "SELECT fielname FROM tablename where rownum < 10"
>     cursor.execute(sql)
>     names = [row[0] for row in cursor.fetchall()]
>     cursor.close()
>     connection.close()

First of all, why are you creating your own connection instead of
using the Django ORM?

Anyway, the proper way to create a cx_Oracle connection is with the
cx_Oracle.connect factory function, and the proper way to create a
cursor is with the connection.cursor() method. See the cx_Oracle docs
and the DB-API docs for details. However, I think there is something
more going on here.

ORA-01804: failure to initialize timezone information
Cause: The timezone information file was not properly read.
Action: Please contact Oracle Customer Support.

This and the fact that cx_Oracle wasn't able to look up the error code
itself suggest that there may be something wrong with your Oracle
client installation. Or it may be that your webserver is also
blocking access to the files in the client directory. What happens if
you try the same code from a Python shell?

kamal sharma

unread,
Apr 22, 2011, 1:40:25 AM4/22/11
to django...@googlegroups.com, ad...@dmarkey.com
When i try the same code from python Shell then it is working fine.

Here is the working output:
>>> import cx_Oracle
>>> from pprint import pprint
>>> connection = cx_Oracle.Connection("%s/%s@%s" % ('foo', 'bar', 'db'))
>>> cursor = cx_Oracle.Cursor(connection)
>>> sql = "SELECT xyz FROM table_name where rownum < 10"
>>> cursor.execute(sql)
[<cx_Oracle.STRING object at 0x33c00>]
>>> data = cursor.fetchall()
>>> pprint(data)
[('Robert Craig',),
 ('Darren Kerr',),
 ('Aviva Garrett',),
 ('Pasvorn Boonmark',),
 ('Dave Wittbrodt',),
 ('Pasvorn Boonmark',),
 ('Rajkumaran Chandrasekaran',),
 ('Pasvorn Boonmark',),
 ('Pasvorn Boonmark',)]


But when i use the same code in django views.py then it does not work and Getting error as: 
Exception Value:
Error while trying to retrieve text for error ORA-01804

def cases(request, db_name, prnum=None, message=''):
    connection = cx_Oracle.Connection("%s/%s@%s" % ('foo', 'bar', 'db'))
    cursor = cx_Oracle.Cursor(connection)
    sql = "SELECT xyz FROM table_name where rownum < 10"
    cursor.execute(sql)
    data = cursor.fetchall()
    pprint(data)
    cursor.close()
    connection.close()
    return render_to_response('web/cases.html', {}, RequestContext(request, {
     'first': 'test1',
     'last': 'test2',
    }))


Thanks,



--

Jirka Vejrazka

unread,
Apr 22, 2011, 5:50:21 AM4/22/11
to django...@googlegroups.com, ad...@dmarkey.com
Hi Kamal,

  checking my install history, this is what I had to do to use
cx_Oracle on Ubuntu Server:

$ /usr/local/oracle/instantclient_11_2$ ln -s libclntsh.so.11.1 libclntsh.so
$ WITH_UNICODE=1 ORACLE_HOME=/usr/local/oracle/instantclient_11_2
python setup.py build
$ sudo bash
# WITH_UNICODE=1 ORACLE_HOME=/usr/local/oracle/instantclient_11_2
python setup.py install

and tested:
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/oracle/instantclient_11_2
$ python
Python 2.5.2 (r252:60911, Jan 20 2010, 21:48:48)
[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2


Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle

>>> cx_Oracle.version
'5.0.3'

Then I added the LD_LIBRARY_PATH to /etc/profile so it'd be used by
all users, including the Apache process.

The error you're getting is most likely caused by ORACLE_HOME not
being set during cx_Oracle installation. To be extra sure, consider
adding it to /etc/profile too.


HTH

Jirka

kamal sharma

unread,
Apr 22, 2011, 9:46:54 AM4/22/11
to django...@googlegroups.com, ad...@dmarkey.com, jirka.v...@gmail.com
Hi Jirka,

Thanks for the response.

I have installed cx_Oracle in Solaris again and followed this steps but still the error is same:

Exception Type:DatabaseError
Exception Value:
Error while trying to retrieve text for error ORA-01804

Download cx_oracle module:

Install:

[me] ~/install_cx_oracle/cx_Oracle-5.0.3> gunzip cx_Oracle-5.0.3.tar.gz
[me] ~/install_cx_oracle/cx_Oracle-5.0.3> tar xvf cx_Oracle-5.0.3.tar
[me] ~/install_cx_oracle/cx_Oracle-5.0.3> cd cx_Oracle-5.0.3
[me] ~/install_cx_oracle/cx_Oracle-5.0.3> /usr/local/bin/python setup.py build
running build
running build_ext
building 'cx_Oracle' extension
creating build
creating build/temp.solaris-2.10-sun4u-2.6-11g
gcc -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/opt/app/oracle/products/11.2.0/rdbms/demo -I/opt/app/oracle/products/11.2.0/rdbms/public -I/usr/local/stow/python/include/python2.6 -c cx_Oracle.c -o build/temp.solaris-2.10-sun4u-2.6-11g/cx_Oracle.o -DBUILD_VERSION=5.0.3
In file included from /opt/app/oracle/products/11.2.0/rdbms/public/oci.h:3024,
                 from cx_Oracle.c:10:
/opt/app/oracle/products/11.2.0/rdbms/public/ociap.h:10788: warning: function declaration isn't a prototype
/opt/app/oracle/products/11.2.0/rdbms/public/ociap.h:10794: warning: function declaration isn't a prototype
creating build/lib.solaris-2.10-sun4u-2.6-11g
gcc -shared build/temp.solaris-2.10-sun4u-2.6-11g/cx_Oracle.o -L/opt/app/oracle/products/11.2.0/lib -L/opt/app/oracle/products/11.2.0 -L. -lclntsh -lpython2.6 -o build/lib.solaris-2.10-sun4u-2.6-11g/cx_Oracle.so

[me] ~/install_cx_oracle/cx_Oracle-5.0.3> echo $ORACLE_HOME/
/opt/app/oracle/products/11.2.0/

[me] ~/install_cx_oracle/cx_Oracle-5.0.3> sudo python setup.py install
Password:
running install
running build
running build_ext
running install_lib
copying build/lib.solaris-2.10-sun4u-2.4-11g/cx_Oracle.so -> /usr/lib/python2.4/site-packages

[me] ~/install_cx_oracle/cx_Oracle-5.0.3> python
Python 2.4.4 (#1, Jan 10 2007, 01:25:01) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> cx_Oracle.version
'5.0.3'

Did i missed something?

Thanks,
Kamal


--

Ian

unread,
Apr 22, 2011, 10:23:27 AM4/22/11
to Django users
On Apr 21, 11:40 pm, kamal sharma <kamalp.sha...@gmail.com> wrote:
> When i try the same code from python Shell then it is working fine.

Okay, so it does sound to me like it's your web server blocking access
to the entire ORACLE_HOME directory.

Is it running under a chroot jail? If so, then you will need to copy
your entire Oracle client installation into the jail and use the
chrooted path for your ORACLE_HOME and LD_LIBRARY_PATH.

Jirka Vejrazka

unread,
Apr 22, 2011, 10:23:44 AM4/22/11
to kamal sharma, django...@googlegroups.com, ad...@dmarkey.com
Are you getting this error only when using the app through a web server? Have you verified that the web server does have ORACLE_HOME and LD_LIBRARY_PATH correctly defined? (e.g. by logging it using os.environ).

Are you sure that the user that is used to run your webserver has permissions to read all Oracle client files?

 Just thinking "loudly" :)

   Jirka

kamal sharma

unread,
Apr 22, 2011, 10:38:40 AM4/22/11
to django...@googlegroups.com
I think i have done a mistake in my last cx_Oracle installation. Is this a problem? I used one python while build and another while install.

ex: [me] ~/install_cx_oracle/cx_Oracle-5.0.3> /usr/local/bin/python setup.py build
      [me] ~/install_cx_oracle/cx_Oracle-5.0.3> sudo python setup.py install

Now i am trying again, but not able to install.

[weevil] ~/install_cx_oracle/cx_Oracle-5.0.3> sudo /usr/local/bin/python setup.py build install
running build
running build_ext
running install
running bdist_egg
running egg_info
creating cx_Oracle.egg-info
error: could not create 'cx_Oracle.egg-info': Permission denied

How to resolve this?

Thanks,
Kamal


--

kamal sharma

unread,
Apr 22, 2011, 3:21:51 PM4/22/11
to Django users, ad...@dmarkey.com, jirka.v...@gmail.com
Yes, i am getting this error when i used from the web application. Also all the user have the permission to webserver to read all Oracle client files.

Not sure how it got set to LD_LIBRARY_PATH /opt/apache-2.2.16/lib:/usr/local/lib:

I have defined the LD_LIBRARY_PATH and ORACLE_HOME as follows in .profile

export ORACLE_HOME=/opt/app/oracle/products/11.2.0
export LD_LIBRARY_PATH=/opt/app/oracle/products/11.2.0/lib

Also tried it in .cshrc file also. But nothing is working.

setenv ORACLE_HOME "/opt/app/oracle/products/11.2.0"
setenv LD_LIBRARY_PATH "$ORACLE_HOME/lib32:$ORACLE_HOME/lib"
setenv LD_LIBRARY_PATH /usr/X11R6/lib:/usr/local/lib


I have written a sample script and it works, when i run from the command prompt.

filename: test_db.py
import cx_Oracle
from pprint import pprint

connection = cx_Oracle.Connection("%s/%s@%s" % ('foo', 'bar', 'db'))
cursor = cx_Oracle.Cursor(connection)
sql = "SELECT name FROM prs where rownum < 10"
cursor.execute(sql)
data = cursor.fetchall()
pprint(data)
cursor.close()
connection.close()

Getting proper output:

[me] ~/install_cx_oracle> /usr/local/bin/python test_db.py
[('Robert Craig',),
 ('Darren Kerr',),
 ('Aviva Garrett',),
 ('Pasvorn Boonmark',),
 ('Dave Wittbrodt',),
 ('Pasvorn Boonmark',),
 ('Rajkumaran Chandrasekaran',),
 ('Pasvorn Boonmark',),
 ('Pasvorn Boonmark',)]

 But samething when i write in django views.py then getting exception.

 
Request Method: GET
Django Version: 1.2.1
Exception Type: DatabaseError
Exception Value: Error while trying to retrieve text for error ORA-01804
Exception Location: /web/views.py in cases, line 43
Python Executable: /bin/python
Python Version: 2.6.6

Thanks,



kamal sharma

unread,
Apr 23, 2011, 2:29:09 PM4/23/11
to Jirka Vejrazka, Django users, ad...@dmarkey.com
No it was .profile of mine. Now I have set the LD_LIBRARY_PATH in app.wsgi as mentioned below and when I print the os.environ in the beginning of views.py then it shows that newly added value.

os.environ["LD_LIBRARY_PATH"] = "/opt/app/oracle/products/11.2.0/lib"

Is there any problem in this statement for connecting the database? The error show in this line only. I tried all the 3 way, but nothing is working.

    connection = cx_Oracle.Connection("%s/%s@%s" % ('foo', 'baar', 'db'))
    connection = cx_Oracle.connect("foo/baar@db")
    connection = cx_Oracle.connect('foo/baar@db')


But Still the issue is not resolved.

Thanks,

On Sat, Apr 23, 2011 at 2:42 AM, Jirka Vejrazka <jirka.v...@gmail.com> wrote:
.profile of which user? Yours or the one running webserver? Have you
confirmed that these are set (e.g. by writing those to a file at the
beginning of your views.py)?

 Cheers

   Jirka

Ian

unread,
Apr 24, 2011, 10:28:32 AM4/24/11
to Django users
On Apr 23, 12:29 pm, kamal sharma <kamalp.sha...@gmail.com> wrote:
> No it was .profile of mine. Now I have set the LD_LIBRARY_PATH in app.wsgi
> as mentioned below and when I print the os.environ in the beginning of
> views.py then it shows that newly added value.

It's clear that your LD_LIBRARY_PATH is fine at this point, since the
library must be loaded in order to get the ORA-01804 error. The
problem, as Jirka and I have suggested, is that the rest of your
Oracle installation is still not visible to the process, which is
preventing the client from reading its data files. Why this is the
case is unclear without knowing more details about your system. Is
the Oracle directory readable by the WSGI user? Is the WSGI process
running inside a chroot jail?

kamal sharma

unread,
Apr 26, 2011, 12:52:02 PM4/26/11
to django...@googlegroups.com, ad...@dmarkey.com, Jirka Vejrazka, ian.g...@gmail.com
Thanks to all for supporting this to fix the issue.

So issue is resolved by adding these 2 lines in "app.wsgi"

os.environ["LD_LIBRARY_PATH"] = "/opt/app/oracle/products/11.2.0/lib"
os.environ["ORACLE_HOME"] = "/opt/app/oracle/products/11.2.0"

Thanks again for helping to fix this issue. Its really a superb forum to fix the issue so quickly. :-)

Regards,
Kamal


--

kamal sharma

unread,
Apr 28, 2011, 2:16:04 PM4/28/11
to django...@googlegroups.com, ad...@dmarkey.com, Jirka Vejrazka, ian.g...@gmail.com
Hi Again,

I have finished my implementation with the local db, but now stuck in major issue.When i try to login to the local oracle database then it is working perfectly fine.I am able to read the data from the db installed in the same UNIX machine.

But when i tried to login a db installed in remote machine, which is installed in the client side, then getting following error.

Traceback (most recent call last):
  File "test_db.py", line 8, in ?
    connection = cx_Oracle.Connection("%s/%s@%s" % ('user', 'user123', 'db'))
cx_Oracle.DatabaseError: ORA-01804: failure to initialize timezone information

Also at the client machine the Oracle is (10.2.0) "/opt/app/oracle/products/10.2.0", but in my machine it is /opt/app/oracle/products/11.2.0 (11.2.0).


Earlier i was using like this in my CGI-Perl code, now i have to migrate this in Django-Python module to use the below logic.How we can solve this problem? 

# Add ORACLE_HOME for 10g Oracle client.
BEGIN { $ENV{ORACLE_HOME} = '/opt/app/oracle/products/10.2.0' }

Any help will be highly appreciated. 

Thanks,
Kamal

kamal sharma

unread,
Apr 29, 2011, 12:12:13 AM4/29/11
to Jirka Vejrazka, Django users
Yes you are correct, that the reason i have replaced the below line in "app.wsgi", but that does not works.

#os.environ["LD_LIBRARY_PATH"] = "/opt/app/oracle/products/11.2.0/lib"
#os.environ["ORACLE_HOME"] = "/opt/app/oracle/products/11.2.0"

os.environ["LD_LIBRARY_PATH"] = "/opt/app/oracle/products/10.2.0/lib"
os.environ["ORACLE_HOME"] = "/opt/app/oracle/products/10.2.0"

Thanks,



On Fri, Apr 29, 2011 at 2:10 AM, Jirka Vejrazka <jirka.v...@gmail.com> wrote:
Kamal, this is exactly the same error you were getting before. Are you
sure that you have ORACLE_HOME properly set on your new client and in
the right environment?

 Cheers

 Jirka

Jirka Vejrazka

unread,
Apr 29, 2011, 4:38:06 AM4/29/11
to kamal sharma, Django users
Have you checked permissions?
Reply all
Reply to author
Forward
0 new messages