简体   繁体   English

pySerial AttributeError:模块'serial'没有属性'Serial'

[英]pySerial AttributeError: module 'serial' has no attribute 'Serial'

I have windows 7 64 bit and Python 3.6我有 windows 7 64 位和 Python 3.6

I literally dig entire web for solving this - and nothing works sadly.我从字面上挖掘了整个 web 来解决这个问题 - 没有什么可悲的。

AttributeError                            Traceback (most recent call last)
<ipython-input-150-80ee8bd5f0f2> in <module>()
----> serial.Serial()

AttributeError: module 'serial' has no attribute 'Serial'

I tried solve this by: - checking if my file is named serial.py - reinstalling module - at first, entire module didn't work, now only it's functions.我尝试通过以下方式解决这个问题: - 检查我的文件是否名为 serial.py - 重新安装模块 - 起初,整个模块都不起作用,现在只有它的功能。 - I can't use any of its features, checking ports don't work, nothing works literally. - 我不能使用它的任何功能,检查端口不起作用,从字面上看没有任何作用。 - I checked my pip - everything is fine, module is in folder site-pacages. - 我检查了我的 pip - 一切都很好,模块在文件夹站点包中。 - If I execute some code inside pySerial files - it works. - 如果我在 pySerial 文件中执行一些代码 - 它可以工作。 But there is no serial.Serial or connecting with COM ports (which I try to do.)但是没有串行。串行或与 COM 端口连接(我尝试这样做。)

I had the same problem with Python3 3.6.3.1 installed under Cygwin.我在 Cygwin 下安装 Python3 3.6.3.1 时遇到了同样的问题。 I replaced 3.6.3.1 with 3.4.5.1 and the script worked again.我用 3.4.5.1 替换了 3.6.3.1,脚本再次运行。 OK, but not a good long term solution.好的,但不是一个好的长期解决方案。

Better solution: I reinstalled Python3 3.6.3.1, and then did "pip install pyserial" , and then I was able to run my script.更好的解决方案:我重新安装了 Python3 3.6.3.1,然后执行了"pip install pyserial" ,然后我就可以运行我的脚本了。 No additional modules show up when I do help("modules"), but something extra must have been added to serial.当我执行 help("modules") 时没有显示额外的模块,但必须向串行中添加了一些额外的模块。

There's another python module called 'serial', that you might have on your system.您的系统上可能还有另一个名为“serial”的 python 模块。 I was using a program that imports the 'serial' module, and on my Linux machine it worked when executed by one user, and failed when executed as another user.我正在使用一个导入“串行”模块的程序,在我的 Linux 机器上,它在由一个用户执行时工作,在作为另一个用户执行时失败。

When running the program as user2, I got the following traceback:以 user2 身份运行程序时,我得到以下回溯:

Traceback (most recent call last):
  File "./grabserial", line 957, in <module>
    restart_requested = grab(sys.argv[1:])
  File "./grabserial", line 365, in grab
    sd = serial.Serial()
AttributeError: 'module' object has no attribute 'Serial'

I figured out that I had the pyserial 'serial' module in /home/user1/.local/lib/python2.7/site-packages/serial/ init .pyc, but that the other user was getting the serial module from /usr/local/lib/python2.7/dist-packages/serial.我发现我在/home/user1/.local/lib/python2.7/site-packages/serial/init .pyc 中有 pyserial 'serial' 模块,但另一个用户正在从 /usr 获取串行模块/local/lib/python2.7/dist-packages/serial.

There are two different python packages that present a module called 'serial' to the sytem.有两个不同的 python 包向系统提供一个称为“串行”的模块。

One module is called 'serial' by pip (this one is a serialization library), and the other is called 'pyserial' (this one is a serial port handling library).一个模块被 pip 称为“串行”(这是一个序列化库),另一个称为“pyserial”(这是一个串行端口处理库)。

The simple fix, if what you want to do is use the serial port handling module, is to uninstall the serialization module with pip, and make sure you have the 'pyserial' installed.如果您想要使用串行端口处理模块,简单的解决方法是使用 pip 卸载序列化模块,并确保安装了“pyserial”。

In my case, I was using a program that used python 2.7 on my machine, so I did:就我而言,我在我的机器上使用了一个使用 python 2.7 的程序,所以我做了:

   $ pip2 uninstall serial
   $ pip2 install pyserial

I did this as 'root', so that the pyserial module would be installed globally (for all users).我以“root”身份执行此操作,以便全局安装 pyserial 模块(适用于所有用户)。 Apparently, user1 had run pip to install pyserial as a local package for their account (so it worked for that account), but in order for other users to to use the module, the module needed to be installed globally.显然,user1 已经运行 pip 来将 pyserial 安装为他们帐户的本地 package(因此它适用于该帐户),但为了让其他用户使用该模块,需要全局安装该模块。 Also, the conflict with the 'serial' module had to be resolved.此外,必须解决与“串行”模块的冲突。

So maybe a little late here, but I recently shared pyserial with some students in a class I am teaching this semester, only to find it threw the error mentioned here.所以这里可能有点晚了,但我最近在我这学期教的 class 中与一些学生分享了 pyserial,却发现它抛出了这里提到的错误。

Took a minute to figure it out and thought surely there's a question about that error on stack overflow, I should let people know this...花了一分钟才弄清楚,并认为肯定有一个关于堆栈溢出错误的问题,我应该让人们知道这一点......

Apparently, if you are on a windows machine using the Windows Subsystem for Linux (WSL) instead of a virtual machine, you will get this error.显然,如果您在 windows 机器上使用 Linux (WSL) 的 Windows 子系统而不是虚拟机,您将收到此错误。 To fix it just go into powershell and enter the command:要修复它,只需将 go 转换为 powershell 并输入命令:

wsl --shutdown

Then restarting powershell and running the pyautogui script again worked for me.然后重新启动 powershell 并再次运行 pyautogui 脚本对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决 AttributeError:模块 'serial' 在 pyserial 中没有属性 'Serial'? - How to solve AttributeError: module 'serial' has no attribute 'Serial' in pyserial? AttributeError: 模块 'serial' 没有属性 'Serial' - AttributeError: module 'serial' has no attribute 'Serial' Python attributeError:模块“ serial”没有属性“ Serial” - Python attributeError: module 'serial' has no attribute 'Serial' Pyserial:“模块‘serial’没有‘tools’属性” - Pyserial: "module 'serial' has no attribute 'tools'" Serial.serial 不起作用,thonny:模块 'serial' 没有属性 'Serial' - Serial.serial is not working, thonny: module 'serial' has no attribute 'Serial' PySerial Python 错误:AttributeError: &#39;Serial&#39; 对象没有属性 &#39;_port_handle&#39; - PySerial Python error: AttributeError: 'Serial' object has no attribute '_port_handle' AttributeError:模块 'serial' 没有属性 'reset_input_buffer' - AttributeError: module 'serial' has no attribute 'reset_input_buffer' AttributeError: 'module' 对象没有属性 'Serial' 与 pi 和 arduino 的连接 - AttributeError: 'module' object has no attribute 'Serial' with connection for pi and arduino Python - 模块“串行”没有属性“串行” - Python - module 'serial' has no attribute 'Serial' &#39;module&#39;对象没有属性&#39;Serial&#39; - 'module' object has no attribute 'Serial'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM