Repository: cupy/cupy

Issue:
Failure importing `cupyx.scipy.linalg` in CuPy 11 without `scipy` installed
### Description

In CuPy 11.0.0, `import cupyx.scipy.linalg` fails if `scipy` isn't installed. This was not the case for CuPy 10.6.0.

This is not a major problem, but I wanted to confirm, is the new behavior intended or is it a bug?

For context, this has come up in the context of some Dask tests that depend on `cupyx.scipy.linalg` that used to import and successfully complete in CuPy 10.6.0 even without `scipy` being installed.

### To Reproduce

CuPy 10.6.0 _without_ `scipy` succeeds:

```py
In [1]: import cupy

In [2]: cupy.__version__
Out[2]: '10.6.0'

In [3]: import cupyx.scipy.linalg

In [4]: import scipy
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 import scipy

ModuleNotFoundError: No module named 'scipy'
```

CuPy 11.0.0 _without_ `scipy` fails:

```python
In [1]: import cupy

In [2]: cupy.__version__
Out[2]: '11.0.0'

In [3]: import cupyx.scipy.linalg
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [3], in <cell line: 1>()
----> 1 import cupyx.scipy.linalg

File /datasets/pentschev/miniconda3/envs/rn-220729/lib/python3.8/site-packages/cupyx/scipy/linalg/__init__.py:2, in <module>
      1 # flake8: NOQA
----> 2 from cupyx.scipy.linalg._special_matrices import (
      3     tri, tril, triu, toeplitz, circulant, hankel,
      4     hadamard, leslie, kron, block_diag, companion,
      5     helmert, hilbert, dft,
      6     fiedler, fiedler_companion, convolution_matrix
      7 )
      8 from cupyx.scipy.linalg._solve_triangular import solve_triangular  # NOQA
      9 from cupyx.scipy.linalg._decomp_lu import lu, lu_factor, lu_solve  # NOQA

File /datasets/pentschev/miniconda3/envs/rn-220729/lib/python3.8/site-packages/cupyx/scipy/linalg/_special_matrices.py:4, in <module>
      2 import cupy
      3 from cupy import _core
----> 4 from cupyx.scipy.linalg import _uarray
      7 @_uarray.implements('tri')
      8 def tri(N, M=None, k=0, dtype=None):
      9     """ Construct (``N``, ``M``) matrix filled with ones at and below the
     10     ``k``-th diagonal. The matrix has ``A[i,j] == 1`` for ``i <= j + k``.
     11
   (...)
     24     .. seealso:: :func:`scipy.linalg.tri`
     25     """

File /datasets/pentschev/miniconda3/envs/rn-220729/lib/python3.8/site-packages/cupyx/scipy/linalg/_uarray.py:73, in <module>
     71 for func_name in _cp_linalg_functions:
     72     cp_func = getattr(_cp_linalg, func_name)
---> 73     scipy_func = getattr(_scipy_linalg, func_name)
     74     _implemented[scipy_func] = cp_func

AttributeError: 'NoneType' object has no attribute 'eigh'

In [4]: import scipy
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 import scipy

ModuleNotFoundError: No module named 'scipy'

In [5]:
```

CuPy 11.0.0 _with_ `scipy` succeeds:

```python
In [1]: import cupy

In [2]: cupy.__version__
Out[2]: '11.0.0'

In [3]: import cupyx.scipy.linalg

In [4]: import scipy

In [5]:
```

### Installation

_No response_

### Environment

CuPy 10.6.0:

```
OS                           : Linux-4.15.0-189-generic-x86_64-with-glibc2.10
Python Version               : 3.8.13
CuPy Version                 : 10.6.0
CuPy Platform                : NVIDIA CUDA
NumPy Version                : 1.21.6
SciPy Version                : None
Cython Build Version         : 0.29.30
Cython Runtime Version       : 0.29.30
CUDA Root                    : /datasets/pentschev/miniconda3/envs/rn-220728
nvcc PATH                    : None
CUDA Build Version           : 11020
CUDA Driver Version          : 11060
CUDA Runtime Version         : 11050
cuBLAS Version               : (available)
cuFFT Version                : 10600
cuRAND Version               : 10207
cuSOLVER Version             : (11, 3, 2)
cuSPARSE Version             : (available)
NVRTC Version                : (11, 5)
Thrust Version               : 101000
CUB Build Version            : 101000
Jitify Build Version         : 87d8ccd
cuDNN Build Version          : None
cuDNN Version                : None
NCCL Build Version           : None
NCCL Runtime Version         : None
cuTENSOR Version             : None
cuSPARSELt Build Version     : None
Device 0 Name                : Tesla V100-SXM2-32GB
Device 0 Compute Capability  : 70
Device 0 PCI Bus ID          : 0000:06:00.0
Device 1 Name                : Tesla V100-SXM2-32GB
Device 1 Compute Capability  : 70
Device 1 PCI Bus ID          : 0000:07:00.0
Device 2 Name                : Tesla V100-SXM2-32GB
Device 2 Compute Capability  : 70
Device 2 PCI Bus ID          : 0000:0A:00.0
Device 3 Name                : Tesla V100-SXM2-32GB
Device 3 Compute Capability  : 70
Device 3 PCI Bus ID          : 0000:0B:00.0
Device 4 Name                : Tesla V100-SXM2-32GB
Device 4 Compute Capability  : 70
Device 4 PCI Bus ID          : 0000:85:00.0
Device 5 Name                : Tesla V100-SXM2-32GB
Device 5 Compute Capability  : 70
Device 5 PCI Bus ID          : 0000:86:00.0
Device 6 Name                : Tesla V100-SXM2-32GB
Device 6 Compute Capability  : 70
Device 6 PCI Bus ID          : 0000:89:00.0
Device 7 Name                : Tesla V100-SXM2-32GB
Device 7 Compute Capability  : 70
Device 7 PCI Bus ID          : 0000:8A:00.0
```

CuPy 11.0.0:

```
OS                           : Linux-4.15.0-189-generic-x86_64-with-glibc2.10
Python Version               : 3.8.13
CuPy Version                 : 11.0.0
CuPy Platform                : NVIDIA CUDA
NumPy Version                : 1.21.6
SciPy Version                : None
Cython Build Version         : 0.29.30
Cython Runtime Version       : 0.29.31
CUDA Root                    : /datasets/pentschev/miniconda3/envs/rn-220729
nvcc PATH                    : None
CUDA Build Version           : 11020
CUDA Driver Version          : 11060
CUDA Runtime Version         : 11050
cuBLAS Version               : (available)
cuFFT Version                : 10600
cuRAND Version               : 10207
cuSOLVER Version             : (11, 3, 2)
cuSPARSE Version             : (available)
NVRTC Version                : (11, 5)
Thrust Version               : 101000
CUB Build Version            : 101000
Jitify Build Version         : 3c4a4ba
cuDNN Build Version          : None
cuDNN Version                : None
NCCL Build Version           : None
NCCL Runtime Version         : None
cuTENSOR Version             : None
cuSPARSELt Build Version     : None
Device 0 Name                : Tesla V100-SXM2-32GB
Device 0 Compute Capability  : 70
Device 0 PCI Bus ID          : 0000:06:00.0
Device 1 Name                : Tesla V100-SXM2-32GB
Device 1 Compute Capability  : 70
Device 1 PCI Bus ID          : 0000:07:00.0
Device 2 Name                : Tesla V100-SXM2-32GB
Device 2 Compute Capability  : 70
Device 2 PCI Bus ID          : 0000:0A:00.0
Device 3 Name                : Tesla V100-SXM2-32GB
Device 3 Compute Capability  : 70
Device 3 PCI Bus ID          : 0000:0B:00.0
Device 4 Name                : Tesla V100-SXM2-32GB
Device 4 Compute Capability  : 70
Device 4 PCI Bus ID          : 0000:85:00.0
Device 5 Name                : Tesla V100-SXM2-32GB
Device 5 Compute Capability  : 70
Device 5 PCI Bus ID          : 0000:86:00.0
Device 6 Name                : Tesla V100-SXM2-32GB
Device 6 Compute Capability  : 70
Device 6 PCI Bus ID          : 0000:89:00.0
Device 7 Name                : Tesla V100-SXM2-32GB
Device 7 Compute Capability  : 70
Device 7 PCI Bus ID          : 0000:8A:00.0
```


