sys 模块概述
sys 是 Python 的标准库模块,提供了与 Python 解释器及其环境交互的函数和变量。它让你能够访问 Python 运行时系统的参数和功能。
sys.path 详解
sys.path 是一个字符串列表 ,指定了 Python 搜索模块的路径。当使用 import 语句时,Python 会按照这个列表中的顺序查找模块。
指定模块查找路径
import sys
import os
# 根据操作系统导入相应的模块
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '当前路径的相对路径'))
或者:
if PROJECT_ROOT not in sys.path:
sys.path.insert(0, PROJECT_ROOT) # 将路径插入到列表的开头,使其具有最高搜索优先级