How to convert a .py to .exe if i want to include many subfolders and imported modules in the final executable?

huangapple go评论96阅读模式
英文:

How to convert a .py to .exe if i want to include many subfolders and imported modules in the final executable?

问题

我想将我的 main.py 转换成一个可执行文件,以便轻松分发,而不需要客户在其系统上安装 Python。我希望将项目及其子文件夹中导入的所有模块(基于项目结构)嵌入一个可执行文件中。

我尝试运行以下命令:

  1. pyinstaller --add-data "website/static;static" --add-data "website/templates;templates" --hidden-import flask,flask_sqlalchemy,flask_login,werkzeug.security,sqlalchemy.sql,sqlalchemy,flask_login.mixins,flask_login.utils,flask_login.config,flask_login.signals,flask_login.view,flask_login.login_manager --hidden-import website.models,website.db,json,base64 --onefile website/main.py

但在运行生成的 .exe 文件时,我遇到了本地主机页面显示:

  1. 内部服务器错误
  2. 服务器遇到内部错误,无法完成您的请求。要么服务器负载过重,要么应用程序中存在错误。

并且控制台显示以下错误:

  1. 已创建数据库!
  2. ERROR:website:Exception on /login [GET]
  3. Traceback (most recent call last):
  4. File "flask\app.py", line 2190, in wsgi_app
  5. File "flask\app.py", line 1486, in full_dispatch_request
  6. File "flask\app.py", line 1484, in full_dispatch_request
  7. File "flask\app.py", line 1469, in dispatch_request
  8. File "auth.py", line 26, in login
  9. File "flask\templating.py", line 150, in render_template
  10. File "jinja2\environment.py", line 1081, in get_or_select_template
  11. File "jinja2\environment.py", line 1010, in get_template
  12. File "jinja2\environment.py", line 969, in _load_template
  13. File "jinja2\loaders.py", line 126, in load
  14. File "flask\templating.py", line 64, in get_source
  15. File "flask\templating.py", line 98, in _get_source_fast
  16. jinja2.exceptions.TemplateNotFound: login.html

我不太清楚如何继续生成一个可执行文件,其中包含所有项目结构并包括引用的导入。

英文:

I have developed a project which has the following structure :

  1. web_app
  2. └── website
  3. | ├── __init__.py
  4. | ├── auth.py
  5. | ├── models.py
  6. | ├── views.py
  7. | ├── static
  8. | ├── images
  9. | └── logo_kg.png
  10. | ├── app.js
  11. | ├── index.js
  12. | └── styles.css
  13. | └── templates
  14. | ├── base.html
  15. | ├── home.html
  16. | ├── login.html
  17. | ├── sign_up.html
  18. | └── viewProjects.html
  19. └── main.py

I want to convert my main.py to an executable in order to distribute it easily without the need of the client to have python installed on his system. I want to embed all the imported modules of my project and its subfolders (based on the project structure) into one executable file.

All the imported modules i have are :

  1. from flask import Flask
  2. from flask_sqlalchemy import SQLAlchemy
  3. from os import path
  4. from flask_login import LoginManager
  5. from flask import Blueprint, render_template, request, flash, redirect, url_for
  6. from .models import User
  7. from werkzeug.security import generate_password_hash, check_password_hash
  8. from . import db
  9. from flask_login import login_user, login_required, logout_user, current_user
  10. from website import create_app
  11. from . import db
  12. from flask_login import UserMixin
  13. from sqlalchemy.sql import func
  14. from flask import Blueprint, render_template, request, flash, jsonify
  15. from flask_login import login_required, current_user
  16. from .models import Project
  17. from . import db
  18. import json
  19. import base64
  20. from sqlalchemy import func

Some of the commands i tried

I tried running commands like :

  1. pyinstaller --add-data "website/static;static" --add-data "website/templates;templates" --hidden-import flask,flask_sqlalchemy,flask_login,werkzeug.security,sqlalchemy.sql,sqlalchemy,flask_login.mixins,flask_login.utils,flask_login.config,flask_login.signals,flask_login.view,flask_login.login_manager --hidden-import website.models,website.db,json,base64 --onefile website/main.py

but upon running the generated .exe i was met with the localhost page displaying :

  1. Internal Server Error
  2. The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

