程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Ros2 pre basic tutorial | Python dependency lookup process

編輯:Python

The author of this series of tutorials : Little fish
official account : Fish fragrance ROS
QQ Communication group :139707339
Teaching video address : Small fish B standing
Full document address : Fish fragrance ROS Official website
Copyright notice : Reprint and commercial use are prohibited unless permitted .

python Compared with the way of introducing dependencies C++ It's too easy . In this section, Xiaoyu takes you to learn from a few examples Python Path lookup mechanism for .

1. To write ROS2 Of Python node

stay d2lros2/d2lros2/chapt2/basic newly build second_ros2_node.py, Enter the following

# Import rclpy library , If Vscode It doesn't matter to show red wavy lines
# We just put VsCode Just a notepad , Who cares what Notepad thinks about code , isn't it? ?
import rclpy
from rclpy.node import Node
# call rclcpp Initialization function for
rclpy.init()
# call rclcpp Run the loop we created second_node node
rclpy.spin(Node("second_node"))

2. function Python node

Open the terminal , Input instruction

ls
python3 second_ros2_node.py

Open a new terminal , Input

ros2 node list

perfect , Four lines of code write a ROS2 Of Python node .

So here comes the question , We import rclpy,rclpy Where is it ?python How did you find it ?

3.Python Package lookup process

Python3 function import rclpy How did you find it ? The answer is through the environment variable PYTHONPATH

Ctrl+C Interrupt node operation , Then enter the following command

echo $PYTHONPATH

result

/opt/ros/humble/lib/python3.10/site-packages:/opt/ros/humble/local/lib/python3.10/dist-packages

You will find that there is something about humble Of python route , Look under the above two directories rclpy, See if you can find rclpy

Find the first path

ls -l /opt/ros/humble/lib/python3.10/site-packages | grep rclpy

Did not find , the second

ls -l /opt/ros/humble/local/lib/python3.10/dist-packages/ | grep rclpy

eureka

drwxr-xr-x 1 root root 4096 Jun 3 04:45 rclpy
drwxr-xr-x 2 root root 4096 May 23 22:23 rclpy-3.3.4-py3.10.egg-info

4.unset experiment

Use unset Directive can delete environment variables , Try deleting PYTHONPATH Then run the code , See if you can still import rclpy.

unset
python3 second_ros2_node.py

The tips are as follows

[email protected]:~/d2lros2/d2lros2/chapt2/basic# python3 second_ros2_node.py
Traceback (most recent call last):
File "/root/d2lros2/d2lros2/chapt2/basic/second_ros2_node.py", line 3, in <module>
import rclpy
ModuleNotFoundError: No module named 'rclpy'

Please remember this error message ModuleNotFoundError: No module named 'xxx', This is what you may often encounter in your future study .

Next When encountered for the first time , You will think of the article "little fish" , Then smile scornfully at it , Then find the directory where the library is located , Add it to the environment .

5. summary

In this section, Xiaoyu took you to learn Python To write ROS2 How nodes work , And bring you to know a common mistake ModuleNotFoundError: No module named 'xxx'.

ROS2 Pre tutorial summary :

I exist because of us ,Ubuntu It also has philosophical significance

ROS2 Pre tutorial | Install... In a virtual machine Ubuntu

ROS2 Pre tutorial | Get along well with Ubuntu The common instructions of

ROS2 Pre tutorial | Get along well with Ubuntu Programming tools for

ROS2 Pre tutorial | Get along well with Ubuntu Common software

ROS2 Pre basic tutorial | Little fish teaches you to use g++ compile ROS2 node

ROS2 Pre basic tutorial | Use CMakeLists.txt compile ROS2 node

ROS2 Pre basic tutorial | Little fish teaches you to use CMake Rely on the lookup process

ROS2 Advanced tutorial summary :

ROS2 Advanced tutorial | ROS2 Anti counterfeiting of operating system of system architecture


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved