有時候,我們想要查找Python安裝路徑及第三方庫的存放路徑,但可能忘記了當初安裝時的具體路徑,這個時候我們就可以通過命令快速找到這些路徑。
查找Python安裝路徑
- Windows下查找命令:
where python3
D:\>where python3
D:\Python\installation\python3.exe
C:\Users\wintest\AppData\Local\Microsoft\WindowsApps\python3.exe
- Linux / Mac 下查找命令:
which python3
[root@wintest ~]# which python3
/usr/bin/python3
在 Linux & Mac 下,我們查到的很可能是軟鏈接形式,它並不是安裝的實際路徑,但我們可以通過 ls -l
命令找到其指向的文件路徑。
[root@wintest ~]# ls -l /usr/bin/python3
lrwxrwxrwx 1 root root 28 Sep 7 2019 /usr/bin/python3 -> /root/python36/bin/python3.6
查找Python第三方庫路徑
如果需要查找Python第三方庫 site-packages
的存放路徑,我們可以直接進入Python交互模式,借助 sys.path
來快速找到。
[root@wintest ~]# python3
Python 3.6.8 (default, Sep 7 2019, 18:50:26)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/root/python36/lib/python36.zip', '/root/python36/lib/python3.6', '/root/python36/lib/python3.6/lib-dynload', '/root/python36/lib/python3.6/site-packages']
不管是 Windows / Linux / Mac ,我們都可以通過以上方式來找到Python第三方庫路徑。