重慶政府a(chǎn)pp渝快辦深圳債務(wù)優(yōu)化公司
【ROS】ros-noetic和anaconda聯(lián)合使用【教程】
文章目錄
- 【ROS】ros-noetic和anaconda聯(lián)合使用【教程】
- 1. 安裝anaconda
- 2. 創(chuàng)建虛擬環(huán)境
- 3. 查看python解釋器路徑
- 4. 在虛擬環(huán)境中使用任意的包
- 5. 創(chuàng)建工作空間和ros功能包進(jìn)行測(cè)試
- Reference
1. 安裝anaconda
在Ubuntu20.04中安裝anaconda可以參考博主的這篇博客,這里就不再贅述。下面簡(jiǎn)要介紹下博主使用的環(huán)境
2. 創(chuàng)建虛擬環(huán)境
Anaconda基本環(huán)境管理語(yǔ)法如下
創(chuàng)建虛擬環(huán)境
conda create -n <your-virtualenv-name> python=3.8
激活虛擬環(huán)境
conda activate <your-virtualenv-name>
激活虛擬環(huán)境后使用pip install rospkg rospy catkin_tools
來(lái)安裝ros依賴(lài)
#in your virtual env
pip install rospkg rospy catkin_tools
3. 查看python解釋器路徑
筆者使用的是
ros-noetic
版本,安裝的anaconda3
,在ros-noetic
中的原生python版本為python3.8.10
,如果使用的ros-melodic
版本,那么原生python應(yīng)該三是python2.7
。
下面我們驗(yàn)證一下基本信息是否正確,打開(kāi)一個(gè)terminal
which python3

默認(rèn)的python3解釋器路徑是/usr/bin/python3
然后,查看anaconda虛擬環(huán)境中的python3解釋器路徑
conda activate <your_virtualenv_name>
which python3
比如筆者的虛擬環(huán)境名字是metaRL
,查看的結(jié)果如下

4. 在虛擬環(huán)境中使用任意的包
筆者在這個(gè)環(huán)境中配置了torch-v2.0.1
具體教程參考這篇博客,這個(gè)所需要的包可以是任何你想使用的包。我們驗(yàn)證一下是否能順利導(dǎo)入
conda activate <your_virtualenv_name>
python
import rospy
print(rospy.__file__)
import torch
print(torch.__file__)
如下所示,我們順利導(dǎo)入了rospy
和torch
并且查看了其存放路徑

5. 創(chuàng)建工作空間和ros功能包進(jìn)行測(cè)試
mkdir -p ~/test_ws/src
cd ~/test_ws/src/
catkin_init_workspace
catkin_create_pkg test_ros_python std_msgs rospy
cd ..
catkin_make
echo "source ~/test_ws/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc
然后創(chuàng)建一個(gè)測(cè)試腳本
roscd test_ros_python
mkdir scripts
touch test_node.py
chmod +x test_node.py
然后在test_node
中編寫(xiě)以下內(nèi)容
#! /usr/bin/env python
# coding :utf-8print('\n*****************************************\n\t[test libraries]:\n')
import rospy
import torchprint(' - rospy.__file__ = %s'%rospy.__file__)
print(' - scipy.__file__ = %s'%torch.__file__)
# check cuda is ready or not
print('cuda is {}'.format('ready' if torch.cuda.is_available() else 'not ready'))
print('\n*****************************************\n\t[finish test]\n')if __name__ == "__main__":rospy.init_node('test_node', anonymous=True)rospy.loginfo('>>>>> hello world >>>>>')
這樣進(jìn)行測(cè)試之后發(fā)現(xiàn),并不能順利導(dǎo)入我所需要的torch
包,如下圖所示

這個(gè)結(jié)果與我們之前在終端中的結(jié)果相違背,那么可以詳細(xì)查看一下python包的搜索路徑,利用sys
庫(kù)
#! /usr/bin/env python
# coding :utf-8print('\n*****************************************\n\t[test libraries]:\n')
import rospy
import sys
for p in sys.path:print(p)# print()
# import torchprint(' - rospy.__file__ = %s'%rospy.__file__)
# print(' - scipy.__file__ = %s'%torch.__file__)
# # check cuda is ready or not
# print('cuda is {}'.format('ready' if torch.cuda.is_available() else 'not ready'))
print('\n*****************************************\n\t[finish test]\n')if __name__ == "__main__":rospy.init_node('test_node', anonymous=True)rospy.loginfo('>>>>> hello world >>>>>')
查看的搜索路徑如下

奇怪的是這里并沒(méi)有我們之前在終端中得到的路徑

我們可以手動(dòng)將這個(gè)路徑添加到python的搜索路徑當(dāng)中
/home/<your-user-name>/anaconda3/envs/<your-virturalenv-name>/lib/python3.8/site-packages
得到如下的腳本文件
#! /usr/bin/env python
# coding :utf-8print('\n*****************************************\n\t[test libraries]:\n')
import rospy
import sys
sys.path.append('/home/sjh/anaconda3/envs/metaRL/lib/python3.8/site-packages')
for p in sys.path:print(p)print()
import torchprint(' - rospy.__file__ = %s'%rospy.__file__)
print(' - scipy.__file__ = %s'%torch.__file__)
# check cuda is ready or not
print('cuda is {}'.format('ready' if torch.cuda.is_available() else 'not ready'))
print('\n*****************************************\n\t[finish test]\n')if __name__ == "__main__":rospy.init_node('test_node', anonymous=True)rospy.loginfo('>>>>> hello world >>>>>')
成功導(dǎo)入了torch

Reference
【Linux】Ubuntu20.04版本配置pytorch環(huán)境2023.09.05【教程】
【ROS】如何在ROS中使用anaconda虛擬環(huán)境?
ROS圖像的Deeplab v3+實(shí)時(shí)語(yǔ)義分割(ROS+Pytorch)