### Additional Information

_No response_


Discussion:
Perhaps the uarray support should be entirely disabled (making the decorator a no-op) when scipy is not available? 
#6460
Thanks for reporting. I think this is unexpected and should be fixed.

Resolving patch:
diff --git a/.pfnci/generate.py b/.pfnci/generate.py
--- a/.pfnci/generate.py
+++ b/.pfnci/generate.py
@@ -168,17 +168,24 @@ def generate_dockerfile(self) -> str:
 
         # Setup Python libraries.
         pip_args = []
+        pip_uninstall_args = []
         for pylib in ('numpy', 'scipy', 'optuna', 'cython', 'cuda-python'):
             pylib_ver = getattr(matrix, pylib)
             if pylib_ver is None:
-                continue
-            pip_spec = self.schema[pylib][pylib_ver]['spec']
-            pip_args.append(f'{pylib}{pip_spec}')
+                pip_uninstall_args.append(pylib)
+            else:
+                pip_spec = self.schema[pylib][pylib_ver]['spec']
+                pip_args.append(f'{pylib}{pip_spec}')
         lines += [
             f'RUN pip install -U {shlex.join(pip_args)}',
-            '',
         ]
-
+        if len(pip_uninstall_args) != 0:
+            # Ensure that packages are not installed.
+            lines += [
+                f'RUN pip uninstall -y {shlex.join(pip_uninstall_args)} && \\',
+                '    pip check',
+            ]
+        lines.append('')
         return '\n'.join(lines)
 
     def _additional_packages(self, kind: str) -> List[str]:
diff --git a/cupyx/scipy/linalg/_uarray.py b/cupyx/scipy/linalg/_uarray.py
--- a/cupyx/scipy/linalg/_uarray.py
+++ b/cupyx/scipy/linalg/_uarray.py
@@ -68,7 +68,8 @@ def inner(func):
     'solve', 'lstsq', 'inv', 'pinv'
 ]
 
-for func_name in _cp_linalg_functions:
-    cp_func = getattr(_cp_linalg, func_name)
-    scipy_func = getattr(_scipy_linalg, func_name)
-    _implemented[scipy_func] = cp_func
+if _scipy_linalg:
+    for func_name in _cp_linalg_functions:
+        cp_func = getattr(_cp_linalg, func_name)
+        scipy_func = getattr(_scipy_linalg, func_name)
+        _implemented[scipy_func] = cp_func


