簡體   English   中英

Pyinstaller:生成-exe文件+文件夾(在--onefile模式下)

[英]Pyinstaller: generate -exe file + folder (in --onefile mode)

現在我正在使用 Pyinstaller。 我有一個從 img 文件夾中獲取圖像的腳本..

/python
|----/img
|----|----icon1.ico
|----|----icon2.ico
|----maint.py

我生成 .exe 的腳本是

pyinstaller.py --windowed --noconsole --clean --onefile maint.py

問題是只生成 .exe 文件,但省略了整個文件夾 /img。

問題:為了自動獲取 .exe 文件 + /img 文件夾,我需要在上一行中添加哪些附加語法?

2013 年 12 月 18 日更新

我的意思是:在執行 pyinstaller.py 腳本后,使用所有參數,我必須在 /dist 文件夾中看到:.exe 文件 + 包含我的應用程序的所有圖標或位圖文件的 /img 文件夾

謝謝

2013 年 12 月 19 日更新

終於,我們得到了!

0. 我正在使用當前版本的 PYInstaller + Python 2.67,將 Sublime Text 作為編輯器。

1. 如果你的 Py 腳本需要一些文件、圖標、圖像,你必須包含一個從項目文件夾(在開發中)或形成臨時數據文件夾(在部署的情況下)中檢索這些文件的函數。 該腳本必須在您的代碼中,恰好位於您放置相對路徑以獲取資源的部分。 請完全遵循此准則: https : //stackoverflow.com/a/13790741

2.在前面的代碼之后,您必須第一次執行pyinstaller命令 - 正如我在我的問題帖子中發布的那樣-。

3. 現在,打開執行 PYInstaller(位於 PYinstaller/)命令后生成的 .spec 文件,並在“a.binaries”行之后添加下一行到 EXE() 函數中:

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          Tree('..\\python\\images', prefix='images\\'),
....

請記住,在Tree(...)函數中,第一個參數是要放在外面的文件夾:這意味着我想包含該文件夾的所有內容(請注意,我正在放置一個相對於 AppStart 的相對路徑。 py 文件)到我的 .EXE 文件的文件容器中。

4.修改后重新執行pyinstaller命令,但在這種情況下指向我的.SPEC文件:

pyinstaller.py --windowed --noconsole --clean --onefile AppStart\AppStart.spec

最后,我的應用程序可以作為可執行文件執行,而無需像有人提到的那樣復制和粘貼所有外部文件夾。 但與往常一樣,我認為這是一種實用的方法。

感謝您的支持。

您必須解決許多問題才能使其正常工作。 例如:

  • 獲取正確的資源路徑
  • 添加數據

第一個問題是(如前所述)通過根據執行模式調整路徑來解決。

def app_path(path):
    frozen = 'not'
    if getattr(sys, 'frozen', False):
            # we are running in executable mode
            frozen = 'ever so'
            app_dir = sys._MEIPASS
    else:
            # we are running in a normal Python environment
            app_dir = os.path.dirname(os.path.abspath(__file__))
    return os.path.join(app_dir, path)

對於第二個問題而不是樹,我使用通配符 (*) 添加我需要的內容。

added_files = [
         ( './pics/*', 'pics' ),
         ( './db/*', 'db' ),
         ]

然后在分析中,

datas = added_files

一個徹底的答案很長。 我寫這篇文章是為了詳細說明我為解決這個問題所經歷的一切。

您還可以在另一個 python 腳本中運行 pyinstaller,然后使用 shutil.copytree() 移動文件夾。 或者對單個文件使用shutil.copyfile()。

import PyInstaller.__main__
import shutil

PyInstaller.__main__.run([
    'YourProgram.py',
    '--icon=UI/Your_Icon.ico',
    '--onefile',
    '--noconsole',
], )

shutil.copytree('path/', 'dist/path')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM