- 把
Store_Codes.csv
在同一文件夾中.py文件。
- 在.spec文件中的字段
data
添加datas=[('Store_Codes.csv', '.')],
你應該添加到您的.py文件
if getattr(sys, 'frozen', False):
# if you are running in a |PyInstaller| bundle
extDataDir = sys._MEIPASS
extDataDir = os.path.join(extDataDir, 'Store_Codes.csv')
#you should use extDataDir as the path to your file Store_Codes.csv file
else:
# we are running in a normal Python environment
extDataDir = os.getcwd()
extDataDir = os.path.join(extDataDir, 'Store_Codes.csv')
#you should use extDataDir as the path to your file Store_Codes.csv file
編譯文件。
討論
當您啓動它創建爲操作系統相應的臨時文件夾位置的臨時文件夾中.exe
。該文件夾名爲_MEIxxxxxx
,其中xxxxxx是一個隨機數。你需要運行該腳本將在那裏 的sys._MEIPASS
是這個臨時文件夾的路徑中的所有文件。
當您添加datas=[('Store_Codes.csv', '.')]
到您的規範文件。它會將文件複製到軟件包的主文件夾。如果你想保持它組織,你可以創建一個使用datas=[('Store_Codes.csv', 'another_folder')]
不同的文件夾,然後在你的代碼,你將有
if getattr(sys, 'frozen', False):
# if you are running in a |PyInstaller| bundle
extDataDir = sys._MEIPASS
extDataDir = os.path.join(extDataDir,another_folder, 'Store_Codes.csv')
#you should use extDataDir as the path to your file Store_Codes.csv file
else:
# we are running in a normal Python environment
extDataDir = os.getcwd()
extDataDir = os.path.join(extDataDir,another_folder 'Store_Codes.csv')
#you should use extDataDir as the path to your file Store_Codes.csv file
我沒有完全按照你寫的,但我得到一個「導入錯誤:沒有名爲‘編號(store_code)’模塊」 – Clive
你把'Store_Codes。py'與主腳本放在同一個文件夾中? – Shane
我做了,它仍然給我一個錯誤,我只是測試了主腳本,它的工作原理應該如此。 – Clive