這篇文章主要介紹怎么使用python3代碼查看包的路徑,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、成都微信小程序、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了彭水苗族土家族免費(fèi)建站歡迎大家使用!
當(dāng)你使用 import 導(dǎo)入一個(gè)包或模塊時(shí),Python 會(huì)去一些目錄下查找,而這些目錄是有優(yōu)先級(jí)順序的,正常人會(huì)使用 sys.path 查看。
>>> import sys >>> from pprint import pprint >>> pprint(sys.path) ['', '/usr/local/Python3.7/lib/python37.zip', '/usr/local/Python3.7/lib/python3.7', '/usr/local/Python3.7/lib/python3.7/lib-dynload', '/home/wangbm/.local/lib/python3.7/site-packages', '/usr/local/Python3.7/lib/python3.7/site-packages'] >>>
那有沒有更快的方式呢?
我這有一種連 console 模式都不用進(jìn)入的方法呢?
你可能會(huì)想到這種,但這本質(zhì)上與上面并無區(qū)別
[wangbm@localhost ~]$ python -c "print('\n'.join(__import__('sys').path))" /usr/lib/python2.7/site-packages/pip-18.1-py2.7.egg /usr/lib/python2.7/site-packages/redis-3.0.1-py2.7.egg /usr/lib64/python27.zip /usr/lib64/python2.7 /usr/lib64/python2.7/plat-linux2 /usr/lib64/python2.7/lib-tk /usr/lib64/python2.7/lib-old /usr/lib64/python2.7/lib-dynload /home/wangbm/.local/lib/python2.7/site-packages /usr/lib64/python2.7/site-packages /usr/lib64/python2.7/site-packages/gtk-2.0 /usr/lib/python2.7/site-packages
這里我要介紹的是比上面兩種都方便的多的方法,一行命令即可解決
[wangbm@localhost ~]$ python3 -m site sys.path = [ '/home/wangbm', '/usr/local/Python3.7/lib/python37.zip', '/usr/local/Python3.7/lib/python3.7', '/usr/local/Python3.7/lib/python3.7/lib-dynload', '/home/wangbm/.local/lib/python3.7/site-packages', '/usr/local/Python3.7/lib/python3.7/site-packages', ] USER_BASE: '/home/wangbm/.local' (exists) USER_SITE: '/home/wangbm/.local/lib/python3.7/site-packages' (exists) ENABLE_USER_SITE: True
從輸出你可以發(fā)現(xiàn),這個(gè)列的路徑會(huì)比sys.path更全,它包含了用戶環(huán)境的目錄。
以上是“怎么使用python3代碼查看包的路徑”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!