如何獲取 Python 模組路徑?


對於純 Python 模組,你可以透過檢視 module.__file__ 找到原始檔的位置。例如,

 >>> import mymodule
>>> mymodule.__file__
C:/Users/Ayush/mymodule.py 

 不過,許多內建模組是用 C 編寫的,因此 module.__file__ 指向了 .so 檔案(Windows 上沒有 module.__file__),因此你看不到原始碼。

 從命令列執行“python -v”,會告訴你正在匯入的內容及其來源。如果你想知道內建模組的位置,這是非常有用的。

更新於:2019 年 9 月 30 日

1K+ 次瀏覽

啟動你的 職業

完成課程以獲得認證

開始使用
廣告