ROS工作空间下编写python脚本导包异常问题解决_Serial

#! /usr/bin/env python

import serial
serial = serial.Serial("/dev/ttyUSB0",1000000,timeout = 0.5)

在自己的工作空间下编写串口数据收发脚本时,需要导入serial包,但在VScode里面写代码时,serial 字段为白色,说明没有这个包,也就是无法导入成功。

tys@tys:~$ sudo pip install pyserial 
The directory '/home/tys/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/tys/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: pyserial in /usr/lib/python2.7/dist-packages

安装pyserial包发现其实已经装过该包了,就在 /usr/lib/python2.7/dist-packages下,但是导入失败。

tys@tys:~$ python
Python 2.7.17 (default, Feb 27 2021, 15:10:58) 
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/tys/seed_ws/devel/lib/python2.7/dist-packages', '/home/tys/cpr_ws/devel/lib/python2.7/dist-packages', '/home/tys/hgd_ws/devel/lib/python2.7/dist-packages', '/home/tys/catkin_ws/devel/lib/python2.7/dist-packages', '/home/tys/cute_ws/devel/lib/python2.7/dist-packages', '/home/tys/robotis_ws/devel/lib/python2.7/dist-packages', '/home/tys/ur_ws/devel/lib/python2.7/dist-packages', '/opt/ros/melodic/lib/python2.7/dist-packages', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/tys/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/wx-3.0-gtk3']
>>> 

看了下系统的sys.path,发现好像每个工作空间下有一个python库,看目前自己写脚本的这个工作空间的 seed_ws/devel/lib/python2.7/dist-packages下并没有serial这个包,所以将/usr/lib/python2.7/dist-packages下的serial包拷贝到seed_ws/devel/lib/python2.7/dist-packages下,source下.bashrc(可能不需要,个人习惯),重新导入serial包就可以自己显示出来了,搞定!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值