# Opencv 4.6.0 with cuda support were successfully built for both python2.7 and python3.8, but cuda unavailable

**URL:** <https://forums.developer.nvidia.com/t/opencv-4-6-0-with-cuda-support-were-successfully-built-for-both-python2-7-and-python3-8-but-cuda-unavailable/271391>\
**Category:** Jetson Xavier NX\
**Tags:** opencv, pytorch, cudnn, cuda\
**Created:** [November 1, 2023, 5:20am UTC](https://forums.developer.nvidia.com/t/opencv-4-6-0-with-cuda-support-were-successfully-built-for-both-python2-7-and-python3-8-but-cuda-unavailable/271391 "2023-11-01T05:20:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iris980125](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@iris980125](https://forums.developer.nvidia.com/u/iris980125)\
**Post date:** [November 1, 2023, 5:20am UTC](https://forums.developer.nvidia.com/t/opencv-4-6-0-with-cuda-support-were-successfully-built-for-both-python2-7-and-python3-8-but-cuda-unavailable/271391/1 "2023-11-01T05:20:17Z")

</div>

Hi I am struggling with activating CUDA (maybe version of pytorch, opencv and python?.)

I am using Jetson Xavier NX developer kit, installed _ **Jetpack 5.0.2** _ so _ **CUDA 11.4 ver and cuDNN 8.4 ver** _ came together.  
To use GPU support I deleted default _ **opencv** _ (4.5.4) of Jetpack and rebuilt _ **4.6.0** _ by modifying Cmake -D attribute of JetsonHack([GitHub - jetsonhacks/nano\_build\_opencv: Build OpenCV on Nvidia Jetson Nano](https://github.com/jetsonhacks/nano_build_opencv).

Here comes problem - `import cv2` in both python version were available, but `cv2.cuda.getCudaEnabledDeviceCount()` return 0 and `torch.cuda.is_available()` return False.  
I downloaded wheel file in this page ([PyTorch for Jetson - #1367 by 19565337738](https://forums.developer.nvidia.com/t/pytorch-for-jetson/72048/1367)) and followed installation guide. And also I checked proper CUDA version for _ **pytorch 1.11.0** _ is CUDA 11.4 by torch.version.cuda in python script - so I can be assure version is not cause of my problem.

When I check cv2.getBuildInformation(), **python (for build) is printed like this : /usr/bin/python2.7**

[attachment for more information - in python3.8]

```auto
>>> print(cv2.getBuildInformation())

General configuration for OpenCV 4.6.0 =====================================
  Version control: unknown

  Extra modules:
    Location (extra): /home/seo/opencv_contrib-4.6.0/modules
    Version control (extra): unknown

  Platform:
    Timestamp: 2023-11-01T00:25:15Z
    Host: Linux 5.10.104-tegra aarch64
    CMake: 3.16.3
    CMake generator: Unix Makefiles
    CMake build tool: /usr/bin/make
    Configuration: RELEASE

  CPU/HW features:
    Baseline: NEON FP16

  C/C++:
    Built as dynamic libs?: YES
    C++ standard: 11
    C++ Compiler: /usr/bin/c++ (ver 9.4.0)
    C++ flags (Release): -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
    C++ flags (Debug): -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
    C Compiler: /usr/bin/cc
    C flags (Release): -fsigned-char -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
    C flags (Debug): -fsigned-char -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
    Linker flags (Release): -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined  
    Linker flags (Debug): -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined  
    ccache: NO
    Precompiled headers: NO
    Extra dependencies: m pthread cudart_static -lpthread dl rt nppc nppial nppicc nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cudnn cufft -L/usr/local/cuda-11.4/lib64 -L/usr/lib/aarch64-linux-gnu
    3rdparty dependencies:

  OpenCV modules:
    To be built: alphamat aruco barcode bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python2 python3 quality rapid reg rgbd saliency sfm shape stereo stitching structured_light superres surface_matching text tracking video videoio videostab viz wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
    Disabled: world
    Disabled by dependency: -
    Unavailable: java julia matlab ovis ts
    Applications: apps
    Documentation: NO
    Non-free algorithms: YES

  GUI: QT5
    QT: YES (ver 5.12.8 )
      QT OpenGL support: YES (Qt5::OpenGL 5.12.8)
    OpenGL support: YES (/usr/lib/aarch64-linux-gnu/libGL.so /usr/lib/aarch64-linux-gnu/libGLU.so)
    VTK support: YES (ver 6.3.0)

  Media I/O: 
    ZLib: /usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.11)
    JPEG: /usr/lib/aarch64-linux-gnu/libjpeg.so (ver 80)
    WEBP: /usr/lib/aarch64-linux-gnu/libwebp.so (ver encoder: 0x020e)
    PNG: /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.37)
    TIFF: /usr/lib/aarch64-linux-gnu/libtiff.so (ver 42 / 4.1.0)
    JPEG 2000: OpenJPEG (ver 2.3.1)
    OpenEXR: /usr/lib/aarch64-linux-gnu/libImath.so /usr/lib/aarch64-linux-gnu/libIlmImf.so /usr/lib/aarch64-linux-gnu/libIex.so /usr/lib/aarch64-linux-gnu/libHalf.so /usr/lib/aarch64-linux-gnu/libIlmThread.so (ver 2_3)
    HDR: YES
    SUNRASTER: YES
    PXM: YES
    PFM: YES

  Video I/O:
    FFMPEG: YES
      avcodec: YES (58.54.100)
      avformat: YES (58.29.100)
      avutil: YES (56.31.100)
      swscale: YES (5.5.100)
      avresample: YES (4.0.0)
    GStreamer: YES (1.16.3)
    v4l/v4l2: YES (linux/videodev2.h)
    Xine: YES (ver 1.2.9)

  Parallel framework: pthreads

  Trace: YES (with Intel ITT)

  Other third-party libraries:
    Lapack: YES (/usr/lib/aarch64-linux-gnu/liblapack.so /usr/lib/aarch64-linux-gnu/libcblas.so /usr/lib/aarch64-linux-gnu/libatlas.so)
    Eigen: YES (ver 3.3.7)
    Custom HAL: YES (carotene (ver 0.0.1))
    Protobuf: build (3.19.1)

  NVIDIA CUDA: YES (ver 11.4, CUFFT CUBLAS FAST_MATH)
    NVIDIA GPU arch: 72
    NVIDIA PTX archs:

  cuDNN: YES (ver 8.4.1)

  OpenCL: YES (no extra features)
    Include path: /home/seo/opencv-4.6.0/3rdparty/include/opencl/1.2
    Link libraries: Dynamic load

  Python 2:
    Interpreter: /usr/bin/python2.7 (ver 2.7.18)
    Libraries: /usr/lib/aarch64-linux-gnu/libpython2.7.so (ver 2.7.18)
    numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.16.5)
    install path: lib/python2.7/dist-packages/cv2/python-2.7

  Python 3:
    Interpreter: /usr/bin/python3.8 (ver 3.8.10)
    Libraries: /usr/lib/aarch64-linux-gnu/libpython3.8.so (ver 3.8.10)
    numpy: /home/seo/.local/lib/python3.8/site-packages/numpy/core/include (ver 1.24.4)
    install path: /home/seo/.pyenv/versions/3.8.10/lib/python3.8/site-packages/cv2/python-3.8

  Python (for build): /usr/bin/python2.7

  Java:                          
    ant: NO
    JNI: NO
    Java wrappers: NO
    Java tests: NO

  Install to: /usr/local
-----------------------------------------------------------------

>>> cv2.cuda.getCudaEnabledDeviceCount()
0
>>> torch.cuda.is_available()
False
>>> exit()

```

I think this is the main reason because when I try to `import torch` in python 2.7, it returns

> ‘no module named torch’

but same action works in python3.8… but still, `cv2.cuda.getCudaEnabledDeviceCount()` returns 0.

Can anybody answer my question? Is REALLY version of python used for building openCV cause this problem? How can I end this issue? I am struggling with this problem for about 2 weeks so would be very appriciate with your help

In addition, I changed default python version of bash shell to 3.8 but it didn’t change anything, failed to build opencv with python 3.8 . Do you know how to force this?

IF this is not the main reason, **installed CUDA itself** has problem? This is output of `nvcc --version`

```auto
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_May__4_00:02:26_PDT_2022
Cuda compilation tools, release 11.4, V11.4.239
Build cuda_11.4.r11.4/compiler.31294910_0

```

---

<div class="post-metadata">

**Author:** ![AastaLLL](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/aastalll/32/14043_2.png) [@AastaLLL](https://forums.developer.nvidia.com/u/AastaLLL)\
**Post date:** [November 1, 2023, 8:48am UTC](https://forums.developer.nvidia.com/t/opencv-4-6-0-with-cuda-support-were-successfully-built-for-both-python2-7-and-python3-8-but-cuda-unavailable/271391/3 "2023-11-01T08:48:11Z")

</div>

Hi,

Do you run the test within a container?  
If yes, please add `--system-site-packages` to access the host package.

Or you can check our container below which has both OpenCV and PyTorch installed.

> **[NVIDIA L4T ML | NVIDIA NGC](https://catalog.ngc.nvidia.com/orgs/nvidia/containers/l4t-ml)**
>
> Get started on your AI journey quickly on Jetson. The Machine learning container contains TensorFlow, PyTorch, JupyterLab, and other popular ML and data science frameworks such as scikit-learn, scipy, and Pandas pre-installed in a Python 3.6...

Thanks.

---

<div class="post-metadata">

**Author:** ![iris980125](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@iris980125](https://forums.developer.nvidia.com/u/iris980125)\
**Post date:** [November 2, 2023, 12:32am UTC](https://forums.developer.nvidia.com/t/opencv-4-6-0-with-cuda-support-were-successfully-built-for-both-python2-7-and-python3-8-but-cuda-unavailable/271391/4 "2023-11-02T00:32:51Z")

</div>

@AastaLLL No, I’m not familiar with L4T so I didn’t tried container yet  
Is it better than creating virtual environment by conda?

---

<div class="post-metadata">

**Author:** ![AastaLLL](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/aastalll/32/14043_2.png) [@AastaLLL](https://forums.developer.nvidia.com/u/AastaLLL)\
**Post date:** [November 2, 2023, 4:20am UTC](https://forums.developer.nvidia.com/t/opencv-4-6-0-with-cuda-support-were-successfully-built-for-both-python2-7-and-python3-8-but-cuda-unavailable/271391/5 "2023-11-02T04:20:32Z")

</div>

Hi,

No, just in case you are facing the error due to the virtual environment setting.

For OpenCV, could you try the below script instead?  
[https://github.com/AastaNV/JEP/blob/master/script/install\_opencv4.6.0\_Jetson.sh](https://github.com/AastaNV/JEP/blob/master/script/install_opencv4.6.0_Jetson.sh)

For PyTorch, we now only provide the package for Python3.  
Please try it with Python3 instead.

Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/system/32/68080_2.png) [@system](https://forums.developer.nvidia.com/u/system)\
**Post date:** [November 21, 2023, 5:29am UTC](https://forums.developer.nvidia.com/t/opencv-4-6-0-with-cuda-support-were-successfully-built-for-both-python2-7-and-python3-8-but-cuda-unavailable/271391/7 "2023-11-21T05:29:12Z")

</div>

Closed on November 21, 2023, 5:29am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
