Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install pyrealsense2 .so and __init__.py #13079

Merged
merged 1 commit into from
Jun 26, 2024

Conversation

eufrizz
Copy link

@eufrizz eufrizz commented Jun 24, 2024

Fix for python bindings, issue #13080 , where pyrealsense2 is not installed when built from source.
Added:

  • install rule for libraries
  • install rule for __init__.py which was necessary for python to find the module

The first was added in an else statement because the installation is done when pybackend2 is used

At make install, you now see these extra lines:

-- Installing: /usr/lib/python3/dist-packages/pyrealsense2/pyrealsense2.cpython-310-aarch64-linux-gnu.so.2.56.0
-- Installing: /usr/lib/python3/dist-packages/pyrealsense2/pyrealsense2.cpython-310-aarch64-linux-gnu.so.2.56
-- Set runtime path of "/usr/lib/python3/dist-packages/pyrealsense2/pyrealsense2.cpython-310-aarch64-linux-gnu.so.2.56.0" to ""
-- Installing: /usr/lib/python3/dist-packages/pyrealsense2/pyrealsense2.cpython-310-aarch64-linux-gnu.so
...
-- Up-to-date: /usr/lib/python3/dist-packages/pyrealsense2/__init__.py

Other related issues:

#10635
#6449
#6964 (comment)
#12894
#3062
and more...

@sysrsbuild
Copy link
Collaborator

Can one of the admins verify this patch?

else() # not BUILD_LEGACY_PYBACKEND

