0
$\begingroup$

I am trying to run (and debug) a ROS2 node made in Python with the PyCharm IDE.

The results won't be exactly the same as ros2 run ..., but it seems directly running the relevant script is at least close: python src/my_project/my_node/node.py

So in PyCharm I add the Python 3.8 interpreter I use for ROS2 and open the project. So far so good, by adding C:\dev\ros2_humble\Lib\site-packages to the interpreter path I get correct code completion etc.

However, when I try to run my file I am prompted with this error:

Traceback (most recent call last):
File "C:\Users\name\project\node.py", line 12, in <module>
    import rclpy   File "C:\dev\ros2_humble\Lib\site-packages\rclpy\__init__.py", line 49, in <module>
    from rclpy.signals import install_signal_handlers
File "C:\dev\ros2_humble\Lib\site-packages\rclpy\signals.py", line 15, in <module>
    from rclpy.exceptions import InvalidHandle
File "C:\dev\ros2_humble\Lib\site-packages\rclpy\exceptions.py", line 15, in <module>
    from rclpy.impl.implementation_singleton import rclpy_implementation as _rclpy
File "C:\dev\ros2_humble\Lib\site-packages\rclpy\impl\implementation_singleton.py", line 32, in <module>
    rclpy_implementation = import_c_library('._rclpy_pybind11', package)
File "C:\dev\ros2_humble\Lib\site-packages\rpyutils\import_c_library.py", line 39, in import_c_library
    return importlib.import_module(name, package=package)
File "C:\Program Files\Python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level) ImportError: DLL load failed while importing _rclpy_pybind11:

The specified module could not be found.
The C extension 'C:\dev\ros2_humble\Lib\site-packages\rclpy\_rclpy_pybind11.cp38-win_amd64.pyd' failed to be imported while being present on the system.
Please refer to 'https://docs.ros.org/en/{distro}/Guides/Installation-Troubleshooting.html#import-failing-even-with-library-present-on-the-system' for possible solutions

I noticed I get the exact same error when running the script from a fresh (unsourced) terminal:

C:\dev\ros2_humble\.venv\Scripts\python.exe C:\Users\name\project\node.py

How could I circumvent this in PyCharm?

$\endgroup$
1
  • $\begingroup$ I should note: starting pycharm64.exe from inside a sourced terminal seems to have no effect on the environment variables in the PyCharm runs. $\endgroup$
    – Roberto
    Nov 16, 2023 at 13:48

1 Answer 1

0
$\begingroup$

Partial answer already. I was experimenting running the script from a regular terminal.

Running:

C:\dev\ros2_humble\.venv\Scripts\python.exe C:\Users\name\project\node.py

Gives the error: (which is not unexpected)

ModuleNotFoundError: No module named 'rclpy'

I can avoid this by setting PYTHONPATH:

$Env:PYTHONPATH="C:\dev\ros2_humble\Lib\site-packages"; C:\dev\ros2_humble\.venv\Scripts\python.exe C:\Users\name\project\node.py

Now I get the same error as with PyCharm:

ImportError: DLL load failed while importing _rclpy_pybind11: The specified module could not be found.

And I found I can fix this by expanding PATH with the bin/ directory of ROS2:

$Env:PYTHONPATH="C:\dev\ros2_humble\Lib\site-packages"; $Env:PATH = "C:\dev\ros2_humble\bin;" + $Env:PATH; C:\dev\ros2_humble\.venv\Scripts\python.exe C:\Users\name\project\node.py

Now my node runs!

It seems you can apply the same with PyCharm, edit the run configuration and under "Environment" change "Environment Variables:" to override the PATH variable. However, when doing this, the error is not fixed...
A quick test with import os; os.environ["Path"] at the start of my script shows that PATH is not modified at all.

I'm not sure how to continue here.

EDIT: I got it, I need to type "path" under environment variables (no capitals) and I need to specify it in full (i.e. I cannot extend it), but now it works!

$\endgroup$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.