python模块的搜索路径如何理解?

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

1、概念

加载模块的时候,系统会在哪些地方寻找此模块。

2、系统默认的模块搜索路径

importsys
sys.path属性可以获取路径列表
#案例p06.py
#案例p06.py
importsys

print(type(sys.path))
print(sys.path)

forpinsys.path:
print(p)
<class'list'>
['D:\\python\\project\\包管理','D:\\PyCharmCommunityEdition2019.1.1\\helpers\\pydev','D:\\python\\project','D:\\PyCharmCommunityEdition2019.1.1\\helpers\\third_party\\thriftpy','D:\\PyCharmCommunityEdition2019.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:\python\project\包管理
D:\PyCharmCommunityEdition2019.1.1\helpers\pydev
D:\python\project
D:\PyCharmCommunityEdition2019.1.1\helpers\third_party\thriftpy
D:\PyCharmCommunityEdition2019.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

3、添加搜索路径

sys.path.append(dir)

发表回复