python如何查看包搜索路径-mile米乐体育
python如何查看包搜索路径
小编给大家分享一下python如何查看包搜索路径,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
最快查看包搜索路径的方式
当你使用 import 导入一个包或模块时,python 会去一些目录下查找,而这些目录是有优先级顺序的,正常人会使用 sys.path 查看。
>>>importsys>>>frompprintimportpprint>>>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 模式都不用进入的方法呢?
你可能会想到这种,但这本质上与上面并无区别
[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-msitesys.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
从输出你可以发现,这个列的路径会比 sys.path 更全,它包含了用户环境的目录。
以上是“python如何查看包搜索路径”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注恰卡编程网行业资讯频道!