Search code examples
pythonsqlalchemypycharmoracleclient

Pycharm can not see the Oracle Client, although it is added. Sqlalchemy, Python


I am trying to run my python code in pycharmie, which connects to the database, although it succeeds from the command line, PyCharm returns the following error.

It seems to me that I added all the necessary changes in the pycharma settings to run the Oracle Client.

enter image description here

My_file.py

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

Error from 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

How can I solve my problem? Why does the Terminal start the code without returning the error, while PyCharm returns the error related to the Oracle Client Library.

Any help will be appreciated.


Solution

  • If you're doing this on macOS (which I'm guessing from the way you have set DYLD_LIBRARY_PATH) then you need to put the Oracle Instant Client in ~/lib or as described in the cx_Oracle installation instructions https://cx-oracle.readthedocs.io/en/latest/installation.html#install-oracle-instant-client because SIP security on macOS now stops DYLD_LIBRARY_PATH working in sub-shells.

    I would also recommend using Instant Client 12.2 unless you actually need to connect to very old DB versions. With 12.2 client you can connect to Oracle DB 10.2 or later.