<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Darsh Jobanputra on Medium]]></title>
        <description><![CDATA[Stories by Darsh Jobanputra on Medium]]></description>
        <link>https://medium.com/@darshj.tech?source=rss-1e9d07949ec5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*HSssZLhT6DboxxFPslo5xg.jpeg</url>
            <title>Stories by Darsh Jobanputra on Medium</title>
            <link>https://medium.com/@darshj.tech?source=rss-1e9d07949ec5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 22:19:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@darshj.tech/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How I finally got YDLidar-SDK’s Python bindings to build]]></title>
            <link>https://medium.com/@darshj.tech/how-i-finally-got-ydlidar-sdks-python-bindings-to-build-beb12686dca6?source=rss-1e9d07949ec5------2</link>
            <guid isPermaLink="false">https://medium.com/p/beb12686dca6</guid>
            <dc:creator><![CDATA[Darsh Jobanputra]]></dc:creator>
            <pubDate>Fri, 05 Sep 2025 02:41:07 GMT</pubDate>
            <atom:updated>2025-09-05T03:30:55.105Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Clear, copy-paste steps for X2/X4 users — what the docs and many tutorials miss, plus a working test script.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/812/1*4CKW5tjVDO1zG21r_zTlmQ.png" /><figcaption><strong>Saving you from the hours of debugging, that I went through!</strong></figcaption></figure><h3><strong>Introduction:</strong></h3><p>If you tried building the YDLidar-SDK and import ydlidar failed — you’re not alone. The repo and many community tutorials often show cmake .. and make but don’t emphasize the steps needed to build the Python bindings. This article explains exactly what’s missing, why it happens, and gives a copy-paste solution that worked for me on YDLidar X2/X4.</p><h3>What actually causes the Python binding to be skipped</h3><p>Open the top-level CMakeLists.txt and you’ll find the key logic:</p><pre>find_package(SWIG)<br>find_package(PythonInterp)<br>find_package(PythonLibs)<br>...<br># build ydlidar sdk python version<br>if(${SWIG_FOUND} AND ${PYTHONLIBS_FOUND})<br>    message(STATUS &quot;build python API....&quot;)<br>    include_directories(python)<br>    add_subdirectory(python)<br>endif()</pre><p>So: if SWIG/Python libs are not detected at configure time, the python/ subtree is not built. Also, some CMake scripts check cache variables (e.g. BUILD_PYTHON) inside python/CMakeLists.txt. Passing -DBUILD_PYTHON=ON makes the build intent explicit and avoids surprises.</p><h3>Why this matters</h3><p>Many guides and the official YDLidar-SDK/doc/howto/how_to_build_and_install.md file from the <a href="https://github.com/YDLIDAR/YDLidar-SDK/tree/master">YDLidar-SDK Github Repo</a> show a generic cmake .. step but don’t make obvious that the Python binding is an <em>optional</em> target. If the Python target isn’t enabled, make builds the C++ library and examples but <strong>no</strong> ydlidar.py or _ydlidar.so appear — so import ydlidar raises ModuleNotFoundError. The official X2 user manual covers hardware and ROS usage but does not document this Python-binding build flag (see <a href="https://d1c6gk3tn6ydje.cloudfront.net/2036899223840006144/f3762c3b418a020bdd10a039332f97f7.pdf">YDLidar X2 User Manual</a>).</p><p>Additionally, community posts such as <a href="https://medium.com/@amnahhmohammed/tinkering-with-ros-ydlidar-setting-up-ydlidars-sdk-f5618da3493a">this one</a> by <a href="https://medium.com/u/52d2ee454b5c">Amnah Ebrahim</a> sometimes reference ydlidar_test.py (or similar) that are not present in the SDK python/examples/ folder — those scripts were created by the author of that post, so readers expecting a ready-made example are left confused.</p><h3>The exact working steps (copy/paste)</h3><ol><li>Add your user to the dialout group:</li></ol><pre>sudo usermod -aG dialout $USER<br># then logout/login or run:<br>newgrp dialout</pre><p>2. Install build dependencies (Ubuntu / Debian):</p><pre>sudo apt update<br>sudo apt install -y git cmake build-essential python3-dev python3-pip swig</pre><p>3. Clone the SDK:</p><pre>cd ~/src<br>git clone https://github.com/YDLIDAR/YDLidar-SDK.git<br>cd YDLidar-SDK</pre><p>4. Build <strong>with Python enabled</strong>:</p><pre>mkdir -p build &amp;&amp; cd build<br>cmake .. -DBUILD_PYTHON=ON<br>make -j$(nproc)</pre><p>Why: -DBUILD_PYTHON=ON explicitly enables the Python build target so the SWIG extension gets compiled. Without it you may end up with only the native C++ library and <em>no</em> Python artifacts in build/python/.</p><p>5. Add the SDK’s Python binding path to PYTHONPATH:</p><pre>export PYTHONPATH=$PYTHONPATH:~/src/YDLidar-SDK/build/python<br># To make permanent:<br>echo &#39;export PYTHONPATH=$PYTHONPATH:~/src/YDLidar-SDK/build/python&#39; &gt;&gt; ~/.bashrc<br>source ~/.bashrc</pre><p>6. Check serial-port permissions (very important for Linux access):</p><pre># See the device and permissions:<br>ls -l /dev | grep ttyUSB<br><br># be careful: this is temporary until you add a proper udev rule<br>sudo chmod 666 /dev/ttyUSB0</pre><p>7. Quick Python import test:</p><pre>python3 - &lt;&lt;&#39;PY&#39;<br>import ydlidar<br>print(&quot;YDLidar Python module imported OK&quot;)<br>PY</pre><p>If that prints the check mark you’re good.</p><h3>Full working plotting script for X2</h3><p>Update the file YDLidar/python/examples/plot_ydlidar_test.py with the following code where values are used by referring to the <a href="https://d1c6gk3tn6ydje.cloudfront.net/2036899223840006144/62c387de16f094206bfa385768078efc.pdf">YDLidar X2 datasheet</a>, for YDLidar X4 use values from <a href="https://www.elecrow.com/download/X4_Lidar_Datasheet.pdf">YDLidar X4 datasheet</a>.</p><pre>import os<br>import ydlidar<br>import time<br>import sys<br>from matplotlib.patches import Arc<br>import matplotlib.pyplot as plt<br>import matplotlib.animation as animation<br>import numpy as np<br><br>RMAX = 32.0<br><br>fig = plt.figure()<br>fig.canvas.set_window_title(&#39;YDLidar LIDAR Monitor&#39;)<br>lidar_polar = plt.subplot(polar=True)<br>lidar_polar.autoscale_view(True,True,True)<br>lidar_polar.set_rmax(RMAX)<br>lidar_polar.grid(True)<br>ports = ydlidar.lidarPortList();<br>port = &quot;/dev/ttyUSB0&quot;;<br>for key, value in ports.items():<br>    port = value;<br><br>laser = ydlidar.CYdLidar();<br>laser.setlidaropt(ydlidar.LidarPropSerialPort, port);<br>laser.setlidaropt(ydlidar.LidarPropSerialBaudrate, 115200) # original 230400 but ydlidar x2 is 115200<br>laser.setlidaropt(ydlidar.LidarPropLidarType, ydlidar.TYPE_TRIANGLE);<br>laser.setlidaropt(ydlidar.LidarPropDeviceType, ydlidar.YDLIDAR_TYPE_SERIAL);<br>laser.setlidaropt(ydlidar.LidarPropScanFrequency, 7.0); # original 10.0, but ydlidar x2 is 5-8<br>laser.setlidaropt(ydlidar.LidarPropSampleRate, 9);<br>laser.setlidaropt(ydlidar.LidarPropSingleChannel, True); #original False, but YDLidar X2 is TRUE<br>scan = ydlidar.LaserScan()<br><br>def animate(num):<br>    r = laser.doProcessSimple(scan);<br>    if r:<br>        angle = []<br>        ran = []<br>        intensity = []<br>        for point in scan.points:<br>            angle.append(point.angle);<br>            ran.append(point.range);<br>            intensity.append(point.intensity);<br>        lidar_polar.clear()<br>        lidar_polar.scatter(angle, ran, c=intensity, cmap=&#39;hsv&#39;, alpha=0.95)<br><br>ret = laser.initialize();<br>if ret:<br>    ret = laser.turnOn();<br>    if ret:<br>        ani = animation.FuncAnimation(fig, animate, interval=50)<br>        plt.show()<br>    laser.turnOff();<br>laser.disconnecting();<br>plt.close();</pre><h3>How I found the -DBUILD_PYTHON clue</h3><ol><li>I inspected CMakeLists.txt (top-level) and saw it only includes python/ when SWIG and Python dev libs are found.</li><li>I searched the repo for build variables and the python CMake logic:</li></ol><pre># search for the variable<br>grep -nR &quot;BUILD_PYTHON&quot; .<br><br># show the python CMake file<br>sed -n &#39;1,240p&#39; python/CMakeLists.txt</pre><p>Passing -DBUILD_PYTHON=ON to cmake sets a cache variable visible to subdirectories, and combined with having swig/python3-dev installed, forces the python build path to run.</p><h3>Troubleshooting checklist (if import still fails)</h3><ol><li>Confirm the Python artifacts exist:</li></ol><pre>ls ~/src/YDLidar-SDK/build/python<br># expect: ydlidar.py  _ydlidar.so  (or platform-appropriate .so)</pre><p>2. Capture CMake output and search for Python/SWIG messages:</p><pre>cd ~/src/YDLidar-SDK/build<br>cmake .. -DBUILD_PYTHON=ON |&amp; tee cmake.log<br>grep -i python cmake.log<br>grep -i swig cmake.log</pre><p>3. Reinstall dependencies if needed (<em>before</em> re-configuring):</p><pre>sudo apt install --reinstall swig python3-dev<br>rm -rf build<br>mkdir build &amp;&amp; cd build<br>cmake .. -DBUILD_PYTHON=ON<br>make -j$(nproc)</pre><p>4. Make sure the Python used at build-time is the one you run:</p><pre>which python3<br>python3 --version<br>python3 -c &quot;import sys; print(sys.executable)&quot;</pre><p><strong>Conclusion</strong></p><p>Still can’t get over the fact that this is the first Medium Article I wrote, and the reason for me to write this article was due to hours of brainrot and debugging. But cool, whatever helps the community, and prevents from going through the same mess I went through! Keep tinkering guys!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=beb12686dca6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>