folium: Folium doesn't package as expected when compiling to an executable

Please add a code sample or a nbviewer link, copy-pastable if possible

import folium
from tkinter import *

class UserInterface:
	"""This is just a basic interface to enter coordinates to test the folium functionality"""

	def __init__(self):
		super(UserInterface,self).__init__()
		UserInterface.window = Tk()
		UserInterface.map_name_label = Label(UserInterface.window, text="Enter A Map Name")
		UserInterface.map_name_label.grid(row=0, column=0)
		UserInterface.map_name_text = StringVar()
		UserInterface.map_name_entry = Entry(UserInterface.window, textvariable=UserInterface.map_name_text)
		UserInterface.map_name_entry.grid(row=0, column=1)

		UserInterface.lat1label = Label(UserInterface.window, text="Point 1 Lat:")
		UserInterface.lat1label.grid(row=1, column=0)
		UserInterface.lat1text = StringVar()
		UserInterface.lat1_entry = Entry(UserInterface.window, textvariable=UserInterface.lat1text)
		UserInterface.lat1_entry.grid(row=1, column=1)

		UserInterface.long1label = Label(UserInterface.window, text="Point 1 Long:")
		UserInterface.long1label.grid(row=2, column=0)
		UserInterface.long1text = StringVar()
		UserInterface.lat1_entry = Entry(UserInterface.window, textvariable=UserInterface.long1text)
		UserInterface.lat1_entry.grid(row=2, column=1)

		UserInterface.lat2label = Label(UserInterface.window, text="Point 2 Lat:")
		UserInterface.lat2label.grid(row=3, column=0)
		UserInterface.lat2text = StringVar()
		UserInterface.lat2_entry = Entry(UserInterface.window, textvariable=UserInterface.lat2text)
		UserInterface.lat2_entry.grid(row=3, column=1)

		UserInterface.long2label = Label(UserInterface.window, text="Point 2 Long:")
		UserInterface.long2label.grid(row=4, column=0)
		UserInterface.long2text = StringVar()
		UserInterface.long2_entry = Entry(UserInterface.window, textvariable=UserInterface.long2text)
		UserInterface.long2_entry.grid(row=4, column=1)

		UserInterface.create_map_button = Button(UserInterface.window, text="Get Map", command=UserInterface.get_map, width=20)
		UserInterface.create_map_button.grid(row=5, columnspan=2)
		UserInterface.map_status = Label(UserInterface.window, text="")
		UserInterface.map_status.grid(row=6, column=0)
		UserInterface.window.mainloop()

	def get_map():
		UserInterface.map_status = Label(UserInterface.window, text="Getting map...")
		UserInterface.map_status.grid(row=6, column=0)
		centerpoint = [float(UserInterface.lat1text.get()), float(UserInterface.long1text.get())]
		marker = [float(UserInterface.lat2text.get()),float(UserInterface.long2text.get())]
		MapProgram.mapping.create_map(centerpoint=centerpoint, marker=marker)

class FoliumTest:
	"""This is just a basic class declaration to test the compiling process of folium when using Pyinstaller"""
	
	def __init__(self):
		super(FoliumTest,self).__init__()

	def create_map(self, centerpoint, marker):
		map_image = folium.Map(location=[centerpoint[0], centerpoint[1]], zoom_start=14, tiles='OpenStreetMap')
		centerpoint_icon_url = "http://maps.google.com/mapfiles/kml/shapes/star.png"
		centerpoint_icon = folium.features.CustomIcon(centerpoint_icon_url, icon_size=(70,70))
		folium.Marker([centerpoint[0], centerpoint[1]], icon=centerpoint_icon).add_to(map_image)
		custom_map_icon_url = "http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png"
		custom_map_icon = folium.features.CustomIcon(custom_map_icon_url, icon_size=(50,50))
		folium.Marker([marker[0], marker[1]], icon=custom_map_icon).add_to(map_image)
		map_image.save(outfile=UserInterface.map_name_text.get() + ".html")
		UserInterface.map_status = Label(UserInterface.window, text="Map created!")
		UserInterface.map_status.grid(row=6, column=0)

class MapProgram:
	"""This sets up the interface with the mapping functionality"""

	def __init__(self):
		MapProgram.mapping = FoliumTest()
		UserInterface()
		#Optional Coords to use for testing
		#centerpoint = [37.9969,-121.2919]
		#marker = [38.0132,-121.3671])

def main():
	MapProgram()
	
if __name__ == '__main__':
	main()

Problem description

When attempting to make an exe using pyinstaller, cx_freeze, py2exe etc, the folium module and its dependencies don’t seem to get compiled properly so that the program can run successfully. In the process of trying to determine where the core problem resided, I came across unexpected behavior when typing from folium import * which resulted in an error AttributeError: module 'folium' has no attribute 'GeoJsonStyle' This seemed odd to me and I thought possibly why the exe is not being compiled as it would throw the error that _cnames.json could not be found, which makes sense if GeoJsonStyle doesn’t exist in the module but perhaps it’s supposed to. If I attempt to compile the above program, but comment out folium, the compiled executable runs fine, so I know it’s related to folium specifically.

Expected Output

I expected the module to compile and load correctly so that it can be utilized in an executable.

Output of folium.__version__

0.7.0

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 32 (10 by maintainers)

Most upvoted comments

@FabeG Well I can’t thank you enough for your patience and knowledge. My executable is FINALLY running when using folium. I’ve had to comment it out for months because I could never get it to run correctly, so this is actually a pretty big deal to me. Thanks so much!

Hi @nathan3leaf, No problem for me to build an exe using folium with cx_freeze. Could you copy/paste the setup.py file you are using in order to find out what’s going wrong ?

Gotcha, I’ll head over to branca. Thanks!

Upgraded fine and from folium import * no longer throws an error.

🎉

It seems that one of the dependencies of folium is branca, is that correct or am I mistaken?

That is correct.

The error that I see when I run my exe refers to a file called _cnames.json which is in the site-packages/branca folder. Could this potentially be a branca issue or is there something about how folium ‘talks’ to branca that could be an issue?

It is probably a branca issue but we need to understand what these exe creators “want” to fix it.

Pardon my understanding of this, I’m still very new to Python and coding and just trying to learn how to properly track down problems that I encounter. I appreciate your help.

No problem. I do recommend you to move this discussion to branca though.

@ocefpaf Ok let’s forget the exe issue for now.

OK.

Is it problematic that from folium import * would throw an error?

Problematic? Yes. I could not reproduce that with folium 0.8.0 (latest) I’m guessing that is a bug that was fixed in 0.7.0.

And if it is problematic, could that have been caused by something on my end?

You probably just need an update.