install(TARGETS pyrealsense2
LIBRARY DESTINATION ${PYTHON_INSTALL_DIR}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @eufrizz
I'm wondering whether all these references to PYTHON_INSTALL_DIR should really be to CMAKECONFIG_PY_INSTALL_DIR, like the other install destinations do?
Have you tried with a non-standard install location?

Copy link
Author

@eufrizz eufrizz Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, CMAKECONFIG_PY_INSTALL_DIR is a different location where the cmake config files are placed, allowing consumers to use find_package(pyrealsense2) in their projects. The location looks something like: /usr/local/lib/cmake/pyrealsense2/pyrealsense2Targets.cmake.
This is not where the .so files should be installed. You'll notice a few lines above that PYTHON_INSTALL_DIR is used when built with pybackend2, but since your commit 953bb06 this is not the default, and libraries are no longer installed.

Copy link
Author

@eufrizz eufrizz Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PYTHON_INSTALL_DIR variable is set here in /CMake/external_pybind11.cmake:

if( CMAKE_VERSION VERSION_LESS 3.12 )

The rsutils bindings are done in the same way:
As for non-standard install locations (i.e. setting CMAKE_INSTALL_PREFIX), it will ignore them and install in the Python dist-packages regardless

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated it to now install ARCHIVE (I assume this is useful for building with SHARED_LIBS=false), as well as RUNTIME and EXPORT, as is done in the install a few lines above. There is already an EXPORT rule for pyrealsense2Targets below so I've left that out.

@@ -168,6 +168,15 @@ install(TARGETS pybackend2 pyrealsense2
)

target_include_directories(pybackend2 PRIVATE ../../src)

else() # not BUILD_LEGACY_PYBACKEND
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User should be able to build and install both pyrealsense2 and pybackend2

Copy link
Author

@eufrizz eufrizz Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They still can, the pybackend2 part has not changed, unless I'm mistaken?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you places the new ‘install’ in an “else” section.
Why do we want to install pyrealsense only if we do not install pybackend?
Or I am missing something 😀

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the "if" statement, a few lines above, you'll see that pyrealsense2 and pybackend2 are both installed. The issue I'm fixing is that if you don't install pybackend2, then pyrealsense2 is never installed!

@eufrizz eufrizz mentioned this pull request Jun 25, 2024
Copy link
Collaborator

@maloel maloel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @eufrizz, much appreciated!

@Nir-Az Nir-Az merged commit bb13f5e into IntelRealSense:development Jun 26, 2024
21 checks passed
@Nir-Az
Copy link
Collaborator

Nir-Az commented Jun 26, 2024

Thanks!

@gfx73
Copy link

gfx73 commented Aug 9, 2024

Hi @eufrizz! Great work. Is there any workaround for building pyrealsense with 2.55.1 build? I tried to set -DBUILD_LEGACY_PYBACKEND=true but this way my build is failing with error 2. Also this flag is not documented in https://dev.intelrealsense.com/docs/build-configuration. I will be waiting for the release)

UPD: the error I am getting during making buld

[ 16%] Building CXX object wrappers/python/CMakeFiles/pybackend2.dir/__/__/src/libusb/device-libusb.cpp.o
/usr/src/librealsense/wrappers/python/pybackend.cpp:17:10: fatal error: core/options.h: No such file or directory
   17 | #include "core/options.h"   // Workaround for the missing DLL_EXPORT template
      |          ^~~~~~~~~~~~~~~~
compilation terminated.
^[[A^[[A^[[A^[[A^[[A^[[Amake[2]: *** [wrappers/python/CMakeFiles/pybackend2.dir/build.make:76: wrappers/python/CMakeFiles/pybackend2.dir/pybackend.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....

@eufrizz
Copy link
Author

eufrizz commented Aug 9, 2024

@gfx73 just apply the changes to the CMakeLists in this PR to your repo and it should work.

@gfx73
Copy link

gfx73 commented Aug 10, 2024

@eufrizz thank you for your reply!

I know this would be a workaround. But I'm building docker image with ci. So applying a patch during build is kinda crutch.

@gfx73
Copy link

gfx73 commented Aug 10, 2024

I was advised to come to this PR to discuss the problem with pybackend build. Please look at this.

Thank you for your attention!

@monajalal
Copy link

@eufrizz
@Nir-Az
@MartyG-RealSense

So I downloaded the file from @eufrizz for CMakeLists.txt and compared it with the one I have, they are the same.

I still have the same exact problem

image

mona@mona:~/librealsense/wrappers/python/examples$ python
Python 3.10.12 (main, Mar 22 2024, 16:50:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyrealsense2 as rs
>>> rs.pipeline()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'pyrealsense2' has no attribute 'pipeline'
>>> import pyrealsense2.pyrealsense2 as rs
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pyrealsense2.pyrealsense2'

So, I also found these in my home dir (I assume created by intel):

mona@mona:~/librealsense_build/librealsense-master/build/release$ ls
total 46M
-rw-rw-r--  1 mona mona 757K Aug 26 18:17 librealsense-file.a
-rw-rw-r--  1 mona mona 1.1M Aug 26 18:18 librsutils.a
-rwxrwxr-x  1 mona mona 363K Aug 26 18:18 rs-embed
-rw-rw-r--  1 mona mona 1.6M Aug 26 18:18 libfw.a
lrwxrwxrwx  1 mona mona   49 Aug 26 18:19 pyrsutils.cpython-310-aarch64-linux-gnu.so.2.55 -> pyrsutils.cpython-310-aarch64-linux-gnu.so.2.55.1
lrwxrwxrwx  1 mona mona   47 Aug 26 18:19 pyrsutils.cpython-310-aarch64-linux-gnu.so -> pyrsutils.cpython-310-aarch64-linux-gnu.so.2.55
-rwxrwxr-x  1 mona mona 499K Aug 26 18:19 pyrsutils.cpython-310-aarch64-linux-gnu.so.2.55.1
lrwxrwxrwx  1 mona mona   23 Aug 26 18:30 librealsense2.so.2.55 -> librealsense2.so.2.55.1
lrwxrwxrwx  1 mona mona   21 Aug 26 18:30 librealsense2.so -> librealsense2.so.2.55
-rwxrwxr-x  1 mona mona  11M Aug 26 18:30 librealsense2.so.2.55.1
lrwxrwxrwx  1 mona mona   26 Aug 26 18:32 librealsense2-gl.so.2.55 -> librealsense2-gl.so.2.55.1
lrwxrwxrwx  1 mona mona   24 Aug 26 18:32 librealsense2-gl.so -> librealsense2-gl.so.2.55
-rwxrwxr-x  1 mona mona 2.2M Aug 26 18:32 librealsense2-gl.so.2.55.1
-rwxrwxr-x  1 mona mona  58K Aug 26 18:32 rs-hello-realsense
-rwxrwxr-x  1 mona mona 690K Aug 26 18:33 rs-software-device
-rwxrwxr-x  1 mona mona 471K Aug 26 18:33 rs-capture
-rwxrwxr-x  1 mona mona  75K Aug 26 18:33 rs-callback
-rwxrwxr-x  1 mona mona 139K Aug 26 18:33 rs-save-to-disk
-rwxrwxr-x  1 mona mona 492K Aug 26 18:33 rs-multicam
-rwxrwxr-x  1 mona mona 456K Aug 26 18:33 rs-pointcloud
-rwxrwxr-x  1 mona mona 841K Aug 26 18:33 rs-align
-rwxrwxr-x  1 mona mona 851K Aug 26 18:34 rs-align-gl
-rwxrwxr-x  1 mona mona 841K Aug 26 18:34 rs-align-advanced
-rwxrwxr-x  1 mona mona 486K Aug 26 18:35 rs-sensor-control
-rwxrwxr-x  1 mona mona 493K Aug 26 18:35 rs-measure
-rwxrwxr-x  1 mona mona  19K Aug 26 18:35 rs-depth
-rwxrwxr-x  1 mona mona  14K Aug 26 18:35 rs-color
-rwxrwxr-x  1 mona mona  14K Aug 26 18:35 rs-distance
-rwxrwxr-x  1 mona mona 864K Aug 26 18:35 rs-post-processing
lrwxrwxrwx  1 mona mona   52 Aug 26 18:36 pyrealsense2.cpython-310-aarch64-linux-gnu.so.2.55 -> pyrealsense2.cpython-310-aarch64-linux-gnu.so.2.55.1
lrwxrwxrwx  1 mona mona   50 Aug 26 18:36 pyrealsense2.cpython-310-aarch64-linux-gnu.so -> pyrealsense2.cpython-310-aarch64-linux-gnu.so.2.55
-rwxrwxr-x  1 mona mona 1.6M Aug 26 18:36 pyrealsense2.cpython-310-aarch64-linux-gnu.so.2.55.1
-rwxrwxr-x  1 mona mona 849K Aug 26 18:36 rs-record-playback
-rwxrwxr-x  1 mona mona 771K Aug 26 18:36 rs-motion
-rwxrwxr-x  1 mona mona 475K Aug 26 18:36 rs-gl
-rwxrwxr-x  1 mona mona 423K Aug 26 18:36 rs-convert
-rwxrwxr-x  1 mona mona 878K Aug 26 18:36 rs-hdr
-rwxrwxr-x  1 mona mona 183K Aug 26 18:36 rs-fw-logger
-rwxrwxr-x  1 mona mona 279K Aug 26 18:36 rs-enumerate-devices
-rwxrwxr-x  1 mona mona 156K Aug 26 18:37 rs-record
-rwxrwxr-x  1 mona mona 401K Aug 26 18:37 rs-terminal
-rwxrwxr-x  1 mona mona 260K Aug 26 18:37 rs-fw-update
-rwxrwxr-x  1 mona mona 388K Aug 26 18:37 rs-data-collect
-rwxrwxr-x  1 mona mona 6.6M Aug 26 18:44 realsense-viewer
-rwxrwxr-x  1 mona mona 6.6M Aug 26 18:44 rs-depth-quality
-rwxrwxr-x  1 mona mona 706K Aug 26 18:44 rs-benchmark
-rwxrwxr-x  1 mona mona 1.8M Aug 26 18:44 rs-rosbag-inspector

Which is also not the version 2.56??? why??? anyhow, I thought soft linking pyrealsense2.cpython-310-aarch64-linux-gnu.so.2.55.1 to pyrealsense2.so may resolve the problem but it doesn't. So could you please suggest a fix?

I am curious why it shows version 2.55? I did checkout the commit hash for 2.56 and you can see it here:

mona@mona:~/librealsense$ git branch -a
* (HEAD detached at origin/r/256)
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/R/2542
  remotes/origin/R/5e/ww07
  remotes/origin/development
  remotes/origin/feature/aus
  remotes/origin/gh-pages
  remotes/origin/jupyter
  remotes/origin/master
  remotes/origin/r/255
  remotes/origin/r/256
  remotes/origin/r/5e/ww12
  remotes/origin/r/5e/ww13
  remotes/origin/r/5e/ww20
  remotes/origin/r/5e/ww23
  remotes/origin/r/5e/ww28

Then proceeded to change line 46 of scripts/libuvc_installation.sh to

cmake ../ -DFORCE_LIBUVC=true -DCMAKE_BUILD_TYPE=release -DBUILD_PYTHON_BINDINGS=ON -DPYTHON_INCLUDE_DIR=/usr/include/python3.10 -DPYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu -DPYTHON_EXECUTABLE=/usr/bin/python3.10

Anyhow, after running ./scripts/libuvc_installation.sh I still get the same error.

I also have added

mona@mona:~/librealsense$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/opt/tmh/bin:/opt/tmh/bin:/home/mona/librealsense_build/librealsense-master/build/release

and still same.

Any help is really appreciated.

@monajalal
Copy link

I understand this is not a solution but the following worked

mona@mona:~/librealsense/wrappers/python/examples$ cp /home/mona/librealsense_build/librealsense-master/build/release/pyrealsense2.cpython-310-aarch64-linux-gnu.so .
mona@mona:~/librealsense/wrappers/python/examples$ cp /home/mona/librealsense_build/librealsense-master/build/release/pyrealsense2.cpython-310-aarch64-linux-gnu.so.2.55.1 .
mona@mona:~/librealsense/wrappers/python/examples$ cp /home/mona/librealsense_build/librealsense-master/build/release/pyrealsense2.cpython-310-aarch64-linux-gnu.so.2.55 .
mona@mona:~/librealsense/wrappers/python/examples$ ls
total 4.8M
drwxrwxr-x 2 mona mona 4.0K Aug 23 15:24 box_dimensioner_multicam
-rw-rw-r-- 1 mona mona  213 Aug 23 15:24 auto_calibration_config.json
-rw-rw-r-- 1 mona mona 3.6K Aug 23 15:24 align-depth2color.py
-rw-rw-r-- 1 mona mona 8.8K Aug 23 15:24 depth_ucal_example.py
-rw-rw-r-- 1 mona mona 9.6K Aug 23 15:24 depth_auto_calibration_example.py
-rw-rw-r-- 1 mona mona 2.8K Aug 23 15:24 readme.md
-rw-rw-r-- 1 mona mona 2.7K Aug 23 15:24 read_bag_example.py
-rw-rw-r-- 1 mona mona 1.9K Aug 23 15:24 python-tutorial-1-depth.py
-rw-rw-r-- 1 mona mona 4.3K Aug 23 15:24 python-rs400-advanced-mode-example.py
-rw-rw-r-- 1 mona mona  16K Aug 23 15:24 pyglet_pointcloud_viewer.py
-rw-rw-r-- 1 mona mona 3.1K Aug 23 15:24 pybackend_example_1_general.py
-rw-rw-r-- 1 mona mona 2.5K Aug 23 15:24 opencv_viewer_example.py
-rw-rw-r-- 1 mona mona  11K Aug 23 15:24 opencv_pointcloud_viewer.py
-rw-rw-r-- 1 mona mona 2.9K Aug 23 15:24 frame_queue_example.py
-rw-rw-r-- 1 mona mona 1.6K Aug 23 15:24 export_ply_example.py
drwxrwxr-x 2 mona mona 4.0K Aug 23 15:25 ethernet_client_server
drwxrwxr-x 5 mona mona 4.0K Aug 27 10:01 ..
-rw-rw-r-- 1 mona mona   87 Aug 27 11:24 __init__.py
-rwxrwxr-x 1 mona mona 1.6M Aug 27 11:31 pyrealsense2.cpython-310-aarch64-linux-gnu.so
-rwxrwxr-x 1 mona mona 1.6M Aug 27 11:31 pyrealsense2.cpython-310-aarch64-linux-gnu.so.2.55.1
drwxrwxr-x 4 mona mona 4.0K Aug 27 11:32 .
-rwxrwxr-x 1 mona mona 1.6M Aug 27 11:32 pyrealsense2.cpython-310-aarch64-linux-gnu.so.2.55

mona@mona:~/librealsense/wrappers/python/examples$ python opencv_viewer_example.py 

@MartyG-RealSense
Copy link
Collaborator

Thanks so much @monajalal for sharing what worked for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants