简体   繁体   English

如何解决 AttributeError:模块 'serial' 在 pyserial 中没有属性 'Serial'?

[英]How to solve AttributeError: module 'serial' has no attribute 'Serial' in pyserial?

I have a Windows 10 with Python 3.7.4 and I use Jupyter Notebook.我有一个 Windows 10 和 Python 3.7.4,我使用 Jupyter Notebook。 I'm trying to use pySerial to connect to my Arduino by Serial attribute.我正在尝试使用 pySerial 通过 Serial 属性连接到我的 Arduino。 I tried installing pySerial simply by pip install pyserial and eventually I tried conda install -c conda-forge pyserial too, but when I try to run my code:我尝试通过 pip install pyserial 简单地安装 pySerial ,最终我也尝试了 conda install -c conda-forge pyserial ,但是当我尝试运行我的代码时:

 import serial
 ser = serial.Serial('COM4', 9600)

I get an error message like:我收到一条错误消息,例如:

AttributeError                            Traceback (most recent call last)
<ipython-input-7-413d0d9dabe7> in <module>
      2 import serial
      3 import time
----> 4 ser = serial.Serial('COM4', 9600)
      5 
      6 # Your Account Sid and Auth Token from twilio.com/console

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

Also, I tried另外,我试过

import serial
serial.__file__

And I got 'C:\Users\lippe\Anaconda3\lib\site-packages\serial\__init__.py' as output.我得到了 'C:\Users\lippe\Anaconda3\lib\site-packages\serial\__init__.py' 作为 output。

I also tried dir(serial) and I can't see the Serial attribute in the output, so I think it's basically not installed and I don't know why.我也试过了dir(serial),在output中看不到Serial属性,所以我觉得基本没有安装,不知道为什么。 I tried digging the internet but I still can't find a solution.我尝试挖掘互联网,但我仍然找不到解决方案。

Make sure you're importing the right serial.确保您正在导入正确的序列号。 After installing pyserial with pip, you can use the __file__ attribute to check that it's the one I expect like so:在使用 pip 安装 pyserial 后,您可以使用__file__属性来检查它是否是我所期望的:

>>> import serial
>>> serial.__file__
'/usr/local/lib/python3.7/site-packages/serial/__init__.py'

Other answers suggest it might work to import the class directly with其他答案表明直接导入 class 可能有效

from serial import Serial

But I'm not sure why this would matter if you're importing the latest published on pypi.但是我不确定如果您要导入在 pypi 上发布的最新版本,为什么这会很重要。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM