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, '引用模块的地址')  

猜你喜欢

转载自www.cnblogs.com/lmh001/p/9957313.html