Python 如何在pyinstaller中添加静态(html、css、js等)文件以创建独立的exe文件?
我正在使用Python 如何在pyinstaller中添加静态(html、css、js等)文件以创建独立的exe文件?,python,pyqt,pyqt5,pyinstaller,qwebengineview,Python,Pyqt,Pyqt5,Pyinstaller,Qwebengineview,我正在使用QtWebEngineWidgets,QtWebChannel创建PyQt5应用程序,它使用HTML、CSS和JavaScript 当我们以常规方式运行时,它工作得很好,例如,python main.py 导入HTML,如下所示 current_dir = os.path.dirname(os.path.realpath(__file__)) filename = os.path.join(current_dir, "index.html") url = QtCore.QUrl.fro
QtWebEngineWidgets
,QtWebChannel
创建PyQt5应用程序,它使用HTML、CSS和JavaScript
当我们以常规方式运行时,它工作得很好,例如,python main.py
导入HTML,如下所示
current_dir = os.path.dirname(os.path.realpath(__file__))
filename = os.path.join(current_dir, "index.html")
url = QtCore.QUrl.fromLocalFile(filename)
# 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>
导入CSS、JavaScript文件,如下所示
current_dir = os.path.dirname(os.path.realpath(__file__))
filename = os.path.join(current_dir, "index.html")
url = QtCore.QUrl.fromLocalFile(filename)
# 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命令:
pyinstaller --onefile --windowed main.py
我需要在生成的
.exe
文件中手动添加静态文件以按预期工作。我想将其包含在.exe
文件本身中。如何获取此信息?根据您的问题,您可以假定您的项目结构如下:
├── index.html
├── jquery.js
├── main.py
├── my_custom.js
└── styles.css
对于您的情况,有两种选择:
导入操作系统
导入系统
从PyQt5导入QtCore、QtWidgets、QtWebEngineWidgets
def资源路径(相对路径):
“”“获取资源的绝对路径,适用于开发人员和PyInstaller”“”
尝试:
#PyInstaller创建临时文件夹并将路径存储在_MEIPASS中
基本路径=系统路径
除例外情况外:
基本路径=os.path.abspath(“.”)
返回os.path.join(基本路径、相对路径)
如果名称=“\uuuuu main\uuuuuuuu”:
导入系统
app=qtwidts.QApplication(sys.argv)
view=QtWebEngineWidgets.QWebEngineView()
文件名=资源路径(“index.html”)
url=QtCore.QUrl.fromLocalFile(文件名)
view.load(url)
view.show()
sys.exit(app.exec_())
如果要向可执行文件添加外部资源,则必须使用“-add data”选项:
对于windows更改“:“带”;”<RCC>
<qresource prefix="/">
<file>index.html</file>
<file>jquery.js</file>
<file>my_custom.js</file>
<file>styles.css</file>
</qresource>
</RCC>
2.3导入资源_rc.py文件,并在main.py文件中使用带有模式“qrc”的url:
导入操作系统
导入系统
从PyQt5导入QtCore、QtWidgets、QtWebEngineWidgets
导入资源
如果名称=“\uuuuu main\uuuuuuuu”:
导入系统
app=qtwidts.QApplication(sys.argv)
view=QtWebEngineWidgets.QWebEngineView()
url=QtCore.QUrl(“qrc:/index.html”)
view.load(url)
view.show()
sys.exit(app.exec_())
2.4使用初始命令编译项目
pyinstaller --onefile --windowed main.py
根据您的问题,您可以假定您的项目结构如下:
├── index.html
├── jquery.js
├── main.py
├── my_custom.js
└── styles.css
对于您的情况,有两种选择:
导入操作系统
导入系统
从PyQt5导入QtCore、QtWidgets、QtWebEngineWidgets
def资源路径(相对路径):
“”“获取资源的绝对路径,适用于开发人员和PyInstaller”“”
尝试:
#PyInstaller创建临时文件夹并将路径存储在_MEIPASS中
基本路径=系统路径
除例外情况外:
基本路径=os.path.abspath(“.”)
返回os.path.join(基本路径、相对路径)
如果名称=“\uuuuu main\uuuuuuuu”:
导入系统
app=qtwidts.QApplication(sys.argv)
view=QtWebEngineWidgets.QWebEngineView()
文件名=资源路径(“index.html”)
url=QtCore.QUrl.fromLocalFile(文件名)
view.load(url)
view.show()
sys.exit(app.exec_())
如果要向可执行文件添加外部资源,则必须使用“-add data”选项:
对于windows更改“:“带”;”<RCC>
<qresource prefix="/">
<file>index.html</file>
<file>jquery.js</file>
<file>my_custom.js</file>
<file>styles.css</file>
</qresource>
</RCC>
2.3导入资源_rc.py文件,并在main.py文件中使用带有模式“qrc”的url:
导入操作系统
导入系统
从PyQt5导入QtCore、QtWidgets、QtWebEngineWidgets
导入资源
如果名称=“\uuuuu main\uuuuuuuu”:
导入系统
app=qtwidts.QApplication(sys.argv)
view=QtWebEngineWidgets.QWebEngineView()
url=QtCore.QUrl(“qrc:/index.html”)
view.load(url)
view.show()
sys.exit(app.exec_())
2.4使用初始命令编译项目
pyinstaller --onefile --windowed main.py
谢谢你的帮助。使用第二种方法有一个小疑问:在HTML中,是否需要将文件导入为
?或者还有其他方法吗?使用第一种方法,我得到了错误:错误:参数--add data:invalid add\u data\u或二进制值:'index.html:。@Pythoncoder try execute:pyinstaller--onefile--windowed--add data=“index.html;”--add data=“jquery.js;”add data=“my\u custom.js;”add data=“my\u custom.js;”add data=“styles.css;”main.py
是,它正在使用代码>。谢谢原因是什么?@Pythoncoder-see谢谢你的帮助。使用第二种方法有一个小疑问:在HTML中,是否需要将文件导入为
?或者还有其他方法吗?使用第一种方法,我得到了错误:错误:参数--add data:invalid add\u data\u或二进制值:'index.html:。
@Pythoncoder try execute:pyinstaller--onefile--windowed--add data=“index.html;”--add data=“jquery.js;”add data=“my\u custom.js;”add data=“my\u custom.js;”add data=“styles.css;”main.py
是,它正在使用代码>。谢谢原因是什么?@Pythoncoder-see