Learn how to import python modules from other packages and make them available for all the system. Also learn how to correctly install python scripts and modules.
Step 0. Create a project in ROS Development Studio(ROSDS)
ROSDS helps you follow our tutorial in a fast pace without dealing without setting up an environment locally. If you haven’t had an account yet, you can create a free account here.
Step 1. Create package
In this ROSject, let’s create a new package which contains the modules that you want to use in other packages.
cd ~/catkin_ws/src catkin_create_pkg rospy
You also need a folder which has the same name as the package under the src folder and inside it, you need a empty__init__.py file.
cd test_pkg/src mkdir test_pkg cd test_pkg touch __init__.py
Then you create the module you want under the src folder(e.g. the ClockSubscriber module in the coomon_tools_pkg). You’ll also need a setup.py file with the following content under the package to help the ROS system find this package.
from distutils.core import setup from catkin_pkg.python_setup import generate_distutils_setup setup_args = generate_disutils_setup( packages=['common_tools_pkg'], package_dir=['':'src'], ) setup(**setup_args)
In the CMakeLists.txt file, you should change it like the following
cmake_minium_required(VERSION 2.8.3) project(common_tools_pkg) ... find_package(catkin REQUIRED COMPONENTS rospy ) ... catkin_package( ) ... include_directories( ${catkin_INCLUDE_DIRS} )
Then you can compile the package
cd ~/catkin_ws catkin_make source devel/setup.bash
Then you can import the module into other scripts. For example,
from common_tools_pkg.clock_subscriber impot ClockSubscriber
That’s it. You can use modules from other packages now.
In addition, we want to talk about how to install the package in your system by changing the CMakeLists.txt like following
... file(GLOB python_scripts_DIR "scripts/*.py" ) ... install(DIRECTORY launch DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}) catkin_install_python(PROGRAMS ${python_scripts_DIR} DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})
This will install all the script and launch file to the system lib folder so that all scripts using python can access to the module.
Edit by: Tony Huang
0 Comments