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


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_path=`rospack find ${package_name}`
if [ $# -eq 1 ]; then
#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


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

# include CMake module for Cython
add_custom_target(ReplicatePythonSourceTree ALL ${CMAKE_COMMAND} -P

##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

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

cython_add_standalone_executable(1 MAIN_MODULE 1.py mypackage/2.py 1.py)
install(TARGETS 1

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

cython_add_module(2 2.py)
install(TARGETS 2
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

5. How to reference packages

You need to add a subdirectory prefix after changing the rules

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


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!