Python使用模块Pyserial模块报

用pip安装pyserial后:sudo -H pip  install pyserial,运行新建的程序,名称为serial.py,程序中用到:import serial.toos.list_ports,但总是提示ImportError:No module named tools.list_ports,在度娘一顿乱搜后,在stack overflow看到有人问这个问题,一个个回答看过来,当看到:

I got here because I had the same problem. The strange thing was that

import serial

worked, but

from serial.tools import list_ports

didn't.

Turns out stupid me named the script "serial.py". After renaming it everything worked.

当时是五味杂陈,一时犯贱工程命名也是serial.py,当再重新命名为MySerial.py后,python MySerial.py编译时:

File "MySerial.py", line 15, in

import serial

File "/User/zhang/Desktop/python/serial_uart/serial.py", line 18, in

ImportError: No module named tools.list_ports

此时已经没有serial.py文件了,怎么报的错误还和serial.py扯上关系,最后发现该文件夹下生成了一个serial.pyc的文件,删除之后运行正常,都是名字惹的祸。


你可能感兴趣的:(Python)