'模块'对象没有属性' to_bytes'蟒蛇

时间:2015-08-29 07:52:18

标签: python serial-port cx-freeze

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

    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   
---------------------------

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

这是一个易于阅读的屏幕截图:

enter image description here

感谢。

3 个答案:

答案 0 :(得分:1)

hints/darwin.sh

答案 1 :(得分:1)

我实际上最终这样做了:

  1. 在C:\ Python27 \ Lib \ site-packages \ serial \ tools中修改文件list_ports_windows.py,并将serial更改为serialutil,如下所示:
  2. 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添加到导入
    2. 不确定这是正确还是错误的方式,但它确实有效。

      感谢。

答案 2 :(得分:-1)

Ports = serial.to_bytes([80,111,114,116,115])#&#34; Ports&#34;没有属性&#39; to_bytes&#39; 两步:

1,pip install pyserial == 3.4

2,pip install pyserial == 2.7

我不知道原因,但它有效