Solvedlibrealsense Installing pyrealsense2 on MacOS?

Required Info
Camera Model { D435 }
Operating System & Version {MacOS Catalina }

Issue Description

Hello all, this is a pretty high level question but I cannot really find the answer to--how do I install pyrealsense2 on macOS? It seems like pip install is not supported on Mac, so I am reading I have to build Python bindings from the source. However, I already have librealsense2 installed using brew, would there be a conflict? It seems pretty confusing to me and it'd be helpful if I could get a clear pipeline of how to get pyrealsense2 on my Mac.

Thanks!

Steps I have followed:

  1. git clone https://github.com/IntelRealSense/librealsense
  2. mkdir build & cd build
  3. cmake .. -G Xcode -DBUILD_EXAMPLES=true -DBUILD_WITH_OPENMP=false -DHWM_OVER_XU=false **-DBUILD_PYTHON_BINDINGS:bool=true**
  4. make -j4

Now I'm stuck at *** No targets specified and no makefile found. Stop.

Also, when I get the project built, how do I use the wrapper? Can I just write import pyrealsense2 on top of my python modules and it'll magically work? I don't see any steps where it links the wrapper to projects

I'm extremely confused

27 Answers

✔️Accepted Answer

Okay I got it, for future Mac users this is how I did it:

  1. git clone https://github.com/IntelRealSense/librealsense
  2. mkdir build & cd build
  3. cmake ../ -DBUILD_PYTHON_BINDINGS=bool:true
  4. make -j4
  5. sudo make install
  6. You should still be in the /build directory so now you want to find the .so files. cd wrapper & cd python
  7. You'll see a bunch of .so files in here, just copy them and then paste them to the root project directory of your project that you want to use pyrealsense2 in. Then you can just import pyrealsense2 in your project modules using import pyrealsense2

thanks @neilyoung-- alternative to steps 6 and 7, export PYTHONPATH=$PYTHONPATH:/usr/local/lib or add that to your .bashrc

Other Answers:

@ducciopiovani I tried this on MacOS Catalina, python 3.7, anaconda environment.
Build librealsense2 and install, then make symbolic link of realsense sdk libraries into the python virtual environment.

In my case] env name : rs2 , python version : 3.7
cd /opt/anaconda3/envs/rs2/lib/python3.7
ln -s /usr/local/lib/pyrealsense2.cpython-37m-darwin.so pyrealsense2.so
ln -s /usr/local/lib/pybackend2.cpython-37m-darwin.so pybackend2.so

I did the following, and it worked for me on macOS Catalina 10.15.7. Hope it helps anyone else!

  1. Install homebrew if you don't already have it.
  2. brew install cmake libusb pkg-config
  3. git clone https://github.com/IntelRealSense/librealsense to clone librealsense
  4. cd librealsense (or cd into wherever you cloned)
  5. mkdir build && cd build
  6. Find the path to your python executable. When I run which python3, I get /Library/Frameworks/Python.framework/Versions/3.8/bin/python3, so I'll use that in Step 7.
  7. cmake .. -DBUILD_EXAMPLES=true -DBUILD_WITH_OPENMP=false -DHWM_OVER_XU=false -DBUILD_PYTHON_BINDINGS=true -G Unix\ Makefiles -DCMAKE_MACOSX_RPATH=ON -DCMAKE_INSTALL_RPATH="/usr/local/lib" -DPYTHON_EXECUTABLE=/Library/Frameworks/Python.framework/Versions/3.8/bin/python3 but replace the value of the DPYTHON_EXECUTABLE flag as appropriate for you (as you determined in Step 6)
  8. sudo make install - I got a ton of output with warnings, but progressed (slowly, but steadily).
  9. cd wrappers/python
  10. ls -l and you'll see two .so files that are NOT symlinks (don't have ->). I saw the following output:
total 63072
drwxr-xr-x  7 raj   staff       224 Jan 22 17:06 CMakeFiles
-rw-r--r--  1 raj   staff     48922 Jan 22 17:06 Makefile
-rw-r--r--  1 raj   staff      9768 Jan 22 17:06 cmake_install.cmake
-rwxr-xr-x  1 root  staff  14784952 Jan 22 17:22 pybackend2.2.41.0.cpython-38-darwin.so
lrwxr-xr-x  1 root  staff        38 Jan 22 17:22 pybackend2.2.cpython-38-darwin.so -> pybackend2.2.41.0.cpython-38-darwin.so
lrwxr-xr-x  1 root  staff        33 Jan 22 17:22 pybackend2.cpython-38-darwin.so -> pybackend2.2.cpython-38-darwin.so
-rwxr-xr-x  1 root  staff  17436208 Jan 22 17:21 pyrealsense2.2.41.0.cpython-38-darwin.so
lrwxr-xr-x  1 root  staff        40 Jan 22 17:21 pyrealsense2.2.41.cpython-38-darwin.so -> pyrealsense2.2.41.0.cpython-38-darwin.so
lrwxr-xr-x  1 root  staff        38 Jan 22 17:21 pyrealsense2.cpython-38-darwin.so -> pyrealsense2.2.41.cpython-38-darwin.so
-rw-r--r--  1 raj   staff      1221 Jan 22 17:06 pyrealsense2Config.cmake
-rw-r--r--  1 raj   staff      1978 Jan 22 17:06 pyrealsense2ConfigVersion.cmake
drwxr-xr-x  3 raj   staff        96 Jan 22 17:06 third_party

which told me that pyrealsense2.2.41.0.cpython-38-darwin.so and pybackend2.2.41.0.cpython-38-darwin.so were the two "real" .so files I was looking for.

  1. Now, you can do many things, as others have said. However, I wanted to be able to import pyrealsense2 universally (as if I had simply run pip install pyrealsense2. So, I copied the two files to my site-packages folder. I ran python3 -m site --user-site to get the path of this folder, which was /Users/raj/Library/Python/3.8/lib/python/site-packages (replace the python3 command with the path to your python executable if necessary). HOWEVER, you also need to rename or symlink these files once copied to site-packages since they include version numbering. So, in the following cp commands, I copied and renamed in one command. I ran cp pyrealsense2.2.41.0.cpython-38-darwin.so /Users/raj/Library/Python/3.8/lib/python/site-packages/pyrealsense2.cpython-38-darwin.so for pyrealsense and cp pybackend2.2.41.0.cpython-38-darwin.so /Users/raj/Library/Python/3.8/lib/python/site-packages/pybackend2.cpython-38-darwin.so for pybackend.
  2. Now I can fire up python3 anywhere and import pyrealsense2 seems to work!
  • sudo make install after make
  • export PYTHONPATH=$PYTHONPATH:/usr/local/lib either on console or in your .bashrc
  • import pyrealsense2 from everywhere

More Issues: