• 【ROS】ros-noetic和anaconda联合使用【教程】


    【ROS】ros-noetic和anaconda联合使用【教程】

    1. 安装anaconda

    在Ubuntu20.04中安装anaconda可以参考博主的这篇博客,这里就不再赘述。下面简要介绍下博主使用的环境

    2. 创建虚拟环境

    Anaconda基本环境管理语法如下

    • 创建虚拟环境
    conda create -n <your-virtualenv-name> python=3.8
    
    • 1
    • 激活虚拟环境
    conda activate <your-virtualenv-name>
    
    • 1

    激活虚拟环境后使用pip install rospkg rospy catkin_tools来安装ros依赖

    #in your virtual env
    pip install rospkg rospy catkin_tools
    
    • 1
    • 2

    3. 查看python解释器路径

    笔者使用的是ros-noetic版本,安装的anaconda3,在ros-noetic中的原生python版本为python3.8.10,如果使用的ros-melodic版本,那么原生python应该三是python2.7

    下面我们验证一下基本信息是否正确,打开一个terminal

    which python3
    
    • 1
    Image

    默认的python3解释器路径是/usr/bin/python3

    然后,查看anaconda虚拟环境中的python3解释器路径

    conda activate <your_virtualenv_name>
    which python3
    
    • 1
    • 2

    比如笔者的虚拟环境名字是metaRL,查看的结果如下

    Image

    4. 在虚拟环境中使用任意的包

    笔者在这个环境中配置了torch-v2.0.1具体教程参考这篇博客,这个所需要的包可以是任何你想使用的包。我们验证一下是否能顺利导入

    conda activate <your_virtualenv_name>
    python
    import rospy
    print(rospy.__file__)
    import torch
    print(torch.__file__)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    如下所示,我们顺利导入了rospytorch并且查看了其存放路径

    Image

    5. 创建工作空间和ros功能包进行测试

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    然后创建一个测试脚本

    roscd test_ros_python
    mkdir scripts
    touch test_node.py
    chmod +x test_node.py
    
    • 1
    • 2
    • 3
    • 4

    然后在test_node中编写以下内容

    #! /usr/bin/env python
    # coding :utf-8
    
    print('\n*****************************************\n\t[test libraries]:\n')
    import rospy
    import torch
    
    print(' - 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 >>>>>')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    这样进行测试之后发现,并不能顺利导入我所需要的torch包,如下图所示

    Image

    这个结果与我们之前在终端中的结果相违背,那么可以详细查看一下python包的搜索路径,利用sys

    #! /usr/bin/env python
    # coding :utf-8
    
    print('\n*****************************************\n\t[test libraries]:\n')
    import rospy
    import sys
    for p in sys.path:
        print(p)
    
    # print()
    # import torch
    
    print(' - 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 >>>>>')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    查看的搜索路径如下

    Image

    奇怪的是这里并没有我们之前在终端中得到的路径

    Image

    我们可以手动将这个路径添加到python的搜索路径当中

    /home/<your-user-name>/anaconda3/envs/<your-virturalenv-name>/lib/python3.8/site-packages
    
    • 1

    得到如下的脚本文件

    #! /usr/bin/env python
    # coding :utf-8
    
    print('\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 torch
    
    print(' - 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 >>>>>')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    成功导入了torch

    Image

    Reference

    【Linux】Ubuntu20.04版本配置pytorch环境2023.09.05【教程】

    【ROS】如何在ROS中使用anaconda虚拟环境?

    ROS图像的Deeplab v3+实时语义分割(ROS+Pytorch)

  • 相关阅读:
    How to install PostgreSQL in Centos8
    数据预处理|数据清洗|使用Pandas进行异常值清洗
    基于matlab的图像复原仿真GUI
    Ubuntu系统下Nginx安装
    只有一个人可以改变世界,那就是你自己!
    人工智能 PyTorch(一)
    vue3中 | 使用Pinia 进行状态管理 | pinia优化重复请求
    PHP+Redis 发布订阅
    java spring cloud 企业电子招标采购系统源码:营造全面规范安全的电子招投标环境,促进招投标市场健康可持续发展
    1.查找存在于数组x但不存在于数组y的元素np.setdiff1d()2.查找两个数组交集以外(不同时存在于两个数组中)的元素np.setxor1d()
  • 原文地址:https://blog.csdn.net/qq_44940689/article/details/133813086