关于ubuntu设置sh文件开机自启动python3和sudo python3问题
- 说明
- 系统为 ubuntu22.04
- python是python3.10.12
- ros系统为ros2 humble
- 背景
- 解决方法
- 补充
说明
系统为 ubuntu22.04
python是python3.10.12
ros系统为ros2 humble
背景
将一个py文件设置为开机自启动,服务启动时报错,显示找不到rclpy模块。
直接运行./autotest.sh没有问题,sudo ./autotest.sh报错如下。
解决方法
1、打开两个终端,分别输入以下命令,如下图所示。
python3
import sys
sys.path
和
sudo python3
import sys
sys.path
2、查看上图打印结果,找到sudo python3那边没有的部分,如上图红框显示。
3、将缺少部分写在XXX.pth文件中,并保存于 /usr/local/lib/python3.10/dist-packages/路径下(路径根据自己实际情况修改,应该可以写在一个pth文件中吧,我也没查,就直接写了两个),如下图所示。
4、此时运行还会缺少一些so文件,报错如下:
Traceback (most recent call last): File “/home/tls/ws/firbot/ui_design/bringup.py”, line 8, in import rclpy File “/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/init.py”, line 49, in from rclpy.signals import install_signal_handlers File “/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/signals.py”, line 15, in from rclpy.exceptions import InvalidHandle File “/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/exceptions.py”, line 15, in from rclpy.impl.implementation_singleton import rclpy_implementation as _rclpy File “/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/impl/implementation_singleton.py”, line 32, in rclpy_implementation = import_c_library(‘._rclpy_pybind11’, package) File “/opt/ros/humble/lib/python3.10/site-packages/rpyutils/import_c_library.py”, line 39, in import_c_library return importlib.import_module(name, package=package) File “/usr/lib/python3.10/importlib/init.py”, line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ImportError: librcl_action.so: cannot open shared object file: No such file or directory The C extension ‘/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/_rclpy_pybind11.cpython-310-x86_64-linux-gnu.so’ failed to be imported while being present on the system. Please refer to ‘https://docs.ros.org/en/{distro}/Guides/Installation-Troubleshooting.html#import-failing-even-with-library-present-on-the-system’ for possible solutions
解决方法为:找到你所缺文件(如librcl_action.so)路径,创建一下软链接就可以了。
sudo ln -s /opt/ros/humble/lib/lib* /usr/lib
补充
1、开机自启动的autotest.sh文件放在了/etc/init.d/目录下,如下图所示。
2、都设置完之后可以启动一下看一下是否启动成功,下图是启动成功的状态。
sudo service autotest.sh start
sudo service autotest.sh status
也可通过命令关闭。
sudo service autotest.sh stop
最后,重启系统就可以看下是否设置成功了。