首页 > 解决方案 > Pycharm 看不到 Oracle 客户端,虽然添加了。Sqlalchemy, Python

问题描述

我正在尝试在连接到数据库的 pycharmie 中运行我的 python 代码,尽管它从命令行成功,但 PyCharm 返回以下错误。

在我看来,我在 pycharma 设置中添加了所有必要的更改来运行 Oracle 客户端。

在此处输入图像描述

我的文件.py

import sqlalchemy as db
engine = db.create_engine('dialect+driver://user:pass@host:port/db')
connection = enigne.connect()

来自 PyCharm 的错误

/home/tymoteusz/Desktop/test/venv/bin/python /home/tymoteusz/Desktop/my_task_sqlalchemy/test_document.py
Traceback (most recent call last):
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2221, in _wrap_pool_connect
    return fn()
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 356, in unique_connection
    return _ConnectionFairy._checkout(self)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 811, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 543, in checkout
    rec = pool._do_get()
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1239, in _do_get
    self._dec_overflow()
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 67, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 277, in reraise
    raise value
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1236, in _do_get
    return self._create_connection()
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 361, in _create_connection
    return _ConnectionRecord(self)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 488, in __init__
    self.__connect(first_connect_check=True)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 690, in __connect
    connection = pool._invoke_creator(self)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
    return dialect.connect(*cargs, **cparams)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 437, in connect
    return self.dbapi.connect(*cargs, **cparams)
cx_Oracle.DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: "libclntsh.so: cannot open shared object file: No such file or directory". See https://oracle.github.io/odpi/doc/installation.html#linux for help

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/tymoteusz/Desktop/my_task_sqlalchemy/test_document.py", line 6, in <module>
    connection = engine.connect()
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2164, in connect
    return self._connection_cls(self, **kwargs)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 103, in __init__
    else engine.raw_connection()
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2252, in raw_connection
    self.pool.unique_connection, _connection
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2225, in _wrap_pool_connect
    e, dialect, self
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1522, in _handle_dbapi_exception_noconnection
    util.raise_from_cause(sqlalchemy_exception, exc_info)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 296, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 276, in reraise
    raise value.with_traceback(tb)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2221, in _wrap_pool_connect
    return fn()
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 356, in unique_connection
    return _ConnectionFairy._checkout(self)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 811, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 543, in checkout
    rec = pool._do_get()
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1239, in _do_get
    self._dec_overflow()
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 67, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 277, in reraise
    raise value
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1236, in _do_get
    return self._create_connection()
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 361, in _create_connection
    return _ConnectionRecord(self)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 488, in __init__
    self.__connect(first_connect_check=True)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 690, in __connect
    connection = pool._invoke_creator(self)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
    return dialect.connect(*cargs, **cparams)
  File "/home/tymoteusz/.local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 437, in connect
    return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.DatabaseError: (cx_Oracle.DatabaseError) DPI-1047: 64-bit Oracle Client library cannot be loaded: "libclntsh.so: cannot open shared object file: No such file or directory". See https://oracle.github.io/odpi/doc/installation.html#linux for help (Background on this error at: http://sqlalche.me/e/4xp6)

Process finished with exit code 1

我该如何解决我的问题?为什么Terminal启动代码没有返回错误,而PyCharm返回Oracle Client Library相关的错误。

任何帮助将不胜感激。

标签: pythonsqlalchemypycharmoracleclient

解决方案


如果您在 macOS 上执行此操作(我从您设置 DYLD_LIBRARY_PATH 的方式猜测),那么您需要将 Oracle Instant Client 放入 ~/lib 或按照 cx_Oracle 安装说明https://cx-oracle中的说明.readthedocs.io/en/latest/installation.html#install-oracle-instant-client因为 macOS 上的 SIP 安全性现在停止 DYLD_LIBRARY_PATH 在子 shell 中工作。

我还建议您使用 Instant Client 12.2,除非您确实需要连接到非常旧的数据库版本。使用 12.2 客户端,您可以连接到 Oracle DB 10.2 或更高版本。


推荐阅读