How to add static(html, css, js, etc) files in pyinstaller to create standalone exe file?

前端 未结 1 1436
無奈伤痛
無奈伤痛 2021-01-25 04:20

I\'m using QtWebEngineWidgets, QtWebChannel to create PyQt5 application, which uses HTML, CSS, JavaScript.

It\'s working fine, when we run in

1条回答
  •  温柔的废话
    2021-01-25 05:14

    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:

    1. using --add-data

      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_())
      

      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 ";".

    2. using .qrc

      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. Create a file called resource.qrc with the following content in the project folder:

      
          
              index.html
              jquery.js
              my_custom.js
              styles.css
          
      
      

      2.2 Convert it to .py using pyrcc5:

      pyrcc5 resource.qrc -o resource_rc.py
      

      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 Compile the project using your initial command

      pyinstaller --onefile --windowed main.py
      

    0 讨论(0)
提交回复
热议问题