Python属性错误:“模块”对象没有属性“串行”

时间:2022-01-07 07:56:48

I'm trying to access a serial port with Python 2.6 on my Raspberry Pi running Debian. My script named serial.py tries to import pySerial:

我正在尝试访问一个串行端口,使用Python 2.6在我的树莓Pi运行Debian。我的脚本命名的系列。py尝试导入pySerial:

import serial
ser = serial.Serial('/dev/ttyAMA0', 9600)
ser.write("hello world!")

For some reason it refuses to establish the serial connection with this error:

由于某些原因,它拒绝建立与此错误的串行连接:

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

When I try to type the same code in the interactive Python interpreter it still doesn't work.

当我尝试在交互式Python解释器中键入相同的代码时,它仍然不起作用。

Strangely, it used to work about a couple hours ago.

奇怪的是,它在几个小时前就开始工作了。

What could be the problem? I've tried to fix this for a while, installing pySerial again, rewriting my code, double-checking the serial port, etc.

有什么问题吗?我试着修复这个问题有一段时间了,重新安装pySerial,重写我的代码,反复检查串口,等等。

Thanks in advance!

提前谢谢!

7 个解决方案

#1


65  

You're importing the module, not the class. So, you must write: from serial import serial

您导入的是模块,而不是类。因此,您必须编写:从串行导入串行。

#2


134  

I'm adding this solution for people who make the same mistake as I did.

我为那些和我犯同样错误的人加入了这个解决方案。

In most cases: rename your project file 'serial.py' and delete serial.pyc if exists, then you can do simple 'import serial' without attribute error.

在大多数情况下:重命名您的项目文件的系列。py和删除系列。如果存在pyc,那么您可以在没有属性错误的情况下执行简单的“导入串行”。

Problem occurs when you import 'something' when your python file name is 'something.py'.

当您在python文件名称为“something.py”时导入“something”时出现问题。

#3


54  

I accidentally installed 'serial' (sudo python -m pip install serial) instead of 'pySerial' (sudo python -m pip install pyserial), which lead to the same error.

我不小心安装了“串行”(sudo python -m pip安装串行),而不是“py串行”(sudo python -m pip安装pyseries),这导致了同样的错误。

If the previously mentioned solutions did not work for you, double check if you installed the correct library.

如果前面提到的解决方案对您不起作用,请再次检查是否安装了正确的库。

#4


2  

If you are helpless like me, try this:

如果你像我一样无助,试试这个:

List all Sub-Modules of "Serial" (or whatever package you are having trouble with) with the method described here: List all the modules that are part of a python package

列出“串行”(或您遇到的任何问题)的所有子模块,并使用这里描述的方法:列出所有属于python包的模块。

In my case, the problems solved one after the other.

在我看来,问题一个接一个地解决了。

...looks like a bug to me...

…对我来说就像一只虫子……

#5


1  

This error can also happen if you have circular dependencies. Check your imports and make sure you do not have any cycles.

如果有循环依赖项,也会发生此错误。检查您的导入,确保您没有任何周期。

#6


1  

This problem is beacouse your proyect is named serial.py and the library imported is name serial too , change the name and thats all.

这个问题是因为你的产品被命名为系列产品。py和导入的库都是name系列,更改名称和thats all。

#7


0  

You have installed the incorrect package named 'serial', try running pip uninstall serial or pip3 uninstall serial for python 3.x and install pyserial if not already installed by running pip install pyserial orpip3 install pyserial for python 3.x.

您已经安装了名为“串行”的不正确的包,尝试运行pip卸载串行或pip3 uninstall系列的python 3。如果没有安装pyseries,则安装pyserial orpip3安装pyseries,用于python 3.x。

#1


65  

You're importing the module, not the class. So, you must write: from serial import serial

您导入的是模块,而不是类。因此,您必须编写:从串行导入串行。

#2


134  

I'm adding this solution for people who make the same mistake as I did.

我为那些和我犯同样错误的人加入了这个解决方案。

In most cases: rename your project file 'serial.py' and delete serial.pyc if exists, then you can do simple 'import serial' without attribute error.

在大多数情况下:重命名您的项目文件的系列。py和删除系列。如果存在pyc,那么您可以在没有属性错误的情况下执行简单的“导入串行”。

Problem occurs when you import 'something' when your python file name is 'something.py'.

当您在python文件名称为“something.py”时导入“something”时出现问题。

#3


54  

I accidentally installed 'serial' (sudo python -m pip install serial) instead of 'pySerial' (sudo python -m pip install pyserial), which lead to the same error.

我不小心安装了“串行”(sudo python -m pip安装串行),而不是“py串行”(sudo python -m pip安装pyseries),这导致了同样的错误。

If the previously mentioned solutions did not work for you, double check if you installed the correct library.

如果前面提到的解决方案对您不起作用,请再次检查是否安装了正确的库。

#4


2  

If you are helpless like me, try this:

如果你像我一样无助,试试这个:

List all Sub-Modules of "Serial" (or whatever package you are having trouble with) with the method described here: List all the modules that are part of a python package

列出“串行”(或您遇到的任何问题)的所有子模块,并使用这里描述的方法:列出所有属于python包的模块。

In my case, the problems solved one after the other.

在我看来,问题一个接一个地解决了。

...looks like a bug to me...

…对我来说就像一只虫子……

#5


1  

This error can also happen if you have circular dependencies. Check your imports and make sure you do not have any cycles.

如果有循环依赖项,也会发生此错误。检查您的导入,确保您没有任何周期。

#6


1  

This problem is beacouse your proyect is named serial.py and the library imported is name serial too , change the name and thats all.

这个问题是因为你的产品被命名为系列产品。py和导入的库都是name系列,更改名称和thats all。

#7


0  

You have installed the incorrect package named 'serial', try running pip uninstall serial or pip3 uninstall serial for python 3.x and install pyserial if not already installed by running pip install pyserial orpip3 install pyserial for python 3.x.

您已经安装了名为“串行”的不正确的包,尝试运行pip卸载串行或pip3 uninstall系列的python 3。如果没有安装pyseries,则安装pyserial orpip3安装pyseries,用于python 3.x。