A simple step for ROS system to compile Python package into executable file

Time:2021-10-13

Python install pattern development rules

This article describes how to compile Python into executable files under ROS system. The steps are relatively simple. Please strictly implement them to avoid omissions

1. Download necessary files

git clone https://gitee.com/alen2020/ros_python_install_mode_files
##Take the cmake directory, setup.py and install.sh under this directory to the directory of your node
cp -r cmake setup.py  car_mqtt_api/

If dynamic parameters are used, you also need to copy install.sh and make the following modifications (if dynamic parameters are needed, please ignore the following)

cp install.sh  car_mqtt_api/

##You need to modify the package under install.sh_ Name, which is modified to the current package name

package_name=
package_path=`rospack find ${package_name}`
if [ $# -eq 1 ]; then
    package_path=$1
fi
#Note that by default, the. Py file is placed in the SRC directory. If it is placed in scripts, it needs to be changed to scripts 
#My default prefix directory for ROS workspace is / root / ROS / catkin_ WS /, if not, please modify
mkdir $package_path/src/$package_name/cfg
cp  /root/ROS/catkin_ws/devel/lib/python2.7/dist-packages/$package_name/cfg/* $package_path/src/$package_name/cfg

2. Transform the directory structure

##In the original structure, the. Py file should be placed in the scripts folder
ls

在这里插入图片描述

The main function is left in the current directory, and the called file is placed in the subdirectory, which is the same as the package name

在这里插入图片描述

3. Create cmakelist rule (with reference relationship)

1) Modify cmakelist under the current package
Add the following contents, where add_ Subdirectory is adjusted according to the directory where your Python files are stored

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
# include CMake module for Cython
include(UseCython)
add_custom_target(ReplicatePythonSourceTree ALL ${CMAKE_COMMAND} -P
  ${CMAKE_CURRENT_SOURCE_DIR}/cmake/ReplicatePythonSourceTree.cmake
  ${CMAKE_CURRENT_BINARY_DIR}
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_subdirectory(src)

##If dynamic parameter call is used, the following statement needs to be added
add_custom_target(install.sh ALL)

add_custom_command(TARGET install.sh
POST_BUILD
COMMAND /bin/sh ${PROJECT_SOURCE_DIR}/install.sh ${PROJECT_SOURCE_DIR})
)

2) Add cmakelists.txt in the scripts directory
Each referenced file should be written in. Note that the file name of the main function should be written at the end

##Add subdirectory
add_subdirectory(mypackage)

cython_add_standalone_executable(1 MAIN_MODULE 1.py mypackage/2.py 1.py)
install(TARGETS 1
  RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})

3) Add cmakelists.txt under the subdirectory of the scripts directory
All referenced files need to add rules

cython_add_module(2 2.py)
set_target_properties(2
  PROPERTIES
  LIBRARY_OUTPUT_DIRECTORY
  ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_PYTHON_DESTINATION})
install(TARGETS 2
  LIBRARY DESTINATION ${CATKIN_PACKAGE_PYTHON_DESTINATION})
Omit the following...

4. Non reference relationship (single file)

Write twice


cython_add_standalone_executable(car_mqtt_api_main MAIN_MODULE car_mqtt_api_main.py car_mqtt_api_main.py)

install(TARGETS car_mqtt_api_main
  RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})

5. How to reference packages

You need to add a subdirectory prefix after changing the rules

primary
from mypackage import 2
After modification
from mypackage.2 import function

6. CFG dynamic parameter adjustment

As mentioned earlier, the subdirectory should have the same name as this node, because after compilation, it will not be named after the subdirectory, but after the node name. The compiled executable file will be placed in lib / python2.7/dist packages of devel or install, and the cfg file in the subdirectory will be read when running in source mode

Such as car_ mqtt_ The executable programs with dynamic parameters after API compilation are placed in the following directory


devel/lib/python2.7/dist-packages/mypackage/cfg

At this time, you need to copy the files in this directory to a subdirectory


mkdir -p mypackage/scritps/cfg
cp */devel/lib/python2.7/dist-packages/mypackage/cfg scripts/mypackage/cfg

7. Common bugs

Module not found: atinstall/lib/python2.7/dist-packages/car_controlPlace under directory__init__.pyfile

This is the end of this article on compiling Python packages into executable files by ROS system. For more information about compiling Python packages into executable files, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!