Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Here is my Python code, which simply generates a map into a .html file using Folium module. As expected, the code doesn't return any error.

import folium
m = folium.Map(location=[39.3999, 8.2245], tiles='cartodbpositron', zoom_start=3)       
m.save('output.html')

However when I convert the python code to .exe file, I get the following error when I want to execute the .exe file:

File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "/home/nade/.local/lib/python3.8/site-packages/PyInstaller/loader/pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "folium/__init__.py", line 5, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "/home/nade/.local/lib/python3.8/site-packages/PyInstaller/loader/pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "branca/__init__.py", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "/home/nade/.local/lib/python3.8/site-packages/PyInstaller/loader/pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "branca/colormap.py", line 20, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/_MEIS1K8Tv/branca/_cnames.json'
[17356] Failed to execute script test

I have used Pyinstaller to convert the python to .exe, here is the command I used (I used auto-py-to-exe for more specific convertion):

pyinstaller --noconfirm --onefile --windowed --icon "/path/to/icon" --key "encryption-key-value" "/path/to/script"

I have realised that this happens only because of Folium library. How do I make this module compatible when converting to .exe?

I did some research and found a workaround for this issue. The solution based on this site: https://www.tutorialfor.com/questions-153381.htm

First, you need to modify these 3 files:

  • \folium\folium.py
  • \folium\raster_layers.py
  • \branca\element.py
  • Change the line ENV = Environment(loader=PackageLoader('folium', 'templates')) to below both 3 files.

    #ENV = Environment(loader=PackageLoader('folium', 'templates'))
    import os, sys
    from jinja2 import FileSystemLoader
    if getattr(sys, 'frozen', False):
            # we are running in a bundle
        templatedir = sys._MEIPASS
    else:
        # we are running in a normal Python environment
        templatedir = os.path.dirname(os.path.abspath(__file__))
    ENV = Environment(loader=FileSystemLoader(templatedir + '\\templates'))
    

    After that, you need to edit the .spec file. Open the spec file and add datas section the below lines.

    binaries=[], datas=[ (".\\env\\Lib\\site-packages\\branca", "branca"), (".\\env\\Lib\\site-packages\\branca\\*.json","branca"), (".\\env\\Lib\\site-packages\\branca\\templates", "templates"), (".\\env\\Lib\\site-packages\\folium\\templates", "templates"), hiddenimports=[],

    Then build using spec file.

    pyinstaller <script_name>.spec
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.