[PYTHON] An error occurs when trying to import scikit-learn after connecting to Oracle with SQLAlchemy

phenomenon

If you import sklearn after actually connecting the DB from the ʻEngine instance of SQLAlchemy as shown below, ʻImport Error will occur.

demo.Case of 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 # <-Here, "Import Error:DLL load failed "occurs
Full stack trace
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:The specified procedure cannot be found.

Cases actually encountered

I don't think you will write ʻimport in the middle of processing as described above in the .py` file, but you may encounter an error in the following cases.

--When importing additionally after working with Jupyter Notebook to some extent --When deserializing an object with joblib.load (...) etc: arrow_left: This is what I encountered

Countermeasures

As symptomatic treatment, it may be declared in advance in the code.

Example of importing in advance


import sqlalchemy
import cx_Oracle
import joblib
import sklearn # <-Not used explicitly in this file, but declared

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') # <-Implicitly used sklearn here

Recommended Posts

An error occurs when trying to import scikit-learn after connecting to Oracle with SQLAlchemy
What to do when an error occurs with import _ssl
[Beanstalk] What to do when an error occurs with import uuid
After installing CentOS8 with VirtualBox, an error occurs when restarting
I get an error when trying to install maec 4.0.1.0 with pip
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
What to do if an error occurs when importing numpy with VScode
Rollback processing when an error occurs with fabric
Workaround if you get an error when trying to install PySide with pip
A memorandum when an error occurs with pip install
What to do if you get an Import Error when importing matplotlib with Jupyter
What to do if you get an Undefined error when trying to use pip with pyenv
Solution when an error occurs when hiding the console screen with PyInstaller
I get an error with import pandas.
I got an error when trying to install Xgboost and its solution
When I get an error with PyInstaller
How to deal with SSL error when connecting to S3 with boto of Python
[Python / Pandas] A bug occurs when trying to replace a DataFrame with `None` with` replace`
When doing sam build with AWS Lambda Layers, the file hierarchy changes and an import error occurs.
What to do if you get an error when trying to load mnist
[OSX] [pyenv] What to do when an SSL error occurs in pip
[AWS] How to deal with WordPress "An error occurred when cropping an image."
What to do if you get an error when trying to send a message in tasks.loop () immediately after startup
What to do if an error occurs when loading a python project created with poetry into VS Code
[Python] I want to know the variables in the function when an error occurs!
[python] What to do when an error occurs in send_keys of headless chrome
What to do if you get an error when installing python with pyenv
I got an error when saving with OpenCV
Error when trying to install psycopg2 in Python
An error occurs when importing japandas (problem unsolved)
scikit-learn import error
[Python] Type Error:'WebElement' object is not iterable What to do when an error occurs
What to do if you get an OpenSSL error when installing Python 2 with pyenv
An easy way to create an import module with jupyter
I get an error when trying meinheld + WebSocket + mongodb
Addresses an issue where an error does not occur even if the path does not pass when trying to read an image with cv2.imread ()
I got an error when using Tensorboard with Pytorch
[Linux] After connecting to DB with teraterm and typing Sqlplus command, until logging in to DB (oracle)
Error and solution when trying to run a classifier made with Labellio with my own ubuntu
(Vagrant) Handling of Permission denied (publickey, gssapi-keyex, gssapi-with-mic) error that occurs when connecting with SSH
What to do when a Remove Error occurs when updating conda
[python] A note when trying to use numpy with Cython
I get [Error 2055] when trying to connect to MySQL on Heroku
I'm trying to create an authentication / authorization process with Django
I tried to create an article in Wiki.js with SQLAlchemy