Python 导入错误:没有名为串行的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23378208/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:52:59  来源:igfitidea点击:

ImportError: No module named serial

pythonmacospython-3.xmodulepyserial

提问by user3587244

I have a script written in Python 3 with 3.3.5 installed, and I am getting this error from the terminal whenever I try to run it. I am using a Mac, OSX 10.7.5

我有一个用 Python 3 编写的脚本,安装了 3.3.5,每当我尝试运行它时,我都会从终端收到此错误。我使用的是 Mac,OSX 10.7.5

I have already installed pyserial(using pip) for python 3. In order to do this, I first installed pip using:

我已经pyserial为 python 3安装了(使用 pip)。为了做到这一点,我首先使用以下方法安装了 pip:

$ curl -O http://python-distribute.org/distribute_setup.py
$ sudo python3 distribute_setup.py
$ curl -O https://raw.githubusercontent.com/pypa/pip/master/contrib/get-pip.py
$ sudo python3 get-pip.py

I then installed pyserialwith:

然后我安装pyserial了:

$sudo pip3 install pyserial

I run the script with Python Launcher and I get the error:

我使用 Python Launcher 运行脚本,但出现错误:

ImportError: No module named serial

The error is at the line which says

错误是在说

import serial

I located pyserialin /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages. I am really stumped and have no clue why I am getting this error.

我设pyserial/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages。我真的很难过,不知道为什么我会收到这个错误。

I tried the following in Terminal:

我在终端中尝试了以下操作:

$ python3
>>> import serial
>>> serial
<module 'serial' from '/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/serial/__init__.py'>

To me it looks like there isn't an issue with the directory. Is that a fair assumption, since Python instantly looks through that directory when I try it with the command line, or is it different when I'm running a script?

对我来说,目录似乎没有问题。这是一个公平的假设,因为当我使用命令行尝试 Python 时,它会立即查看该目录,还是在我运行脚本时会有所不同?

I'm very new to all of this so any sort of help and patience would be hugely appreciated.

我对这一切都很陌生,因此非常感谢任何形式的帮助和耐心。

Thanks.

谢谢。

EDIT: For anyone else looking at this having a similar problem, I didn't reallyfix it, but I worked around it by just running my program with eclipse using the PyDev plugin. If that's an option for you, worked like a charm for me.

编辑:对于其他有类似问题的人,我并没有真正解决它,但我只是通过使用 PyDev 插件在 eclipse 中运行我的程序来解决它。如果这对你来说是一个选择,那么对我来说就像一个魅力。

回答by Shrey

First use the two commands (which pip& which python) to get the exact path of the python and pip commands. After this only you will be able to determine the exact issue.

首先使用两个命令(which pip& which python)来获取 python 和 pip 命令的确切路径。只有在此之后,您才能确定确切的问题。

回答by BitByte_Bake

On my ubuntu 14.4 with python 2.7 as default, I installed pyserial for python3 (which my IDE is using actually) with the help of following command:

在默认使用 python 2.7 的 ubuntu 14.4 上,我在以下命令的帮助下为 python3(我的 IDE 实际使用的)安装了 pyserial:

sudo apt-get install python3-serial

回答by Jostikas

For me, the problem was solved by running the script importing serial in a user (not administrative) context. Windows, not Mac, so YMMV.

对我来说,问题是通过在用户(非管理)上下文中运行脚本导入序列来解决的。Windows,而不是 Mac,所以 YMMV。