1

Please advise how I can cx-freeze my python program which uses serial :

    import serial    
    import serial.tools.list_ports;

    print serial.tools.list_ports()

Here's my 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")])

After I build using cx_freeze, this is my error :

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

I'm not sure why I see this error. Any advise appreciated.

Here's a screenshot for easy reading :

enter image description here

Thanks.

1
  • added import serial. I still get the same error Commented Aug 29, 2015 at 8:28

3 Answers 3

1
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)])
Sign up to request clarification or add additional context in comments.

3 Comments

I still get the same error. This makes no difference.
Added the path variable to my setup.py as you suggested.. Still does not work
Thank you for trying to help me. Please let me know if you have any thoughts on my solution
1

I actually ended up doing this:

  1. Modifying the file list_ports_windows.py inside C:\Python27\Lib\site-packages\serial\tools and changed serial to serialutil like this :

    Ports = serialutil.to_bytes([80, 111, 114, 116, 115]) # "Ports"
    PortName = serialutil.to_bytes([80, 111, 114, 116, 78, 97, 109, 101]) # "PortName"
    
  2. Then added from serial import serialutil to the imports

Not sure if this is the right way, but it worked.

3 Comments

PortName = serial.to_bytes([80, 111, 114, 116, 78, 97, 109, 101]) # "PortName" Yes my library include this. Update your libraries on python ! What is verion ? my is 2.6
Serial version ? inside ` init.py` ?
On IDLE : import serial; serial.VERSION
-1

Ports = serial.to_bytes([80, 111, 114, 116, 115]) # "Ports" no attribute 'to_bytes' two step:

1、pip install pyserial==3.4

2、pip install pyserial==2.7

i don't know the reason, but it worked

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.