ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ros2 can't access custom python module

asked 2023-07-24 00:53:24 -0500

nona gravatar image

updated 2023-07-27 02:08:54 -0500

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

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"],
},

)

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
0

answered 2023-07-26 15:54:18 -0500

mustafakurban gravatar image

updated 2023-07-26 15:54:54 -0500

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

edit flag offensive delete link more

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?

nona gravatar image nona  ( 2023-07-27 02:07:57 -0500 )edit
0

answered 2023-07-25 21:06:25 -0500

billy gravatar image

updated 2023-07-25 21:29:14 -0500

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.

edit flag offensive delete link more

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.

nona gravatar image nona  ( 2023-07-26 01:49:24 -0500 )edit

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.

billy gravatar image billy  ( 2023-07-26 15:30:37 -0500 )edit
0

answered 2023-07-24 03:44:51 -0500

MSanMon gravatar image

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

edit flag offensive delete link more

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.

nona gravatar image nona  ( 2023-07-24 08:02:07 -0500 )edit

Question Tools

Stats

Asked: 2023-07-24 00:53:24 -0500

Seen: 559 times

Last updated: Jul 27 '23