0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SQLAlchemyでOracleに接続した後scikit-learnをimportしようとするとエラーになる件

Posted at

現象

以下のようにSQLAlchemyのEngineインスタンスから実際にDBを接続した後にsklearnをインポートするとImportErrorが発生する。

demo.py/ImportErrorになるケース
import sqlalchemy
import cx_Oracle

dsnStr = cx_Oracle.makedsn('localhost','1521','pdb-name')
connect_str = 'oracle://user:pass@' + dsnStr.replace('SID', 'SERVICE_NAME')
engine = sqlalchemy.create_engine(connect_str, max_identifier_length=128)

conn = engine.connect()

import sklearn # <-ここで「ImportError: DLL load failed」発生
スタックトレース全文
Traceback (most recent call last):
  File "demo.py", line 12, in <module>
    import sklearn
  File "C:\Users\kurukuruz\anaconda3\envs\sqla-demo\lib\site-packages\sklearn\__init__.py", line 80, in <module>    
    from .base import clone
  File "C:\Users\kurukuruz\anaconda3\envs\sqla-demo\lib\site-packages\sklearn\base.py", line 21, in <module>
    from .utils import _IS_32BIT
  File "C:\Users\kurukuruz\anaconda3\envs\sqla-demo\lib\site-packages\sklearn\utils\__init__.py", line 23, in <module>
    from .class_weight import compute_class_weight, compute_sample_weight
  File "C:\Users\kurukuruz\anaconda3\envs\sqla-demo\lib\site-packages\sklearn\utils\class_weight.py", line 7, in <module>
    from .validation import _deprecate_positional_args
  File "C:\Users\kurukuruz\anaconda3\envs\sqla-demo\lib\site-packages\sklearn\utils\validation.py", line 25, in <module>
    from .fixes import _object_dtype_isnan, parse_version
  File "C:\Users\kurukuruz\anaconda3\envs\sqla-demo\lib\site-packages\sklearn\utils\fixes.py", line 18, in <module> 
    import scipy.stats
  File "C:\Users\kurukuruz\anaconda3\envs\sqla-demo\lib\site-packages\scipy\stats\__init__.py", line 388, in <module>
    from .stats import *
  File "C:\Users\kurukuruz\anaconda3\envs\sqla-demo\lib\site-packages\scipy\stats\stats.py", line 180, in <module>  
    from . import distributions
  File "C:\Users\kurukuruz\anaconda3\envs\sqla-demo\lib\site-packages\scipy\stats\distributions.py", line 8, in <module>
    from ._distn_infrastructure import (entropy, rv_discrete, rv_continuous,
  File "C:\Users\kurukuruz\anaconda3\envs\sqla-demo\lib\site-packages\scipy\stats\_distn_infrastructure.py", line 23, in <module>
    from scipy import optimize
  File "C:\Users\kurukuruz\anaconda3\envs\sqla-demo\lib\site-packages\scipy\optimize\__init__.py", line 387, in <module>
    from .optimize import *
  File "C:\Users\kurukuruz\anaconda3\envs\sqla-demo\lib\site-packages\scipy\optimize\optimize.py", line 36, in <module>
    from ._numdiff import approx_derivative
  File "C:\Users\kurukuruz\anaconda3\envs\sqla-demo\lib\site-packages\scipy\optimize\_numdiff.py", line 6, in <module>
    from scipy.sparse.linalg import LinearOperator
  File "C:\Users\kurukuruz\anaconda3\envs\sqla-demo\lib\site-packages\scipy\sparse\linalg\__init__.py", line 114, in <module>
    from .eigen import *
  File "C:\Users\kurukuruz\anaconda3\envs\sqla-demo\lib\site-packages\scipy\sparse\linalg\eigen\__init__.py", line 9, in <module>
    from .arpack import *
  File "C:\Users\kurukuruz\anaconda3\envs\sqla-demo\lib\site-packages\scipy\sparse\linalg\eigen\arpack\__init__.py", line 20, in <module>
    from .arpack import *
  File "C:\Users\kurukuruz\anaconda3\envs\sqla-demo\lib\site-packages\scipy\sparse\linalg\eigen\arpack\arpack.py", line 43, in <module>
    from . import _arpack
ImportError: DLL load failed while importing _arpack: 指定されたプロシージャが見つかりません。

実際に遭遇するケース

.pyファイル内において上記のように処理途中でimportを記述することは無いと思うが、下記のようなケースではエラーに遭遇する可能性がある。

  • Jupyter Notebookである程度作業した後に追加でインポートする場合
  • joblib.load(...)などでオブジェクトをデシリアライズする場合:arrow_left:自分が遭遇したのはこれ

対策

対症療法としてはコード内であらかじめ宣言しておけばよい。

あらかじめimportしておく例
import sqlalchemy
import cx_Oracle
import joblib
import sklearn # <- このファイル内では明には使用しないが宣言しておく

dsnStr = cx_Oracle.makedsn('localhost','1521','pdb-name')
connect_str = 'oracle://user:pass@' + dsnStr.replace('SID', 'SERVICE_NAME')
engine = sqlalchemy.create_engine(connect_str, max_identifier_length=128)

conn = engine.connect()

joblib.load('path/to/file.dump') # <- ここで暗黙的にsklearnが使用されている
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?