import os import inspect currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # 获取当前文件所在的目录 rootdir = os.path.dirname(currentdir) # 去掉文件名,返回目录 os.sys.path.insert(0, rootdir) # 动态修改系统路径 把自己项目所在的地址放在sys.path 列表里面 # sys.path.insert(0,’模块的名称’) 添加相关的路径,但在退出python环境后自己添加的路径就会自动消失!
python自定义模块路径
最新推荐文章于 2024-01-06 12:18:45 发布
该博客探讨了如何使用Python内置的os和inspect模块来获取当前脚本的目录,并将该项目目录添加到sys.path中。通过这样做,可以确保在运行脚本时能够正确导入项目内的模块。此外,还提到了sys.path.insert()函数的临时性,即在退出Python环境后,添加的路径会自动消失。
摘要由CSDN通过智能技术生成