'Pyinstaller Import Issues with Jinja2
There are a couple post in regards to Pyinstaller having issuing recognizing jinja2, unfortunately none have resolved my issue. Ideally if Pyinstaller supports a package which it states has support for jinja2 its hook should retrieve the package and its files.
I receive the following error when I run the built program:
ImportError: Missing optional dependency 'Jinja2' . DataFrame.style requires jinja2. Use pip or conda to install Jinja2.
From build log:
30562 INFO: Processing module hooks...
30563 INFO: Loading module hook 'hook-jinja2.py' from 'd:\mainapplicationfolder\venv\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
I have tried the following under Windows 10, Python 3.8.5 with pyinstaller - 4.1, jinja2 - 2.11.2:
1. --path "venv\Lib\site-packages" (Path to my python packages, Pyinstaller builds several packages but ignores jinja2)
2. --hidden-import="jinja2" (no effect)
3. --path "D:\mainapplicationfolder\venv\lib\site-packages\jinja2\lexer.py" (To test to see if it will build one single file, no luck)
4. manually created a spec file and added gmas80s's script to add the exact path of each file (no effect)
My program directory tree is as follows:
mainapplicationfolder
-makespec.py
-main.py
-functions (folder)
-images (folder)
-venv (folder)
--Lib
---site-packages
----jinja2
-32_32_gQU_icon.ico
Example of my spec folder:
# -*- mode: python -*-
# <<< START ADDED PART
from PyInstaller.building.build_main import Analysis, PYZ, EXE, COLLECT, BUNDLE, TOC
def collect_pkg_data(package, include_py_files=False, subdir=None):
import os
from PyInstaller.utils.hooks import get_package_paths, remove_prefix, PY_IGNORE_EXTENSIONS
# Accept only strings as packages.
if type(package) is not str:
raise ValueError
pkg_base, pkg_dir = get_package_paths(package)
if subdir:
pkg_dir = os.path.join(pkg_dir, subdir)
# Walk through all file in the given package, looking for data files.
data_toc = TOC()
for dir_path, dir_names, files in os.walk(pkg_dir):
for f in files:
extension = os.path.splitext(f)[1]
if include_py_files or (extension not in PY_IGNORE_EXTENSIONS):
source_file = os.path.join(dir_path, f)
dest_folder = remove_prefix(dir_path, os.path.dirname(pkg_base) + os.sep)
dest_file = os.path.join(dest_folder, f)
data_toc.append((dest_file, source_file, 'DATA'))
return data_toc
pkg_data = collect_pkg_data('jinja2') # <<< Put the name of your package here
# <<< END ADDED PART
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['makespec.py'],
pathex=['venv\\Lib\\site-packages', 'venv\\Lib\\site-packages\\jinga2', 'D:\\mainapplicationfolder'],
binaries=[],
datas=[('functions\\*', 'functions'), ('images\\*', 'images')],
hiddenimports=['jinja2'],
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,
[],
exclude_binaries=True,
name='myprogram',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True , icon='32_32_gQU_icon.ico')
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
pkg_data, # <<< Add here the collected files
strip=False,
upx=True,
upx_exclude=[],
name='makespec')
The program builds and seems to pull in all the other packages except Jinja2 even after all the files are specified. It seems many others have successfully navigated this issue. I am not sure where I have gone wrong.
Solution 1:[1]
I ran into the same issue and found it wasn't the jinja2 package but its dependency on markupsafe. The markupsafe package was updated and it broke 'soft_unicode'. You can see the github thread here: https://github.com/pallets/markupsafe/issues/284
Try this version in your requirements.txt or pip.
pip install markupsafe==2.0.1
Best of luck.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | script_kitty |