'module' 对象没有属性 'to_bytes' Python

'module' object has no attribute 'to_bytes' Python

请告诉我如何 cx-freeze 我的 python 使用串口的程序:

    import serial    
    import serial.tools.list_ports;

    print serial.tools.list_ports()

这是我的 setup.py

import sys
from cx_Freeze import setup, Executable

    setup(
        name = "My test program",
        version = "3.1",
        description = "My test",
        executables = [Executable("pystest.py", base = "Win32GUI")])

在我使用 cx_freeze 构建之后,这是我的错误:

---------------------------
cx_Freeze: Python error in main script
---------------------------
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
    exec(code, m.__dict__)
  File "pystest.py", line 1, in <module>
  File "C:\Python27\lib\site-packages\serial\tools\list_ports.py", line 27, in <module>
    from serial.tools.list_ports_windows import *
  File "C:\Python27\lib\site-packages\serial\tools\list_ports_windows.py", line 127, in <module>
    Ports = serial.to_bytes([80, 111, 114, 116, 115]) # "Ports"
AttributeError: 'module' object has no attribute 'to_bytes'

---------------------------
OK   
---------------------------

我不确定为什么会看到此错误。任何建议表示赞赏。

为了便于阅读,这里有一个屏幕截图:

谢谢。

import sys
from cx_Freeze import setup, Executable
path = ["pystest"]+sys.path

build_exe_options = {"packages": ["os","serial"], "excludes": ["tkinter"],"path":path}
#add more package what are using for your app?

base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(  name = "My test program",
        version = "3.1",
        description = "My test",
        options = {"build_exe": build_exe_options},
        executables = [Executable("pystest.py", base=base)])

我实际上是这样做的:

  1. 在 C:\Python27\Lib\site-packages\serial\tools 中修改文件 list_ports_windows.py,并将 serial 更改为 serialutil,如下所示:
Ports = serialutil.to_bytes([80, 111, 114, 116, 115]) # "Ports"
PortName = serialutil.to_bytes([80, 111, 114, 116, 78, 97, 109, 101]) # "PortName"
  1. 然后将 from serial import serialutil 添加到导入

不确定这是正确还是错误的方法,但后来它奏效了。

谢谢。

Ports = serial.to_bytes([80, 111, 114, 116, 115]) # "Ports" 无属性 'to_bytes' 两步:

1、pip安装pyserial==3.4

2、pip安装pyserial==2.7

我不知道原因,但它起作用了