搜尋
首頁後端開發Python教學Python 的 sys.path 如何取得其初始值?

How does Python's `sys.path` get its initial values?

Python 的 sys.path 從哪裡取得初始值?

Python 的 sys.path 的初始化涉及一個複雜的過程,旨在智慧地確定導入模組最相關的路徑。以下是詳細的步驟:

1. Python 的位置確定

Python 透過解析任何符號連結並利用來自作業系統的資訊來確定其物理位置。該值被指派給 sys.executable.

2。 sys.exec_prefix 和 sys.prefix 的初始化

如果 pyvenv.cfg 檔案存在於包含 sys.executable 或上一級的目錄中,則檢查其內容。

  • Linux 和 Mac: 如果指定了 home 設定選項,它將取代 sys.executable 目錄作為 sys.prefix 的初始值。
  • Windows: PYTHONHOME 環境變數優先。如果設定,它會設定 sys.prefix 和 sys.exec_prefix。但是,如果 pyvenv.cfg 檔案包含 home 設置,它將覆蓋 PYTHONHOME 值。

否則,sys.exec_prefix 和 sys.prefix 是透過從 sys.executable 的位置向後遍歷或pyvenv.cfg 目錄。此過程中使用的特定標誌性檔案是 lib/python/dyn-load 和 lib/python/os.py.

3。 Initial Population of sys.path

  • 正在執行的腳本的目錄包含在sys.path中。
  • 加入PYTHONPATH環境變數的內容,除了在特定場景(例如,在 Windows 上使用 applocal=true)。
  • zip 檔案路徑,Linux/Mac 上的 lib/python35.zip 或 os.path.join(os.dirname(sys.executable),新增了 Windows 上的「python.zip」)。

其他特定平台的迭代步驟:

  • Windows(非-applocal): 檢查與PythonPath 相關的登錄項目並將其新增至sys.path。
  • Windows(具有未知的 sys.前綴): 透過 sys 中的目錄進行搜尋如果可能的話,用於動態確定 sys.prefix 的地標檔案的 .path。
  • Mac 和 Linux: sys.exec_prefix 的值附加到 sys.path。
  • Windows: PYTHONPATH 中編譯時定義的路徑是相對於 sys.prefix 新增的(如果動態找到)。
  • 所有平台: Python 載入網站模組,它根據全面的搜尋過程動態地向 sys.path 添加更多路徑。

這種詳細的演算法確保 Python 精心結合系統定義的路徑和使用者提供的環境變量,以提供智慧且靈活的搜尋導入模組的路徑。

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

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?May 03, 2025 am 12:11 AM

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

說明如何將內存分配給Python中的列表與數組。說明如何將內存分配給Python中的列表與數組。May 03, 2025 am 12:10 AM

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

您如何在Python數組中指定元素的數據類型?您如何在Python數組中指定元素的數據類型?May 03, 2025 am 12:06 AM

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

什麼是Numpy,為什麼對於Python中的數值計算很重要?什麼是Numpy,為什麼對於Python中的數值計算很重要?May 03, 2025 am 12:03 AM

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

討論'連續內存分配”的概念及其對數組的重要性。討論'連續內存分配”的概念及其對數組的重要性。May 03, 2025 am 12:01 AM

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

您如何切成python列表?您如何切成python列表?May 02, 2025 am 12:14 AM

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

在Numpy陣列上可以執行哪些常見操作?在Numpy陣列上可以執行哪些常見操作?May 02, 2025 am 12:09 AM

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,減法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Python的數據分析中如何使用陣列?Python的數據分析中如何使用陣列?May 02, 2025 am 12:09 AM

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

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

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

熱工具

SublimeText3 英文版

SublimeText3 英文版

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

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境