PyInstaller: Bundle Python Applications
Learn and practice to create stand-alone applications with PyInstaller.
We'll cover the following...
Let’s revisit the app from the User Form: Select Starting Point lesson that lets the user select the starting point and receive the shortest route.
Press + to interact
Python 3.8
from ortools.constraint_solver import routing_enums_pb2from ortools.constraint_solver import pywrapcpfrom python_tsp.distances import great_circle_distance_matriximport pandas as pddf=pd.read_csv('MoscowMcD.csv')userStartingPoint = pd.read_csv('UserSelection.csv', header=None)# create a new list called 'StoreSelectionIndex' that contain the index of the rows in the 'df' data frame where the 'Store' column matches any of the values in the 'userStartingPoints' liststoreSelectionIndex = df[df["Store"].isin(userStartingPoint[0])].index.values.tolist()userStartingPoints2list=int(storeSelectionIndex[0])def create_data_model():"""Stores the data for the problem."""data = {}sources=df[['lat','lon']].to_numpy()destinations=df[['lat','lon']].to_numpy()distance_matrix = great_circle_distance_matrix(sources)data['distance_matrix']=distance_matrixdata['num_vehicles'] = 1data['depot'] = userStartingPoints2listreturn datadef print_solution(manager, routing, solution):"""Prints solution on console."""#print('Objective: {} miles'.format(solution.ObjectiveValue()))print('Objective: {} kilometers'.format(solution.ObjectiveValue() * 0.00160934))index = routing.Start(0)plan_output = 'Route for vehicle 0:\n'route_distance = 0while not routing.IsEnd(index):plan_output += ' {} ->'.format(manager.IndexToNode(index))previous_index = indexindex = solution.Value(routing.NextVar(index))route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)plan_output += ' {}\n'.format(manager.IndexToNode(index))print(plan_output)plan_output += 'Route distance: {}miles\n'.format(route_distance)def main():"""Entry point of the program."""# instantiate the data problemdata = create_data_model()# create the routing index managermanager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']),data['num_vehicles'], data['depot'])# create Routing Modelrouting = pywrapcp.RoutingModel(manager)def distance_callback(from_index, to_index):"""Returns the distance between the two nodes."""# convert from routing variable Index to distance matrix NodeIndexfrom_node = manager.IndexToNode(from_index)to_node = manager.IndexToNode(to_index)return data['distance_matrix'][from_node][to_node]transit_callback_index = routing.RegisterTransitCallback(distance_callback)# define cost of each arcrouting.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)# set first solution heuristicsearch_parameters = pywrapcp.DefaultRoutingSearchParameters()search_parameters.first_solution_strategy = (routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)# solve the problemsolution = routing.SolveWithParameters(search_parameters)if solution:print_solution(manager, routing, solution)if __name__ == '__main__':main()
Wouldn’t it be cool to share this application with colleagues who don’t have Python installed so they can also use the app without having to worry about any Python package installations?
Introduction to PyInstaller
... Ask