'Python script with MQTT not working when compiled with pyinstaller
So I have a python script that I made. Basically it connects to a mqtt broker using paho-mqtt and display data in a graph using mathplotlib. It works perfectly fine on my computer, but I need to share the script with some people, and since I'm not sure how tech savy they are, to save them the trouble of installing python, pip, and all the dependecies needed, I looked into turning my script to an exe file. I found pyinstaller, and tried it. But the exe it gives me doesn't work. It won't connect to the MQTT broker, and I'm getting this error :
File "app.py", line 123, in <module>
File "paho\mqtt\client.py", line 914, in connect
File "paho\mqtt\client.py", line 1044, in reconnect
File "paho\mqtt\client.py", line 3685, in _create_socket_connection
File "socket.py", line 845, in create_connection
File "socket.py", line 833, in create_connection
TimeoutError: timed out
[16016] Failed to execute script 'app' due to unhandled exception!
The line it refers to is the client.connect(...)
Also, the execution is really slow, it takes something like 30 secondes for the first prompt to appear in the cli, where as before compilation it's almost instant.
The command I used to build : pyinstaller --onefile 'app.py'
Here is a simplified version of the code that still generate the issue :
import paho.mqtt.client as mqtt
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
import pandas as pd
import time
import json
from matplotlib.animation import FuncAnimation
from datetime import datetime
isReading = True
timestamps = []
dataX = []
finish = False
newData = False
lasttime = None
lastX = None
#for testing :
sensorid = "sensorid"
#MQTT functions--------------------
def on_connect(client, userdata, flags, rc):
client.subscribe("data/" + sensorid)
def on_message(client, userdata, msg):
if isReading == True:
global newData, lasttime, lastX, lastY, lastZ
newData = True
data_string = msg.payload.decode('utf8')
data_json = json.loads(data_string)
lasttime = data_json.get('time')
lastX = data_json.get('X')
#/MQTT functions--------------------
#PLT funtions-------------------
def on_plt_close(event):
global finish
finish = True
client.loop_stop()
def animate(i):
global newData, lasttime, lastX, lastY, lastZ
if newData == True:
timestamps.append(datetime.fromtimestamp(int(lasttime)))
dataX.append(int(lastX))
newData = False
ax1.cla()
ax1.plot_date(timestamps, dataX, 'r', label="X", xdate=True, ydate=False)
ax1.legend()
#/PLT functions---------------------
fig=plt.figure()
ax1=fig.add_subplot()
fig.canvas.mpl_connect('close_event', on_plt_close)
ani = FuncAnimation(plt.gcf(), animate, 500)
#setup and connect MQTT client
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("ip", 1883, 60)
client.loop_start()
plt.show()
while finish == False:
time.sleep(1)
Does anyone know what I do wrong ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
