• 技术文章 >Python技术 >Python基础教程

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

    小妮浅浅小妮浅浅2021-04-28 10:19:45原创3171

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

    1、概念

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

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

    1

    2

    3

    import sys

    sys.path  属性可以获取路径列表

    # 案例 p06.py

    1

    2

    3

    4

    5

    6

    7

    8

    # 案例 p06.py

    import sys

       

    print(type(sys.path))

    print(sys.path)

       

    for p in sys.path:

        print(p)

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    <class 'list'>

    ['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:\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

    3、添加搜索路径

    1

    sys.path.append(dir)

    以上就是python模块的搜索路径介绍,希望能对大家有所帮助。更多Python学习指路:python基础教程

    专题推荐:python模块
    上一篇:python抽象类的知识整理 下一篇:python包的导入方式有几种

    相关文章推荐

    • python模块导入顺序是什么样子的?• python模块中如何添加路径?• python模块中load方法如何使用?• python模块中Counter计数器怎么用?• 如何使用python模块将时间戳转时间日期?• python中的wxPython模块是什么?• python模块有哪些内置属性?• python模块引入的三种方式

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网