Robotics StackExchange | Archived questions

ros2 can't access custom python module

i have my own python module, which I'm trying to import in my ros2 project (installed via 'pip install -e mymodule/setup.py'). However, I keep getting the following error: "ModuleNotFoundError: No module named 'mymodule'".

I can import the module without errors when using python in the terminal, so I assume it's something about the python path or interpreter that I have to consider when working with ros.

The setup.py file of my_module looks like this:

setup(
name="my_module",
version="xx",
author="xx",
author_email="xx",
description="xx.",
long_description=long_description,
long_description_content_type="text/markdown",
url="xx",
packages=find_packages(),
classifiers=[
    "Programming Language :: Python :: 3",
],
python_requires=">=3.10",
install_requires=[
    "ray<2.0",
    "tensorflow==2.9",
    "kornia",
    "torch==1.12",
    "opencv-contrib-python",
    "rasterio",
    "pyproj",
    "numpy",
    "attrs",
    "loguru",
    "psutil",
    "numba",
    "click",
    "click_option_group",
    "geopy",
    "PyYAML",
    "python-dotenv",
    "msgpack",
    "websockets",
    "simplejpeg",
    "parse",
    "scipy",
],
extras_require={
    "dev": [
        "invoke",
        "mypy",
        "pylint",
        "isort",
        "black",
        "pytest",
        "pytest-cov",
    ],
},
entry_points={
    "console_scripts": ["my_module = my_module.cli.top_level_cli:top_level_cli"],
},

)

Asked by nona on 2023-07-24 00:53:24 UTC

Comments

Answers

In which folder is installed? In which folder are you (relative to the package) when you are able to import it using the terminal?

Asked by MSanMon on 2023-07-24 03:44:51 UTC

Comments

The root looks like this (I’m in a docker container):

  • my_package
    • setup.py
  • ros2_ws

The Python package is installed in the following path: /root/my-package/env-my-package/bin/my-package

I’m able to import the python package in the terminal when I’m in the ros2_ws.

Asked by nona on 2023-07-24 08:02:07 UTC

Your package.xml, setup.cfg, and setup.py files determine if the python script gets moved to the correct (build) folder for ROS2 launch. If that transfer doesn't happen when you 'colcon build', it won't be found at run time. I banged my head on this issue for a long time but there's only a chance it's the same issue you're having. But at least look into it. In ROS2 getting your own .py to run seems much more complicated to me than it did in ROS. But I'm also right at the edge of learning disabled when it comes to compootur things.

Let me add a bit...it not just the moving of the files because moving them manually doesn't fix it. It also impacts how the paths are defined during the build IIUC.

Asked by billy on 2023-07-25 21:06:25 UTC

Comments

I'm not sure i understand correctly. I believe that my colcon build process is working correctly, the problem is rather that i cant import the python module which i installed via pip.

Asked by nona on 2023-07-26 01:49:24 UTC

In my case I had a X.py program in my ROS2_WS that importing from another Y.py module installed on my system and it ran fine from the terminal but couldn't import Y.py when running from ROS2 RUN. I think this sounds similar to your situation but I wasn't attempting to use something installed via PIP, so maybe different situation. Also of note, the import needed to have the following format to work in ROS2 RUN.

import X.Y

where Y was the module I was importing to program X, and I had put Y.py directly into the ROS2 scr folder with X.py.
import Y worked from terminal, but import X.Y was required for ROS2 RUN.

Asked by billy on 2023-07-26 15:30:37 UTC

If you share your setup.py folder, maybe we can solve the problem. It can be related with entry_points stuff

Asked by mustafakurban on 2023-07-26 15:54:18 UTC

Comments

hi! I shared the setup.py file in the post now. I assume you mean the setup.py file of my python module, not of my ros package?

Asked by nona on 2023-07-27 02:07:57 UTC