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

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

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

问题

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

我尝试运行以下命令:

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 文件时,我遇到了本地主机页面显示:

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

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

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

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

英文:

I have developed a project which has the following structure :

web_app
└── website
|   ├── __init__.py
|   ├── auth.py
|   ├── models.py
|   ├── views.py
|   ├── static
|   │   ├── images
|   │   │   └── logo_kg.png
|   │   ├── app.js
|   │   ├── index.js
|   │   └── styles.css
|   └── templates
|       ├── base.html
|       ├── home.html
|       ├── login.html
|       ├── sign_up.html
|       └── viewProjects.html
└── 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 :

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from os import path
from flask_login import LoginManager
from flask import Blueprint, render_template, request, flash, redirect, url_for
from .models import User
from werkzeug.security import generate_password_hash, check_password_hash
from . import db
from flask_login import login_user, login_required, logout_user, current_user
from website import create_app
from . import db
from flask_login import UserMixin
from sqlalchemy.sql import func
from flask import Blueprint, render_template, request, flash, jsonify
from flask_login import login_required, current_user
from .models import Project
from . import db
import json
import base64
from sqlalchemy import func

Some of the commands i tried

I tried running commands like :

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 :

Internal Server Error
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 :

Created Database!
ERROR:website:Exception on /login [GET]
Traceback (most recent call last):
  File "flask\app.py", line 2190, in wsgi_app
  File "flask\app.py", line 1486, in full_dispatch_request
  File "flask\app.py", line 1484, in full_dispatch_request
  File "flask\app.py", line 1469, in dispatch_request
  File "auth.py", line 26, in login
  File "flask\templating.py", line 150, in render_template
  File "jinja2\environment.py", line 1081, in get_or_select_template
  File "jinja2\environment.py", line 1010, in get_template
  File "jinja2\environment.py", line 969, in _load_template
  File "jinja2\loaders.py", line 126, in load
  File "flask\templating.py", line 64, in get_source
  File "flask\templating.py", line 98, in _get_source_fast
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。

# main.spec

block_cipher = None

a = Analysis(['main.py'],
             pathex=['<path_to_your_project>'],
             binaries=[],
             datas=[('<path_to_your_project>/website/static', 'static'),
                    ('<path_to_your_project>/website/templates', 'templates')],
             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'],
             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.zipfiles,
          a.datas,
          [],
          name='main',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=True)

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

英文:

Try using a spec file to manage pyinstaller properly.

# main.spec

block_cipher = None

a = Analysis([&#39;main.py&#39;],
             pathex=[&#39;&lt;path_to_your_project&gt;&#39;],
             binaries=[],
             datas=[(&#39;&lt;path_to_your_project&gt;/website/static&#39;, &#39;static&#39;),
                    (&#39;&lt;path_to_your_project&gt;/website/templates&#39;, &#39;templates&#39;)],
             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;],
             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.zipfiles,
          a.datas,
          [],
          name=&#39;main&#39;,
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          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:

确定