python的module检索

在python里面sys.path是用来标志python运行的时候有哪些模块是需要被预先加载的。如果我们简单的使用,可以直接app,也可以insert到指定的位置。

但是在我们设置了PYTHONPATH(里面的每一项会直接添加到sys.path中)后,发现它并不是第一个。

如:

tiger@in30-119 ~ $ env PYTHONPATH=/opt/tiger/toutiao/app/growth/legion/models python
Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print '\n'.join(sys.path)

/home/tiger/.local/lib/python2.7/site-packages/google_api_python_client-1.4.1-py2.7.egg
/home/tiger/.local/lib/python2.7/site-packages/uritemplate-0.6-py2.7.egg
/home/tiger/.local/lib/python2.7/site-packages/oauth2client-1.4.12-py2.7.egg
/home/tiger/.local/lib/python2.7/site-packages/rsa-3.2-py2.7.egg
/home/tiger/.local/lib/python2.7/site-packages/pyasn1_modules-0.0.7-py2.7.egg
/home/tiger/.local/lib/python2.7/site-packages/pyasn1-0.1.8-py2.7.egg
/home/tiger/.local/lib/python2.7/site-packages/simplejson-3.8.0-py2.7.egg
/data00/tiger/ss_lib/python_package/lib/python2.7/site-packages
/opt/tiger/toutiao/app/growth/legion/models
/usr/lib/python2.7
/usr/lib/python2.7/plat-linux2
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/home/tiger/.local/lib/python2.7/site-packages
/usr/local/lib/python2.7/dist-packages
/opt/tiger/pyutil
/opt/tiger/ss_thrift_gen
/opt/tiger/ss_lib
/opt/tiger
/opt/tiger/ss_lib/python_package/lib/python2.7/site-packages
/opt/tiger/toutiao/lib
/usr/lib/python2.7/dist-packages
/usr/lib/pymodules/python2.7

我们看到在自己指定的PYTHONPATH之前还有很多个路径,为什么会这样呢?

1. 首先我们需要知道一点,python运行的时候,会去默认的site-package里面找是否有pth文件,然后把里面的每一项都加入到sys.path中

2. 然后咱们需要了解的就是site模块:

python的module检索_第1张图片



你可能感兴趣的:(技术--python)