Python 的 sys.path 是從哪裡初始化的?
Python 的 sys.path,包含 Python 搜尋要導入的模組的目錄,經過複雜的初始化過程來確定其初始值。以下是其設定方式的詳細說明:
sys.executable 確定
- Python 在檔案系統上找到其物理位置並將其用作sys 的值.executable.
sys.exec_prefix 和sys.prefix 初始化
- Python 讀取與sys.executable 同一目錄下的pyvenv.cfg檔案或上一個目錄。
-
如果檔案存在,它將提取以下資訊:
- 在Windows 上:如果「applocal = true,」sys.prefix 設定為包含sys.executable 的目錄。否則,它使用“home =
”指令。 - 在 Linux/Mac 上:Sys.exec_prefix 設定為「home =
」指示。除非 PYTHONHOME 環境變數存在,否則 Sys.prefix 也設定為該值。
- 在Windows 上:如果「applocal = true,」sys.prefix 設定為包含sys.executable 的目錄。否則,它使用“home =
- 如果pyvenv.cfg 不存在或不包含必要的信息,Python 會嘗試確定sys.exec_prefix和sys.prefix 透過從sys.executable 向後走,尋找特定的標誌性文件。
sys.path 初始化
- Python 將正在執行的腳本的目錄加入 sys.path。
- 在 Linux/Mac 上,它會新增 PYTHONPATH 環境變數(如果已設定)。
- 它包含zip 檔案路徑(/lib/python35.zip 或 path/to/python.zip)。
- 在 Windows 上,如果 pyvenv.cfg 中沒有“applocal = true”,它會添加來自“HK_CURRENT_USERSoftwarePythonPythonCore<”的子項;DLLVersion>PythonPath。 ”
- 在Windows 上且pyvenv.cfg 中沒有“applocal = true”,並且如果未找到sys.prefix,則會添加“HK_CURRENT_USERSoftwarePythonPythonCore
PythonPath”的“核心內容”。 在Windows 上且pyvenv.cfg 中沒有“applocal = true”,它會添加來自“HK_LOCAL_MACHINESoftwarePythonPythonCore - 在 Windows 上且在 pyvenv.cfg 中沒有「applocal = true」 .cfg,如果找不到 sys.prefix,則會新增「HK_LOCAL_MACHINESoftwarePythonPythonCore
PytPath」的「核心內容」。 - 在 Windows 上且未設定 PYTHONPATH、未找到前綴且沒有註冊表項目前,它添加了 PYTHONPATH 的相對編譯時值。
- 它在 PYTHONPATH 編譯時巨集中合併了相對於動態找到的 sys.prefix 的路徑。
- 在 Linux/Mac 上,它加入了 sys.exec_prefix。在 Windows 上,它會新增用於搜尋 sys.prefix 的目錄。
- 在 Windows 上,如果 pyvenv.cfg 中沒有“applocal = true”,它會嘗試透過在 sys.path 中定位標誌性檔案來尋找 sys.prefix 。如果不成功,會將 sys.prefix 留空。
站點模組的其他修改
- 站點模組透過新增進一步修改sys.path基於sys.prefix、sys.exec_prefix 和特定於平台的路徑的目錄。
- 它檢查這些新路徑中的配置文件,可能會添加更多路徑。
以上是Python 如何初始化 sys.path 清單?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

concatenateListSinpythonWithTheSamelements,使用:1)operatoTotakeEpduplicates,2)asettoremavelemavphicates,or3)listcompreanspherensionforcontroloverduplicates,每個methodhasdhasdifferentperferentperferentperforentperforentperforentperfornceandordorimplications。

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允許ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版
視覺化網頁開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

WebStorm Mac版
好用的JavaScript開發工具