python中的库及module的 路径
在python执行时,python调用的库或者模块文件,应该从哪里来加载呢?
包括了python系统的库目录、设置的环境变量PYTHONPATH;还可以在python代码中设置sys.path,在其中添加自己的模块路径。
查看一下python中的sys.path
>>> import sys
>>> sys.path
['', '/Users/david/Desktop/python_c', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']
>>> quit()
如果自己有了新的module和package,可以放置到一个自己的固定目录下,然后设置PYTHONPATH环境变量,这样比较好。也可以在代码中如下设置:
import sys
sys.path.insert(0, "./path/to/your/modules/")