and the console showing the following errors :

  1. Created Database!
  2. ERROR:website:Exception on /login [GET]
  3. Traceback (most recent call last):
  4. File "flask\app.py", line 2190, in wsgi_app
  5. File "flask\app.py", line 1486, in full_dispatch_request
  6. File "flask\app.py", line 1484, in full_dispatch_request
  7. File "flask\app.py", line 1469, in dispatch_request
  8. File "auth.py", line 26, in login
  9. File "flask\templating.py", line 150, in render_template
  10. File "jinja2\environment.py", line 1081, in get_or_select_template
  11. File "jinja2\environment.py", line 1010, in get_template
  12. File "jinja2\environment.py", line 969, in _load_template
  13. File "jinja2\loaders.py", line 126, in load
  14. File "flask\templating.py", line 64, in get_source
  15. File "flask\templating.py", line 98, in _get_source_fast
  16. jinja2.exceptions.TemplateNotFound: login.html

I don't really know how to proceed with generating one executable that will have embedded all the project structure and include the referenced imports.

答案1

得分: 1

尝试使用一个 spec 文件来正确管理 PyInstaller。

  1. # main.spec
  2. block_cipher = None
  3. a = Analysis(['main.py'],
  4. pathex=['<path_to_your_project>'],
  5. binaries=[],
  6. datas=[('<path_to_your_project>/website/static', 'static'),
  7. ('<path_to_your_project>/website/templates', 'templates')],
  8. hiddenimports=['flask', 'flask_sqlalchemy', 'flask_login', 'werkzeug.security', 'sqlalchemy.sql', 'sqlalchemy', 'flask_login.mixins', 'flask_login.utils', 'flask_login.config', 'flask_login.signals', 'flask_login.view', 'flask_login.login_manager', 'website.models', 'website.db', 'json', 'base64'],
  9. hookspath=[],
  10. runtime_hooks=[],
  11. excludes=[],
  12. win_no_prefer_redirects=False,
  13. win_private_assemblies=False,
  14. cipher=block_cipher,
  15. noarchive=False)
  16. pyz = PYZ(a.pure, a.zipped_data,
  17. cipher=block_cipher)
  18. exe = EXE(pyz,
  19. a.scripts,
  20. a.binaries,
  21. a.zipfiles,
  22. a.datas,
  23. [],
  24. name='main',
  25. debug=False,
  26. bootloader_ignore_signals=False,
  27. strip=False,
  28. upx=True,
  29. upx_exclude=[],
  30. runtime_tmpdir=None,
  31. console=True)

将其保存为 main.spec 并运行
pyinstaller main.spec

英文:

Try using a spec file to manage pyinstaller properly.

  1. # main.spec
  2. block_cipher = None
  3. a = Analysis([&#39;main.py&#39;],
  4. pathex=[&#39;&lt;path_to_your_project&gt;&#39;],
  5. binaries=[],
  6. datas=[(&#39;&lt;path_to_your_project&gt;/website/static&#39;, &#39;static&#39;),
  7. (&#39;&lt;path_to_your_project&gt;/website/templates&#39;, &#39;templates&#39;)],
  8. hiddenimports=[&#39;flask&#39;, &#39;flask_sqlalchemy&#39;, &#39;flask_login&#39;, &#39;werkzeug.security&#39;, &#39;sqlalchemy.sql&#39;, &#39;sqlalchemy&#39;, &#39;flask_login.mixins&#39;, &#39;flask_login.utils&#39;, &#39;flask_login.config&#39;, &#39;flask_login.signals&#39;, &#39;flask_login.view&#39;, &#39;flask_login.login_manager&#39;, &#39;website.models&#39;, &#39;website.db&#39;, &#39;json&#39;, &#39;base64&#39;],
  9. hookspath=[],
  10. runtime_hooks=[],
  11. excludes=[],
  12. win_no_prefer_redirects=False,
  13. win_private_assemblies=False,
  14. cipher=block_cipher,
  15. noarchive=False)
  16. pyz = PYZ(a.pure, a.zipped_data,
  17. cipher=block_cipher)
  18. exe = EXE(pyz,
  19. a.scripts,
  20. a.binaries,
  21. a.zipfiles,
  22. a.datas,
  23. [],
  24. name=&#39;main&#39;,
  25. debug=False,
  26. bootloader_ignore_signals=False,
  27. strip=False,
  28. upx=True,
  29. upx_exclude=[],
  30. runtime_tmpdir=None,
  31. console=True)

Save that as main.spec and run
pyinstaller main.spec

huangapple
  • 本文由 发表于 2023年6月13日 02:17:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定