Python pyserial:没有名为工具的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14108428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pyserial: No module named tools
提问by charmoniumQ
I have installed the latest pySerial on my Ubuntu box with python 2.7.2, and it works fine for most things, but whenever I try to import the 'tools' package, it says that it can't find 'tools'. The documentation for pySerialexplicitly references this 'tools' package.
我已经使用 python 2.7.2 在我的 Ubuntu 机器上安装了最新的 pySerial,它在大多数情况下都可以正常工作,但是每当我尝试导入“工具”包时,它都会说找不到“工具”。pySerial的文档明确引用了这个“工具”包。
>>> from serial import tools
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
import serial.tools
ImportError: No module named tools
and when I:
当我:
>>> serial.VERSION
'2.5'
which is the latest version according to Source Forge
根据Source Forge,这是最新版本
So why can't I get to the 'tools' package of pySerial?
那么为什么我无法访问 pySerial 的“工具”包?
采纳答案by user1881957
Use pipto install pyserial. First install pip:
使用pip安装pyserial。首先安装pip:
sudo apt-get install python-pip
After that install pyserial:
安装后pyserial:
sudo pip install pyserial
回答by user1881957
You have to call the functions of module this way:
您必须以这种方式调用模块的功能:
from serials import tools
For example: If I want to call sqrtfunction from mathmodule, I would do:
例如:如果我想sqrt从math模块调用函数,我会这样做:
from math import sqrt
import math.sqrt **is wrong.**
回答by SingleNegationElimination
It looks like the ubuntu package does not quite match up with upstream. compare the official pySerial package on their SVN:
看起来 ubuntu 包与上游不太匹配。比较他们 SVN 上的官方 pySerial 包:
http://pyserial.svn.sourceforge.net/viewvc/pyserial/trunk/pyserial/serial/
http://pyserial.svn.sourceforge.net/viewvc/pyserial/trunk/pyserial/serial/
to the ubuntu package in launchpad:
到启动板中的 ubuntu 包:
http://bazaar.launchpad.net/~ubuntu-branches/ubuntu/raring/pyserial/raring/files/head:/serial/
http://bazaar.launchpad.net/~ubuntu-branches/ubuntu/raring/pyserial/raring/files/head:/serial/
Tools is missing there, too. It does look like they keep the miniterm.pyscript, and install it... somewhere; I'm not versed enough in deb packaging to understand where, though.
那里也缺少工具。看起来他们确实保留了miniterm.py脚本,然后将其安装到...某处;不过,我对 deb 包装不够精通,无法理解在哪里。
回答by Thomas
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”。重命名后一切正常。
回答by chenhui
yes,use python-pip install ,,,then ,,,upgrade,,,the version of serial will be 2.7 ...it workes
是的,使用 python-pip install ,,,then ,,,upgrade,,,serial 的版本是 2.7 ...它可以工作
回答by Alejandro
Just in case that doing "pip install --upgrade" doesn't work(as happened to me), in Linux you can check if you have a serial package in /usr/lib/python2.7/dist-packages, the new serial packaged the one that pip install goes to /usr/**local**/lib/python2.7/dist-packages/serial, for some reason the one in usr/lib/python2.7/has precedence and the module tools doesn't exist in that version of pyserial. Changing names or deleting the directory solve the problem.
以防万一执行“pip install --upgrade”不起作用(就像我发生的那样),在 Linux 中,您可以检查您是否有一个串行包/usr/lib/python2.7/dist-packages,新的串行打包了 pip install 去的那个/usr/**local**/lib/python2.7/dist-packages/serial,对于某些人原因是 inusr/lib/python2.7/具有优先权,并且该版本的 pyserial 中不存在模块工具。更改名称或删除目录即可解决问题。
回答by user5915698
The pyserial-2.4-py2.7.egg in the site-packages folder was getting imported instead of from serial in the site-packages folder. Once I renamed this file so that it no longer imported from this it worked fine. You can easily debug this by doing
site-packages 文件夹中的 pyserial-2.4-py2.7.egg 被导入而不是从 site-packages 文件夹中的 serial 导入。一旦我重命名了这个文件,使它不再从这个文件中导入它就可以正常工作。您可以通过执行此操作轻松调试
import serial
serial._ _path_ _
to see where serial is getting imported from.
查看从哪里导入序列号。
回答by greenchad
From the pyserial webpage: https://pyserial.readthedocs.io/en/latest/pyserial.html
从 pyserial 网页:https://pyserial.readthedocs.io/en/latest/pyserial.html
They recommend that you use the command: python -m pip install pyserial
他们建议您使用命令:python -m pip install pyserial
That worked for me. I don't know enough about pip to undersand the difference from just a regular pip install pyserial, but it worked for me (after much frustration).
那对我有用。我对 pip 知之甚少,无法理解与常规 pip install pyserial 的区别,但它对我有用(在经历了很多挫折之后)。
回答by rocky
In my case, I installed serialbut not pyserialafter getting the following error.
就我而言,我安装了serial但没有pyserial在收到以下错误后安装。
ImportError: No module named 'serial'
Then import serialwas ok, but from serial import toolscan't work.
然后没问题import serial,但from serial import tools不能工作。
Just sudo pip uninstall serialand sudo pip install pyserial
只是sudo pip uninstall serial和sudo pip install pyserial
Hope to help people like me.
希望能帮助像我这样的人。

