python - 在ROS2中导入包中的模块

标签 python python-3.x import ros2

我已经为 ROS2 创建了一个包,并且添加了我下载的 Python 存储库。我遇到的问题是,在原始存储库中,来自自己存储库的模块是直接导入的,而在我的存储库中,我必须导入它们,在模块之前添加 ROS2 包名称,即使我从同一个存储库导入模块,例如:

将 planner_pkg.SimpleOneTrailerSystem 导入为 SimpleOneTrailerSystem

虽然我想:

导入 SimpleOneTrailerSystem

我的ROS2项目结构如下:

ros2_ws
  src
    planner
      planner_pkg
        __init__.py
        SimpleOneTrailerSystem.py
        planner_node.py
        ...
      package.xml
      setup.py

包.xml

<?xml version="1.0"?>
<package format="2">
  <name>planner_pkg</name>
  <version>0.0.1</version>
  <description>This package contains algorithm for park planner</description>

  <maintainer email=""></maintainer>
  <license>Apache License 2.0</license>

  <exec_depend>rclpy</exec_depend>
  <exec_depend>std_msgs</exec_depend>

  <!-- These test dependencies are optional
  Their purpose is to make sure that the code passes the linters -->
  <test_depend>ament_copyright</test_depend>
  <test_depend>ament_flake8</test_depend>
  <test_depend>ament_pep257</test_depend>
  <test_depend>python3-pytest</test_depend>

  <export>
    <build_type>ament_python</build_type>
  </export>
</package>

设置.py:

from setuptools import setup

package_name = 'planner_pkg'

setup(
    name=package_name,
    version='0.0.0',
    packages=[package_name],
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    author='',
    author_email='',
    maintainer='',
    maintainer_email='',
    keywords=['ROS'],
    classifiers=[
        'Intended Audience :: Developers',
        'License :: OSI Approved :: Apache Software License',
        'Programming Language :: Python',
        'Topic :: Software Development',
    ],
    description='Package containing examples of how to use the rclpy API.',
    license='Apache License, Version 2.0',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'planner_node = planner_pkg.planner_node:main',
        ],
    },
)

最佳答案

首先根据Module Search Path文档中,当您导入某些内容时,Python 会在以下位置查找该某些内容:

  • 来自内置模块
  • sys.path,这是一个列表,包含:
    • 输入脚本的目录
    • PYTHONPATH,这是一个包含目录列表的环境变量
    • 依赖于安装的默认值

其次,当您构建 ROS2 Python 包时(通过调用 colcon build 调用 ament_python 构建类型),您的 Python 代码将被复制到 install 具有如下树结构的文件夹:

install
...
├── planner_pkg
│   ├── bin
│   │   └── planner_node
│   ├── lib
│   │   └── python3.6
│   │       └── site-packages
│   │           ├── planner_pkg
│   │           │   ├── __init__.py
│   │           │   ├── planner_node.py
│   │           │   └── SimpleOneTrailerSystem.py
...

现在,当您导入 SimpleOneTrailerSystem 时,Python 将首先从内置模块中搜索它,但肯定不会在那里找到。列表中的下一个来自 sys.path。您可以在 planner_node.py 顶部添加 print(sys.path) 来查看类似以下列表的内容:

['/path/to/install/planner_pkg/bin', 
 '/path/to/install/planner_pkg/lib/python3.6/site-packages', 
 '/opt/ros/eloquent/lib/python3.6/site-packages', 
 '/usr/lib/python36.zip', 
 '/usr/lib/python3.6', 
 ...other Python3.6 installation-dependent dirs...
]

sys.path 列表中的第一个是输入脚本的 bin 文件夹。那里只有可执行文件,没有 SimpleOneTrailerSystem.py 文件/模块,因此导入将失败。

列表中的下一个是planner_pkg/lib/pythonX.X/site-packages,正如您从上面的树结构中看到的,有一个SimpleOneTrailerSystem.py 模块但它位于planner_pkg 文件夹下。所以像这样直接导入

import SimpleOneTrailerSystem

不会工作。您需要使用包文件夹来限定它,如下所示:

import planner_pkg.SimpleOneTrailerSystem

有两种方法可以解决这个问题。

  1. 导入之前修改sys.path -ing SimpleOneTrailerSystem

    import sys
    sys.path.append("/path/to/install/planner_pkg/lib/python3.6/site-packages/planner_pkg")
    
    import SimpleOneTrailerSystem
    

    此方法将 planner_pkg install 目录的路径添加到 sys.path 列表中,以便您无需指定它在后续导入中。

  2. 在运行 ROS2 节点之前修改 PYTHONPATH

    $ colcon build
    $ source install/setup.bash
    $ export PYTHONPATH=$PYTHONPATH:/path/to/install/planner_pkg/lib/python3.6/site-packages/planner_pkg
    $ planner_node
    

    此方法与第一种方法几乎相同,但不涉及任何代码更改(也不涉及重建),因为您只需要修改 PYTHONPATH 环境变量。

关于python - 在ROS2中导入包中的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57426715/

相关文章:

javascript - 通过模板输出时Python字典对象转换为JSON

python - Django 麻烦 : django. setup() 抛出 "ImportError: No module named ' project_name'"

python - 在 Linux 中达到一定大小时停止 Python 脚本写入文件

python - 将当前行值与前一行值进行比较

python - Pyserial: "module ' serial' 没有属性 'tools' "

html - 如何将多个CSS页面导入到一个CSS主页面,再将CSS主页面导入到HTML页面?

python - IPython/jupyter 笔记本中的自动更新模块

python - 从python中的其他文件导入函数

Python glob 多种文件类型

python - 你如何反转/大写字符串中的单词