本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
1、概念
加载模块的时候,系统会在哪些地方寻找此模块。
2、系统默认的模块搜索路径
import sys sys.path 属性可以获取路径列表 # 案例 p06.py
# 案例 p06.py import sys print(type(sys.path)) print(sys.path) for p in sys.path: print(p)
['D:\python\project\包管理', 'D:\PyCharm Community Edition 2019.1.1\helpers\pydev', 'D:\python\project', 'D:\PyCharm Community Edition 2019.1.1\helpers\third_party\thriftpy', 'D:\PyCharm Community Edition 2019.1.1\helpers\pydev', 'C:\Users\user\.PyCharmCE2019.1\system\cythonExtensions', 'D:\python\project\包管理', 'D:\Anaconda3\envs\opp\python37.zip', 'D:\Anaconda3\envs\opp\DLLs', 'D:\Anaconda3\envs\opp\lib', 'D:\Anaconda3\envs\opp', 'D:\Anaconda3\envs\opp\lib\site-packages'] D:pythonproject包管理 D:PyCharm Community Edition 2019.1.1helperspydev D:pythonproject D:PyCharm Community Edition 2019.1.1helpersthird_partythriftpy D:PyCharm Community Edition 2019.1.1helperspydev C:Usersuser.PyCharmCE2019.1systemcythonExtensions D:pythonproject包管理 D:Anaconda3envsopppython37.zip D:Anaconda3envsoppDLLs D:Anaconda3envsopplib D:Anaconda3envsopp D:Anaconda3envsopplibsite-packages
3、添加搜索路径
sys.path.append(dir)
以上就是python模块的搜索路径介绍,希望能对大家有所帮助。更多Python学习指路:python基础教程


