[python]sys.path.append()方法

通过一个例子来看

#!/usr/bin/python
# -*- coding: utf-8 -*-


import sys




if __name__ == '__main__':


    print sys.path
    sys.path.append('/home/work')
    print sys.path


输出结果:


[work@bjyz-liurong ~]$ python test.py 
['/home/work', '/usr/lib/python2.6/site-packages/pip-9.0.1-py2.6.egg', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info']
['/home/work', '/usr/lib/python2.6/site-packages/pip-9.0.1-py2.6.egg', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info', '/home/work']

其实sys.path是一个列表,这个列表内的路径都添加到环境变量中去了。

使用sys.path.append()方法可以添加自定义的路径。

你可能感兴趣的:(python)