Test patch:
diff --git a/.pfnci/linux/tests/benchmark.Dockerfile b/.pfnci/linux/tests/benchmark.Dockerfile
--- a/.pfnci/linux/tests/benchmark.Dockerfile
+++ b/.pfnci/linux/tests/benchmark.Dockerfile
@@ -24,3 +24,5 @@ RUN pyenv install 3.10.0 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.22.*' 'scipy==1.7.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/benchmark.head.Dockerfile b/.pfnci/linux/tests/benchmark.head.Dockerfile
--- a/.pfnci/linux/tests/benchmark.head.Dockerfile
+++ b/.pfnci/linux/tests/benchmark.head.Dockerfile
@@ -24,3 +24,5 @@ RUN pyenv install 3.10.0 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.22.*' 'scipy==1.7.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda-example.Dockerfile b/.pfnci/linux/tests/cuda-example.Dockerfile
--- a/.pfnci/linux/tests/cuda-example.Dockerfile
+++ b/.pfnci/linux/tests/cuda-example.Dockerfile
@@ -24,3 +24,5 @@ RUN pyenv install 3.8.11 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.20.*' 'scipy==1.7.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda-head.Dockerfile b/.pfnci/linux/tests/cuda-head.Dockerfile
--- a/.pfnci/linux/tests/cuda-head.Dockerfile
+++ b/.pfnci/linux/tests/cuda-head.Dockerfile
@@ -24,3 +24,5 @@ RUN pyenv install 3.10.0 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy>=0a0' 'scipy>=0a0' 'optuna>=0a0' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda-slow.Dockerfile b/.pfnci/linux/tests/cuda-slow.Dockerfile
--- a/.pfnci/linux/tests/cuda-slow.Dockerfile
+++ b/.pfnci/linux/tests/cuda-slow.Dockerfile
@@ -24,3 +24,5 @@ RUN pyenv install 3.9.6 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.21.*' 'scipy==1.7.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda102.Dockerfile b/.pfnci/linux/tests/cuda102.Dockerfile
--- a/.pfnci/linux/tests/cuda102.Dockerfile
+++ b/.pfnci/linux/tests/cuda102.Dockerfile
@@ -30,3 +30,5 @@ RUN pyenv install 3.7.11 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.20.*' 'scipy==1.6.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda102.multi.Dockerfile b/.pfnci/linux/tests/cuda102.multi.Dockerfile
--- a/.pfnci/linux/tests/cuda102.multi.Dockerfile
+++ b/.pfnci/linux/tests/cuda102.multi.Dockerfile
@@ -30,3 +30,5 @@ RUN pyenv install 3.7.11 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.20.*' 'scipy==1.6.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda110.Dockerfile b/.pfnci/linux/tests/cuda110.Dockerfile
--- a/.pfnci/linux/tests/cuda110.Dockerfile
+++ b/.pfnci/linux/tests/cuda110.Dockerfile
@@ -24,3 +24,5 @@ RUN pyenv install 3.9.6 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.20.*' 'scipy==1.7.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda110.multi.Dockerfile b/.pfnci/linux/tests/cuda110.multi.Dockerfile
--- a/.pfnci/linux/tests/cuda110.multi.Dockerfile
+++ b/.pfnci/linux/tests/cuda110.multi.Dockerfile
@@ -24,3 +24,5 @@ RUN pyenv install 3.9.6 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.20.*' 'scipy==1.7.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda111.Dockerfile b/.pfnci/linux/tests/cuda111.Dockerfile
--- a/.pfnci/linux/tests/cuda111.Dockerfile
+++ b/.pfnci/linux/tests/cuda111.Dockerfile
@@ -24,4 +24,6 @@ RUN pyenv install 3.7.11 && \
     pyenv global 3.7.11 && \
     pip install -U setuptools pip
 
-RUN pip install -U 'numpy==1.20.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip install -U 'numpy==1.20.*' 'cython==0.29.*'
+RUN pip uninstall -y scipy optuna cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda111.multi.Dockerfile b/.pfnci/linux/tests/cuda111.multi.Dockerfile
--- a/.pfnci/linux/tests/cuda111.multi.Dockerfile
+++ b/.pfnci/linux/tests/cuda111.multi.Dockerfile
@@ -24,4 +24,6 @@ RUN pyenv install 3.7.11 && \
     pyenv global 3.7.11 && \
     pip install -U setuptools pip
 
