Search code examples
pythonpy2exepyserialcanopy

Executable out of script containing serial_for_url


I have developed a python script for making a serial communication to a digital pump. I now need to make an executable out of it. However even though it works perfectly well when running it with python and py2exe does produce the .exe properly when I run the executable the following error occurs:

File: pump_model.pyc in line 96 in connect_new
File: serial\__init__.pyc in line 71 in serial_for_url
ValueError: invalid URL protocol 'loop' not known

The relevant piece of my code is the following: # New serial connection def connect_new(self, port_name): """Function for configuring a new serial connection."""

    try:
        self.ser = serial.Serial(port = port_name,\
                baudrate = 9600,\
                parity = 'N',\
                stopbits = 1,\
                bytesize = 8,\
                timeout = self.timeout_time)
    except serial.SerialException:
        self.ser = serial.serial_for_url('loop://',\
              timeout = self.timeout_time) # This line BLOWS!
    except:
        print sys.exc_info()[0]

    finally:
        self.initialize_pump()

I should note that the application was written in OSX and was tested on Windows with the Canopy Python Distribution.


Solution

  • Found it!

    It seems that for some reason the 'loop://' arguement can't be recognised after the .exe production.

    I figured out by studying the pyserial/init.py script that when issuing the command serial.serial_for_url(‘loop://') you essentially call: sys.modules['serial.urlhandler.protocol_loop’].Serial(“loop://“)

    So you have to first import the serial.urlhandler.protocol_loop and then issue that command in place of the one malfunctioning. So you can now type:

    __import__('serial.urlhandler.protocol_loop')
    sys.modules[‘serial.urlhandler.protocol_loop’].Serial("loop://")
    

    After this minor workaround it worked fine.