python中os.path 与sys.path

Posted lvmenghui0

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中os.path 与sys.path相关的知识,希望对你有一定的参考价值。

常用的命令

1 import sys
2 import os.path
3 
4 this_dir = os.path.dirname(__file__)
5 sys.path.insert(0, this_dir + /..) 或 sys.path.append(this_dir)

通过上述代码即首先获取当前目录,使用sys.path将要导入的package或module加入到PATH环境变量中。

1.获取当前目录

1   __file__    #是用来获得模块所在的路径的
2   os.path.dirname()  ##返回目录路径

2.sys.path —— 动态地改变Python搜索路径

如果python中导入的package或module不在环境变量PATH中,那么可以使用sys.path将要导入的package或module加入到PATH环境变量中。

1  sys.path.append(’引用模块的地址)   # 
2  sys.path.insert(0, 引用模块的地址)  

 

以上是关于python中os.path 与sys.path的主要内容,如果未能解决你的问题,请参考以下文章