-RUN pip install -U 'numpy==1.20.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip install -U 'numpy==1.20.*' 'cython==0.29.*'
+RUN pip uninstall -y scipy optuna cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda112.Dockerfile b/.pfnci/linux/tests/cuda112.Dockerfile
--- a/.pfnci/linux/tests/cuda112.Dockerfile
+++ b/.pfnci/linux/tests/cuda112.Dockerfile
@@ -25,3 +25,5 @@ RUN pyenv install 3.7.11 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.21.*' 'scipy==1.7.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda112.multi.Dockerfile b/.pfnci/linux/tests/cuda112.multi.Dockerfile
--- a/.pfnci/linux/tests/cuda112.multi.Dockerfile
+++ b/.pfnci/linux/tests/cuda112.multi.Dockerfile
@@ -25,3 +25,5 @@ RUN pyenv install 3.7.11 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.21.*' 'scipy==1.7.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda113.Dockerfile b/.pfnci/linux/tests/cuda113.Dockerfile
--- a/.pfnci/linux/tests/cuda113.Dockerfile
+++ b/.pfnci/linux/tests/cuda113.Dockerfile
@@ -24,3 +24,5 @@ RUN pyenv install 3.8.11 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.20.*' 'scipy==1.6.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda113.multi.Dockerfile b/.pfnci/linux/tests/cuda113.multi.Dockerfile
--- a/.pfnci/linux/tests/cuda113.multi.Dockerfile
+++ b/.pfnci/linux/tests/cuda113.multi.Dockerfile
@@ -24,3 +24,5 @@ RUN pyenv install 3.8.11 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.20.*' 'scipy==1.6.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda114.Dockerfile b/.pfnci/linux/tests/cuda114.Dockerfile
--- a/.pfnci/linux/tests/cuda114.Dockerfile
+++ b/.pfnci/linux/tests/cuda114.Dockerfile
@@ -24,3 +24,5 @@ RUN pyenv install 3.10.0 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.22.*' 'scipy==1.7.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda114.multi.Dockerfile b/.pfnci/linux/tests/cuda114.multi.Dockerfile
--- a/.pfnci/linux/tests/cuda114.multi.Dockerfile
+++ b/.pfnci/linux/tests/cuda114.multi.Dockerfile
@@ -24,3 +24,5 @@ RUN pyenv install 3.10.0 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.22.*' 'scipy==1.7.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda115.Dockerfile b/.pfnci/linux/tests/cuda115.Dockerfile
--- a/.pfnci/linux/tests/cuda115.Dockerfile
+++ b/.pfnci/linux/tests/cuda115.Dockerfile
@@ -24,3 +24,5 @@ RUN pyenv install 3.10.0 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.23.*' 'scipy==1.8.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda115.multi.Dockerfile b/.pfnci/linux/tests/cuda115.multi.Dockerfile
--- a/.pfnci/linux/tests/cuda115.multi.Dockerfile
+++ b/.pfnci/linux/tests/cuda115.multi.Dockerfile
@@ -24,3 +24,5 @@ RUN pyenv install 3.10.0 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.23.*' 'scipy==1.8.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda116.Dockerfile b/.pfnci/linux/tests/cuda116.Dockerfile
--- a/.pfnci/linux/tests/cuda116.Dockerfile
+++ b/.pfnci/linux/tests/cuda116.Dockerfile
@@ -24,3 +24,5 @@ RUN pyenv install 3.10.0 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.22.*' 'scipy==1.7.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda116.multi.Dockerfile b/.pfnci/linux/tests/cuda116.multi.Dockerfile
--- a/.pfnci/linux/tests/cuda116.multi.Dockerfile
+++ b/.pfnci/linux/tests/cuda116.multi.Dockerfile
@@ -24,3 +24,5 @@ RUN pyenv install 3.10.0 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.22.*' 'scipy==1.7.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda117.Dockerfile b/.pfnci/linux/tests/cuda117.Dockerfile
--- a/.pfnci/linux/tests/cuda117.Dockerfile
+++ b/.pfnci/linux/tests/cuda117.Dockerfile
@@ -24,3 +24,5 @@ RUN pyenv install 3.10.0 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.23.*' 'scipy==1.8.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/cuda117.multi.Dockerfile b/.pfnci/linux/tests/cuda117.multi.Dockerfile
--- a/.pfnci/linux/tests/cuda117.multi.Dockerfile
+++ b/.pfnci/linux/tests/cuda117.multi.Dockerfile
@@ -24,3 +24,5 @@ RUN pyenv install 3.10.0 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.23.*' 'scipy==1.8.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/rocm-4-3.Dockerfile b/.pfnci/linux/tests/rocm-4-3.Dockerfile
--- a/.pfnci/linux/tests/rocm-4-3.Dockerfile
+++ b/.pfnci/linux/tests/rocm-4-3.Dockerfile
@@ -33,3 +33,5 @@ RUN pyenv install 3.7.11 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.20.*' 'scipy==1.6.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/rocm-5-0.Dockerfile b/.pfnci/linux/tests/rocm-5-0.Dockerfile
--- a/.pfnci/linux/tests/rocm-5-0.Dockerfile
+++ b/.pfnci/linux/tests/rocm-5-0.Dockerfile
@@ -33,3 +33,5 @@ RUN pyenv install 3.9.6 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.23.*' 'scipy==1.8.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/rocm-5-1.Dockerfile b/.pfnci/linux/tests/rocm-5-1.Dockerfile
--- a/.pfnci/linux/tests/rocm-5-1.Dockerfile
+++ b/.pfnci/linux/tests/rocm-5-1.Dockerfile
@@ -33,3 +33,5 @@ RUN pyenv install 3.9.6 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.23.*' 'scipy==1.8.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/.pfnci/linux/tests/rocm-5-2.Dockerfile b/.pfnci/linux/tests/rocm-5-2.Dockerfile
--- a/.pfnci/linux/tests/rocm-5-2.Dockerfile
+++ b/.pfnci/linux/tests/rocm-5-2.Dockerfile
@@ -33,3 +33,5 @@ RUN pyenv install 3.9.6 && \
     pip install -U setuptools pip
 
 RUN pip install -U 'numpy==1.23.*' 'scipy==1.8.*' 'optuna==2.*' 'cython==0.29.*'
