搜尋
首頁後端開發Python教學Python 如何初始化 sys.path 清單?

How does Python initialize the sys.path list?

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 也設定為該值。
  • 如果pyvenv.cfg 不存在或不包含必要的信息,Python 會嘗試確定sys.exec_prefix和sys.prefix 透過從sys.executable 向後走,尋找特定的標誌性文件。

sys.path 初始化

  1. Python 將正在執行的腳本的目錄加入 sys.path。
  2. 在 Linux/Mac 上,它會新增 PYTHONPATH 環境變數(如果已設定)。
  3. 它包含zip 檔案路徑(/lib/python35.zip 或 path/to/python.zip)。
  4. 在 Windows 上,如果 pyvenv.cfg 中沒有“applocal = true”,它會添加來自“HK_CURRENT_USERSoftwarePythonPythonCore<”的子項;DLLVersion>PythonPath。 ”
  5. 在Windows 上且pyvenv.cfg 中沒有“applocal = true”,並且如果未找到sys.prefix,則會添加“HK_CURRENT_USERSoftwarePythonPythonCorePythonPath”的“核心內容”。
  6. 在Windows 上且pyvenv.cfg 中沒有“applocal = true”,它會添加來自“HK_LOCAL_MACHINESoftwarePythonPythonCorePythonPath”的子項。
  7. 在 Windows 上且在 pyvenv.cfg 中沒有「applocal = true」 .cfg,如果找不到 sys.prefix,則會新增「HK_LOCAL_MACHINESoftwarePythonPythonCorePytPath」的「核心內容」。
  8. 在 Windows 上且未設定 PYTHONPATH、未找到前綴且沒有註冊表項目前,它添加了 PYTHONPATH 的相對編譯時值。
  9. 它在 PYTHONPATH 編譯時巨集中合併了相對於動態找到的 sys.prefix 的路徑。
  10. 在 Linux/Mac 上,它加入了 sys.exec_prefix。在 Windows 上,它會新增用於搜尋 sys.prefix 的目錄。
  11. 在 Windows 上,如果 pyvenv.cfg 中沒有“applocal = true”,它會嘗試透過在 sys.path 中定位標誌性檔案來尋找 sys.prefix 。如果不成功,會將 sys.prefix 留空。

站點模組的其他修改

    站點模組透過新增進一步修改sys.path基於sys.prefix、sys.exec_prefix 和特定於平台的路徑的目錄。
  • 它檢查這些新路徑中的配置文件,可能會添加更多路徑。

以上是Python 如何初始化 sys.path 清單?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Python:深入研究彙編和解釋Python:深入研究彙編和解釋May 12, 2025 am 12:14 AM

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

Python是一種解釋或編譯語言,為什麼重要?Python是一種解釋或編譯語言,為什麼重要?May 12, 2025 am 12:09 AM

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

對於python中的循環時循環與循環:解釋了關鍵差異對於python中的循環時循環與循環:解釋了關鍵差異May 12, 2025 am 12:08 AM

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

循環時:實用指南循環時:實用指南May 12, 2025 am 12:07 AM

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

Python:它是真正的解釋嗎?揭穿神話Python:它是真正的解釋嗎?揭穿神話May 12, 2025 am 12:05 AM

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

與同一元素的Python串聯列表與同一元素的Python串聯列表May 11, 2025 am 12:08 AM

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

解釋與編譯語言:Python的位置解釋與編譯語言:Python的位置May 11, 2025 am 12:07 AM

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

循環時:您什麼時候在Python中使用?循環時:您什麼時候在Python中使用?May 11, 2025 am 12:05 AM

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

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱門文章

熱工具

SecLists

SecLists

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

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

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

SublimeText3 英文版

SublimeText3 英文版

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

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具