2016-03-05 18 views
8

Tôi đang sử dụng pyinstaller để xây dựng ứng dụng bình của mình, mọi thứ đều hoạt động tốt ngoại trừ tôi gặp vấn đề với mẫu Jinja2.Pyinstaller Jinja2 TemplateNotFound

Nó đã cho tôi jinja2.exceptions.TemplateNotFound,

Tôi cố gắng để đưa from app import template đó là thư mục mẫu, nhưng nó đã không làm việc (tôi đoán vì chúng không chứa bất kỳ tập tin py).

Tôi cũng đã cố gắng thay đổi các tập tin .spec để bao gồm các thư mục templates

added_files = [ 
     ('..\\CommerceApp\\app\\templates', 'templates'), 
     ('..\\CommerceApp\\app\\static', 'static') 
     ] 

a = Analysis(['..\\CommerceApp\\run.py'], 
      pathex=['D:\\PythonProjects\\CommerceAppExe'], 
      binaries=None, 
      datas=added_files, 
      hiddenimports=[], 
      hookspath=[], 
      runtime_hooks=[], 
      excludes=[], 
      win_no_prefer_redirects=False, 
      win_private_assemblies=False, 
      cipher=block_cipher) 

Nhưng nó đã không làm việc một trong hai, kết quả tương tự như thể tôi sao chép các thư mục bằng tay bởi bản thân mình.

Có cách nào để bao gồm Mẫu được đóng gói cùng với .exe không?


Sửa

Đây là spec tập tin của tôi

# -*- mode: python -*- 

block_cipher = None 

a = Analysis(['..\\CommerceApp_withPyInstaller\\run.py'], 
      pathex=['D:\\PythonProjects\\CommerceAppExe'], 
      binaries=None, 
      datas=[], 
      hiddenimports=[], 
      hookspath=[], 
      runtime_hooks=[], 
      excludes=[], 
      win_no_prefer_redirects=False, 
      win_private_assemblies=False, 
      cipher=block_cipher) 
pyz = PYZ(a.pure, a.zipped_data, 
      cipher=block_cipher) 
exe = EXE(pyz, 
      a.scripts, 
      exclude_binaries=True, 
      name='SupplyTracker', 
      debug=False, 
      strip=False, 
      upx=True, 
      console=True) 
coll = COLLECT(exe, 
       a.binaries, 
       a.zipfiles, 
       a.datas, 
       strip=False, 
       upx=True, 
       name='SupplyTracker') 

Chỉnh sửa 2

Được chấp nhận trả lời thay đổi để gmas80 được gây ra sự cố.

Sửa 3

Ngoài ra tôi chỉ nhận ra, tôi chỉ có thể làm cho một thư mục mới với tên gói của tôi và điền thông tin vào mẫu tĩnh css, html, vv, và nó là gonna làm việc (kết quả tương tự với những gì gmas80 script does)

+0

Tạo 1 thư mục ứng dụng đã đóng băng và kiểm tra xem tất cả các tệp mẫu đã được thu thập chưa! – gmas80

Trả lời

5

Tôi không tin rằng sự cố là gì được mô tả trong https://stackoverflow.com/a/35816876/2741329. Tôi vừa có thể đóng băng một ứng dụng với Jinja2.

Trong file spec của tôi, tôi sử dụng phương pháp này để thu thập tất cả các mẫu:

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('<YOUR LIB HERE>') 

Sau đó thêm pkg_data đến COLLECT (1 thư mục) hoặc đến EXE (1-file) .spec.

Trong giải pháp 1 thư mục, bạn sẽ có thể tìm thấy tất cả các mẫu của mình trong thư mục con đã tạo.


Sửa

này có thể làm việc (giả sử rằng bạn có một gói (ví dụ, bạn có một __init__.py) làm theo các gợi ý: http://flask.pocoo.org/docs/0.10/patterns/packages/):

# -*- 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('<yourapplication>') # <<< Put the name of your package here 
# <<< END ADDED PART  

block_cipher = None 

a = Analysis(['..\\CommerceApp_withPyInstaller\\run.py'], 
      pathex=['D:\\PythonProjects\\CommerceAppExe'], 
      binaries=None, 
      datas=[], 
      hiddenimports=[], 
      hookspath=[], 
      runtime_hooks=[], 
      excludes=[], 
      win_no_prefer_redirects=False, 
      win_private_assemblies=False, 
      cipher=block_cipher) 
pyz = PYZ(a.pure, a.zipped_data, 
      cipher=block_cipher) 
exe = EXE(pyz, 
      a.scripts, 
      exclude_binaries=True, 
      name='SupplyTracker', 
      debug=False, 
      strip=False, 
      upx=True, 
      console=True) 
coll = COLLECT(exe, 
       a.binaries, 
       a.zipfiles, 
       a.datas, 
       pkg_data, # <<< Add here the collected files 
       strip=False, 
       upx=True, 
       name='SupplyTracker') 
+0

Xin lỗi nếu tôi không biết, làm thế nào để bạn đặt thuộc tính 'pkg_data' vào' COLLECT' trong 'spec 'tập tin? Tôi đã thử đặt 'pathname' của tập lệnh (../path/to/pkg_data.py) nhưng nó không hoạt động. Tôi có nên 'nhập' tập lệnh trong tập lệnh chính của mình không? hoặc đặt nó ở đâu đó trong thuộc tính 'Phân tích'? Cảm ơn – andiwin

+0

@ kingathur61: Tại sao bạn không đặt toàn bộ tập tin spec của bạn trong cơ thể câu hỏi? Vì vậy, tôi có thể chỉnh sửa nó như tôi tin rằng có thể làm việc. – gmas80

+0

Tôi đã thêm tệp spec vào chỉnh sửa – andiwin

2

Gói Jinja2 sử dụng pkg_resources API không được hỗ trợ bởi PyInstaller. Mô-đun pkg_resources được cung cấp qua gói setuptools.

Từ FAQ trang của pyinstaller:

pkg_resources hiện chưa được hỗ trợ bởi PyInstaller. Điều này có nghĩa là rằng một ứng dụng sử dụng thư viện sử dụng API pkg_resources có thể sẽ không hoạt động. Tình huống duy nhất trong đó nó hoạt động là khi nó được sử dụng trên các tệp .egg (xem ở trên). Đối với chi tiết theo dõi số # 183.

+0

Ah, tôi hiểu rồi, vậy cho đến khi pyinstaller được cập nhật, tôi không thể làm gì được? – andiwin

+0

@Forge và @ kingathur61: '' jinja2.exceptions.TemplateNotFound'' không liên quan đến '' pkg_resources''. Nếu không, nó sẽ có một cái gì đó khác nhau: https://github.com/pyinstaller/pyinstaller/issues/1898 – gmas80