+RUN pip uninstall -y cuda-python && \
+    pip check
diff --git a/tests/cupyx_tests/scipy_tests/sparse_tests/test_coo.py b/tests/cupyx_tests/scipy_tests/sparse_tests/test_coo.py
--- a/tests/cupyx_tests/scipy_tests/sparse_tests/test_coo.py
+++ b/tests/cupyx_tests/scipy_tests/sparse_tests/test_coo.py
@@ -860,6 +860,7 @@ def test_rmul_dense_ndim3(self):
                 x * m
 
     @pytest.mark.xfail(
+        scipy_available and
         numpy.lib.NumpyVersion(scipy.__version__) >= '1.8.0rc1' and
         numpy.lib.NumpyVersion(scipy.__version__) <= '1.9.0rc1',
         reason='See scipy/15210')
diff --git a/tests/cupyx_tests/scipy_tests/special_tests/test_basic.py b/tests/cupyx_tests/scipy_tests/special_tests/test_basic.py
--- a/tests/cupyx_tests/scipy_tests/special_tests/test_basic.py
+++ b/tests/cupyx_tests/scipy_tests/special_tests/test_basic.py
@@ -3,7 +3,6 @@
 import cupy
 import numpy
 import pytest
-import scipy.special  # NOQA
 
 import cupyx.scipy.special
 from cupy import testing
diff --git a/tests/cupyx_tests/scipy_tests/special_tests/test_log_softmax.py b/tests/cupyx_tests/scipy_tests/special_tests/test_log_softmax.py
--- a/tests/cupyx_tests/scipy_tests/special_tests/test_log_softmax.py
+++ b/tests/cupyx_tests/scipy_tests/special_tests/test_log_softmax.py
@@ -4,8 +4,6 @@
 from cupy import testing
 import cupyx.scipy.special  # NOQA
 
-import scipy.special  # NOQA
-
 
 atol = {'default': 1e-6, cupy.float64: 1e-14}
 rtol = {'default': 1e-6, cupy.float64: 1e-14}
diff --git a/tests/cupyx_tests/scipy_tests/special_tests/test_softmax.py b/tests/cupyx_tests/scipy_tests/special_tests/test_softmax.py
--- a/tests/cupyx_tests/scipy_tests/special_tests/test_softmax.py
+++ b/tests/cupyx_tests/scipy_tests/special_tests/test_softmax.py
@@ -5,8 +5,6 @@
 import cupyx
 import cupyx.scipy.special  # NOQA
 
-import scipy.special  # NOQA
-
 
 atol = {
     'default': 1e-6,
diff --git a/tests/cupyx_tests/scipy_tests/special_tests/test_sph_harm.py b/tests/cupyx_tests/scipy_tests/special_tests/test_sph_harm.py
--- a/tests/cupyx_tests/scipy_tests/special_tests/test_sph_harm.py
+++ b/tests/cupyx_tests/scipy_tests/special_tests/test_sph_harm.py
@@ -1,6 +1,5 @@
 import cupy as cp
 import pytest
-import scipy.special  # NOQA
 
 import cupyx.scipy.special  # NOQA
 from cupy import testing
diff --git a/tests/cupyx_tests/scipy_tests/stats_tests/test_morestats.py b/tests/cupyx_tests/scipy_tests/stats_tests/test_morestats.py
--- a/tests/cupyx_tests/scipy_tests/stats_tests/test_morestats.py
+++ b/tests/cupyx_tests/scipy_tests/stats_tests/test_morestats.py
@@ -5,8 +5,6 @@
 import cupyx
 import cupyx.scipy.stats  # NOQA
 
-import scipy.stats  # NOQA
-
 
 atol = {
     cupy.float16: 5e-3,