How Can I Safely Specify the Python Path in OpenCV?

Python 2.7 and Python 3.8 are fixed versions on AGX Xavier. Trying to casually change them causes issues with the display manager, which is problematic.

I need to specify the Python path(Python3.9) to install OpenCV, but the path doesn’t change with update-alternatives

script: install_opencv.sh
.
.
.
cmake -D WITH_CUDA=ON
-D WITH_CUDNN=ON
-D CUDA_ARCH_BIN=“7.2”
-D CUDA_ARCH_PTX=“”
-D OPENCV_GENERATE_PKGCONFIG=ON
-D OPENCV_EXTRA_MODULES_PATH=…/…/opencv_contrib-${version}/modules
-D WITH_GSTREAMER=ON
-D WITH_LIBV4L=ON
-D BUILD_opencv_python3=ON
-D BUILD_TESTS=OFF
-D BUILD_PERF_TESTS=OFF
-D BUILD_EXAMPLES=OFF
-D CMAKE_BUILD_TYPE=RELEASE
-D CMAKE_INSTALL_PREFIX=/usr/local …
-D PYTHON_DEFAULT_EXECUTABLE=$(which python)
make -j$(nproc)

result :
.
.
.

– 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 (ver 3.8.10)
– Libraries: /usr/lib/aarch64-linux-gnu/libpython3.8.so (ver 3.8.10)
– numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.17.4)
– install path: lib/python3.8/dist-packages/cv2/python-3.8


How can I safely change the Python path? Is there an inernal command to do this?

Hi,

Please find below’s topic for the info:

Thanks.

Thank you for your response!

Despite running the install OpenCV shell script. the path is not being set.

Did somthing go wrong in the Python 3.9 installation process?

– python install
$ sudo apt install software-properties-common # add-apt-repository
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt update # update package
$ sudo apt install python3.9-dev
$ sudo apt install python3.9-dbg

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 1
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2
$ sudo update-alternatives --config python # python3.9 selection

—cmake with opencv

echo “------------------------------------”
echo “** Download opencv “${version}” (2/4)”
echo “------------------------------------”
mkdir $folder
cd ${folder}
curl -L https://github.com/opencv/opencv/archive/${version}.zip -o opencv-${version}.zip
curl -L https://github.com/opencv/opencv_contrib/archive/${version}.zip -o opencv_contrib-${version}.zip
unzip opencv-${version}.zip
unzip opencv_contrib-${version}.zip
rm opencv-${version}.zip opencv_contrib-${version}.zip
cd opencv-${version}/

echo “------------------------------------”
echo “** Build opencv “${version}” (3/4)”
echo “------------------------------------”
mkdir release
cd release/
cmake -D WITH_CUDA=ON
-D WITH_CUDNN=ON
-D CUDA_ARCH_BIN=“7.2”
-D CUDA_ARCH_PTX=“”
-D OPENCV_GENERATE_PKGCONFIG=ON
-D OPENCV_EXTRA_MODULES_PATH=…/…/opencv_contrib-${version}/modules
-D WITH_GSTREAMER=ON
-D WITH_LIBV4L=ON
-D BUILD_opencv_python3=ON
-D BUILD_TESTS=OFF
-D BUILD_PERF_TESTS=OFF
-D BUILD_EXAMPLES=OFF
-D CMAKE_BUILD_TYPE=RELEASE
-D CMAKE_INSTALL_PREFIX=/usr/local …
-D PYTHON3_EXECUTABLE=/usr/bin/python3.9
-D PYTHON_LIBRARIES=/usr/lib/python3.9/config-3.9-aarch64-linux-gnu/libpython3.9.so
make -j$(nproc)

echo “------------------------------------”
echo “** Install opencv “${version}” (4/4)”
echo “------------------------------------”
sudo make install
echo ‘export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH’ >> ~/.bashrc
echo ‘export PYTHONPATH=/usr/local/lib/python3.9/site-packages/:$PYTHONPATH’ >> ~/.bashrc
source ~/.bashrc

echo “** Install opencv “${version}” successfully”
echo “** Bye :)”

—result

– 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.9 (ver 3.8.10)
– Libraries: /usr/lib/aarch64-linux-gnu/libpython3.8.so (ver 3.8.10)
– numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.17.4)
– install path: lib/python3.8/dist-packages/cv2/python-3.8

– Python (for build): /usr/bin/python

I was able to switch to Python3.9 using a virtual environment!

Thank you for your help.

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