There is a chance this is still a problem and the Pyinstaller and/or Folium people have no interest in fixing it, but I'll post it again here in case someone out there has discovered a workaround.
I have a program that creates maps, geocodes etc and recently added the folium package to create some interactive maps in html format. I always compile my code using pyinstaller so that others at my company can just use the executable rather than running the python code. If I run my code in an IDE, it loads, runs and performs exactly as expected. However, when I attempt to compile while I have import folium
somewhere in my script, I get an error when trying to run the executable that pyinstaller creates.
The error text reads something like this:
Traceback (most recent call last):
File "analysisSuite.py", line 58, in <module>
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "c:\users\natha\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\folium\__init__.py", line 8, in <module>
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "c:\users\natha\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\branca\__init__.py", line 5, in <module>
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "c:\users\natha\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\branca\colormap.py", line 29, in <module>
File "site-packages\pkg_resources\__init__.py", line 1143, in resource_stream
File "site-packages\pkg_resources\__init__.py", line 1390, in get_resource_stream
File "site-packages\pkg_resources\__init__.py", line 1393, in get_resource_string
File "site-packages\pkg_resources\__init__.py", line 1469, in _get
File "c:\users\natha\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 479, in get_data
with open(path, 'rb') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\natha\\AppData\\Local\\Temp\\_MEI309082\\branca\\_cnames.json'
[30956] Failed to execute script analysisSuite
I am still relatively new to Python, so trying to decipher what the issue is by this text is pretty overwhelming. I have no idea if there is a workaround, where I just need to edit a file, add a file or add some parameter to pyinstaller, but perhaps someone else out there can read this and has an idea of what could be causing this problem. Thanks in advance to anyone that has suggestions.
EDIT: The problem seems to be with branca, which is a dependency of folium. It looks for that _cnames.json file which is in the site-packages\branca folder but either doesn't get copied as it should or perhaps I need to somehow identify in my script where it should look for those files and then just manually copy them into a folder that I choose.
ADDITIONAL UPDATE: I've been testing and testing and have determined the heart of the problem. When you run your exe, it gets unpacked in a temp folder. One of the modules within branca
is colormap.py
In the colormap
file, there are essentially three lines that keep branca
from loading correctly.
resource_package = __name__
resource_path_schemes = '/_schemes.json'
resource_path_cnames = '/_cnames.json'
So, when the executable gets unpacked in this temp folder and branca tries to load up, because of these above lines, it expects these two files to also be in this temp folder, but of course, they won't be because they're being told to always and only be in the folder where the colormap module lives. The key here is figuring out a way so that the path reference can be relative, so that it doesn't look in the temp folder but also that the reference is dynamic, so that wherever you have your executable, as long as you have those json files present in some folder that it "knows" about, then you'll be good. Now I just need to figure out how to do that.