pyinstaller打包含有folium模块,运行错误的解决方法

pyinstaller打包含有folium模块,运行错误的解决方法

在用pyinstaller打包folium模块的程序后,运行出现一堆错误代码。解决办法参照

基本思路是修改python库site-packages内对应文件里的环境变量

  1. \folium\folium.py
  2. \folium\raster_layers.py
  3. \branca\element.py

image-20241008171526756image-20241008171720862

#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'))

image-20241008171750461

接着在终端运行(要进行主程序所在的目录):

pyinstaller -F -w map_app.py

可以不用等待该命令运行结束,即手动按ctrl+c 主动打断打包命令。

因为,此时会自动生成一个map_app.spec的文件,我们要对这个map_app.spec文件进行修改。

# -*- mode: python -*-
 
block_cipher = None
 
 
a = Analysis(['map_app.py'],
         pathex=['C:\\Users\\XXXX\\PycharmProjects\\XXX'],
         binaries=[],
         datas=[
         ("C:\\Users\\venv\\Lib\\site-packages\\branca\\*.json","branca"),
         ("C:\\Users\\venv\\Lib\\site-packages\\branca\\templates","templates"),
         ("C:\\Users\\venv\\Lib\\site-packages\\folium\\templates","templates"),
         ],
         hiddenimports=[],
         hookspath=[],
         runtime_hooks=[],
         excludes=[],
         win_no_prefer_redirects=False,
         win_private_assemblies=False,
         cipher=block_cipher,
         noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
         cipher=block_cipher)
exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.datas,
    [],
    name='map_app',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=False,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
    icon=['D:\\google_image\\微信图片_2024.jpg'],
)

最后再运行以下命令

pyinstaller map_app.spec

大功告成!