python基础:搜索路径

如何将写好的脚本或者是模块加入python的搜索路径?

>>>import sys
>>> sys.path
['', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages']
#出来的是一个路径列表,python会从这里搜索路径,其中模块应该存放在/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages

>>>sys.path.append("/desktop/python3.4/test.py")  
#把test.py的路径加入python搜索路径里去
>>>sys.path
['', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages','/desktop/python3.4/test.py']
>>>import test
"Hellow world"

  

原文地址:https://www.cnblogs.com/alan-babyblog/p/5148696.html