檢索由 pyInstaller 創建的 Python EXE 中的應用程式路徑
在程式設計領域,了解應用程式的執行上下文至關重要。特別是在處理捆綁應用程式時,例如使用 pyInstaller 建立的應用程式。當應用程式需要存取位於其旁邊的設定檔時,就會出現一個常見的挑戰。在本文中,我們將探索一種技術來解決此問題,方法是在 pyInstaller 生成的 EXE 中準確地檢索應用程式的路徑。
當使用傳統方法建構與應用程式相鄰的檔案的路徑時,如下面的程式碼片段所示:
import os config_name = 'myapp.cfg' config_path = os.path.join(sys.path[0], config_name)
人們在 pyInstaller EXE 的上下文中遇到了困境。 sys.path 清單通常包含應用程式所在的目錄,但它是空的。為了解決這個問題,我們採用了一種更強大的方法來區分腳本和凍結的可執行檔。
import os import sys config_name = 'myapp.cfg' # Distinguish script file from frozen exe if getattr(sys, 'frozen', False): application_path = os.path.dirname(sys.executable) elif __file__: application_path = os.path.dirname(__file__) config_path = os.path.join(application_path, config_name)
在此程式碼中,我們檢查「sys」模組的「frozen」屬性。如果為 True,我們推測該應用程式正在作為凍結的可執行檔運行,並從“sys.executable”派生其路徑。相反,如果 file 存在,我們就斷定應用程式正在以腳本模式運行,並且其路徑源自 __file__。
利用此方法可以確保成功檢索應用程式的路徑,從而能夠存取相關文件,例如關鍵設定檔。
以上是如何檢索 pyInstaller 產生的 EXE 中的應用程式路徑?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Dreamweaver CS6
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3漢化版
中文版,非常好用

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。