无法在Ubuntu上使用Django连接到Oracle数据库

Cannot connect to Oracle database with Django on Ubuntu

每当我尝试运行像python manage.py syncdb这样的命令时,我都会收到以下错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Traceback (most recent call last):
  File"manage.py", line 11, in
    execute_manager(settings)
  File"/home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File"/home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File"/home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/django/core/management/__init__.py", line 261, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File"/home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/django/core/management/__init__.py", line 67, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File"/home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File"/home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/south/management/commands/__init__.py", line 10, in
    import django.template.loaders.app_directories
  File"/home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/django/template/loaders/app_directories.py", line 21, in
    mod = import_module(app)
  File"/home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File"/home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/django/contrib/admin/__init__.py", line 1, in
    from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
  File"/home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/django/contrib/admin/helpers.py", line 1, in
    from django import forms
  File"/home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/django/forms/__init__.py", line 17, in
    from models import *
  File"/home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/django/forms/models.py", line 6, in
    from django.db import connections
  File"/home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/django/db/__init__.py", line 77, in
    connection = connections[DEFAULT_DB_ALIAS]
  File"/home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/django/db/utils.py", line 91, in __getitem__
    backend = load_backend(db['ENGINE'])
  File"/home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/django/db/utils.py", line 32, in load_backend
    return import_module('.base', backend_name)
  File"/home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File"/home/damon/Workspace/django-projects/acm-cie/env/lib/python2.6/site-packages/django/db/backends/oracle/base.py", line 24, in
    raise ImproperlyConfigured("Error loading cx_Oracle module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading cx_Oracle module: libclntsh.so.11.1: cannot open shared object file: No such file or directory

这可以解决Ubuntu吗?


您需要同时安装cx_Oracle和Oracle Client。

cx_Oracle可以在这里找到。

可以在此处找到适当的Oracle客户端。

在启动应用程序之前,还需要设置LD_LIBRARY_PATH变量。 通常可以这样做(例如):

1
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib

现在您应该能够通过cx_Oracle错误消息。


首先要做的事情 - 这实际上是一个Django问题,还是只是一个cx_Oracle问题? 是否正确安装了cx_Oracle? 您可以在Python shell会话中连接到Oracle数据库吗?

1
2
3
import cx_Oracle
conn = cx_Oracle.connect('/') # user/password@dsn
cursor = conn.cursor()

如果这不会引发异常,则表示已成功连接。


尝试easy_install cx_Oracle,它将从源代码编译cx_Oracle模块