如何在 pyinstaller 中添加静态(html、css、js 等)文件以创建独立的 exe 文件? [英] How to add static(html, css, js, etc) files in pyinstaller to create standalone exe file?
问题描述
我正在使用 QtWebEngineWidgets
、QtWebChannel
创建 PyQt5 应用程序,它使用 HTML、CSS、JavaScript.
I'm using QtWebEngineWidgets
, QtWebChannel
to create PyQt5 application, which uses HTML, CSS, JavaScript.
它工作正常,当我们以一般方式运行时,即 python main.py
It's working fine, when we run in general way i.e., python main.py
导入 HTML 如下,
Importing HTML as below,
current_dir = os.path.dirname(os.path.realpath(__file__))
filename = os.path.join(current_dir, "index.html")
url = QtCore.QUrl.fromLocalFile(filename)
导入 CSS、JavaScript 文件如下,
Importing CSS, JavaScript files as below,
# in index.html
<link rel="stylesheet" href="styles.css">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="my_custom.js"></script>
现在,我正在尝试使用 pyinstaller
创建一个独立的 .exe
文件.
Now, I'm trying to create a standalone .exe
file using pyinstaller
.
我从这里尝试过,但没有成功.
I have tried from here with no success.
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
pyinstaller 命令:
Pyinstaller command:
pyinstaller --onefile --windowed main.py
我需要在生成的 .exe
文件中手动添加静态文件才能按预期工作.我想将它包含在 .exe
文件本身中.如何获得?
I need to manually add static files at generated .exe
file to work as expected. Which I want to include it in .exe
file itself. How to get this?
推荐答案
从你的问题可以推测你的项目结构如下:
From your question you can presume that the structure of your project is as follows:
├── index.html
├── jquery.js
├── main.py
├── my_custom.js
└── styles.css
对于您的情况,有两种选择:
For your case there are 2 options:
使用
--添加数据
import os
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
view = QtWebEngineWidgets.QWebEngineView()
filename = resource_path("index.html")
url = QtCore.QUrl.fromLocalFile(filename)
view.load(url)
view.show()
sys.exit(app.exec_())
如果要向可执行文件添加外部资源,则必须使用--add-data"选项:
If you want to add external resources to the executable then you must use the "--add-data" option:
pyinstaller --onefile --windowed --add-data="index.html:." --add-data="jquery.js:." --add-data="my_custom.js:." --add-data="styles.css:." main.py
对于窗口,将:"改为;".
For windows change ":" with ";".
使用.qrc
使用此方法,您将使用 pyrcc5 将文件(.html、.css、.js 等)转换为 .py 代码,为此您必须执行以下步骤:
With this method you will convert the files (.html, .css, .js, etc) into .py code using pyrcc5 for this you must follow the following steps:
2.1.在项目文件夹中创建一个名为 resource.qrc 的文件,内容如下:
2.1. Create a file called resource.qrc with the following content in the project folder:
<RCC>
<qresource prefix="/">
<file>index.html</file>
<file>jquery.js</file>
<file>my_custom.js</file>
<file>styles.css</file>
</qresource>
</RCC>
2.2 使用 pyrcc5 将其转换为 .py:
2.2 Convert it to .py using pyrcc5:
pyrcc5 resource.qrc -o resource_rc.py
2.3 导入resource_rc.py 文件并在main.py 文件中使用带有模式qrc"的url:
2.3 Import the resource_rc.py file and use the url with schema "qrc" in the main.py file:
import os
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
import resource_rc
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
view = QtWebEngineWidgets.QWebEngineView()
url = QtCore.QUrl("qrc:/index.html")
view.load(url)
view.show()
sys.exit(app.exec_())
2.4 使用初始命令编译项目
2.4 Compile the project using your initial command
pyinstaller --onefile --windowed main.py
这篇关于如何在 pyinstaller 中添加静态(html、css、js 等)文件以创建独立的 exe 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!