[{"data":1,"prerenderedAt":1350},["ShallowReactive",2],{"post-2025-07-08-python":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"tags":11,"body":14,"_type":1344,"_id":1345,"_source":1346,"_file":1347,"_stem":1348,"_extension":1349},"\u002Fblog\u002F2025-07-08-python","blog",false,"","Python-模块","Python模块与包管理：import机制、自定义模块、pip包管理与虚拟环境。","2025-07-08",[12,13],"Python","基础",{"type":15,"children":16,"toc":1337},"root",[17,22,29,44,49,54,69,75,93,107,112,117,144,163,183,208,247,260,274,304,327,340,408,428,433,444,463,484,507,522,530,550,572,580,596,630,646,661,669,698,707,715,721,726,742,748,753,761,766,785,798,841,862,885,904,927,947,970,1001,1006,1034,1046,1085,1090,1161,1166,1211,1216,1230,1235,1308,1313,1331],{"type":18,"tag":19,"props":20,"children":21},"element","hr",{},[],{"type":18,"tag":23,"props":24,"children":26},"h3",{"id":25},"模块概述",[27],{"type":28,"value":25},"text",{"type":18,"tag":30,"props":31,"children":32},"p",{},[33,35,42],{"type":28,"value":34},"模块（module）：一个扩展名为 ",{"type":18,"tag":36,"props":37,"children":39},"code",{"className":38},[],[40],{"type":28,"value":41},".py",{"type":28,"value":43}," 的文件，一个模块可以包含很多个函数。使用模块，不仅可以避免函数名和变量名的冲突，而且提高了代码的可维护性和重用性。",{"type":18,"tag":23,"props":45,"children":47},{"id":46},"自定义模块",[48],{"type":28,"value":46},{"type":18,"tag":30,"props":50,"children":51},{},[52],{"type":28,"value":53},"自定义模块有两个作用：",{"type":18,"tag":55,"props":56,"children":57},"ul",{},[58,64],{"type":18,"tag":59,"props":60,"children":61},"li",{},[62],{"type":28,"value":63},"规范代码，让代码更易于阅读",{"type":18,"tag":59,"props":65,"children":66},{},[67],{"type":28,"value":68},"便于其他程序调用",{"type":18,"tag":70,"props":71,"children":73},"h4",{"id":72},"创建模块",[74],{"type":28,"value":72},{"type":18,"tag":30,"props":76,"children":77},{},[78,83,85,91],{"type":18,"tag":79,"props":80,"children":81},"strong",{},[82],{"type":28,"value":72},{"type":28,"value":84},"是指将模块相关代码编写至一个以 ",{"type":18,"tag":36,"props":86,"children":88},{"className":87},[],[89],{"type":28,"value":90},"module_name.py",{"type":28,"value":92}," 形式命名的单独文件，其中创建模块名时注意模块名不是与Python自带的模块重名。",{"type":18,"tag":94,"props":95,"children":96},"blockquote",{},[97],{"type":18,"tag":30,"props":98,"children":99},{},[100,102],{"type":28,"value":101},"模块的扩展名必须为 ",{"type":18,"tag":36,"props":103,"children":105},{"className":104},[],[106],{"type":28,"value":41},{"type":18,"tag":70,"props":108,"children":110},{"id":109},"导入模块",[111],{"type":28,"value":109},{"type":18,"tag":30,"props":113,"children":114},{},[115],{"type":28,"value":116},"导入模块存在两种方式：",{"type":18,"tag":55,"props":118,"children":119},{},[120,133],{"type":18,"tag":59,"props":121,"children":122},{},[123,125,131],{"type":28,"value":124},"使用 ",{"type":18,"tag":36,"props":126,"children":128},{"className":127},[],[129],{"type":28,"value":130},"import",{"type":28,"value":132}," 语句导入模块",{"type":18,"tag":59,"props":134,"children":135},{},[136,137,143],{"type":28,"value":124},{"type":18,"tag":36,"props":138,"children":140},{"className":139},[],[141],{"type":28,"value":142},"from ... import ...",{"type":28,"value":132},{"type":18,"tag":30,"props":145,"children":146},{},[147,149,154,156,161],{"type":28,"value":148},"调用代码可以使用 ",{"type":18,"tag":36,"props":150,"children":152},{"className":151},[],[153],{"type":28,"value":130},{"type":28,"value":155}," 语句实现，使用 ",{"type":18,"tag":36,"props":157,"children":159},{"className":158},[],[160],{"type":28,"value":130},{"type":28,"value":162}," 语句还可以一次导入多个模块，模块名之间使用逗号间隔。其语法格式如下所示：",{"type":18,"tag":164,"props":165,"children":169},"pre",{"code":166,"language":167,"meta":7,"className":168,"style":7},"import module_name1 [[as other_name], module_name2 ...]\n","python","language-python shiki shiki-themes github-dark",[170],{"type":18,"tag":36,"props":171,"children":172},{"__ignoreMap":7},[173],{"type":18,"tag":174,"props":175,"children":178},"span",{"class":176,"line":177},"line",1,[179],{"type":18,"tag":174,"props":180,"children":181},{},[182],{"type":28,"value":166},{"type":18,"tag":55,"props":184,"children":185},{},[186,197],{"type":18,"tag":59,"props":187,"children":188},{},[189,195],{"type":18,"tag":36,"props":190,"children":192},{"className":191},[],[193],{"type":28,"value":194},"module_name",{"type":28,"value":196}," ：指定需要导入的模块名称",{"type":18,"tag":59,"props":198,"children":199},{},[200,206],{"type":18,"tag":36,"props":201,"children":203},{"className":202},[],[204],{"type":28,"value":205},"as other_name",{"type":28,"value":207}," ：为导入的模块起一个别名，通过别名调用函数",{"type":18,"tag":94,"props":209,"children":210},{},[211],{"type":18,"tag":30,"props":212,"children":213},{},[214,216,222,224,230,232,237,239,245],{"type":28,"value":215},"在调用模块中的变量、函数或者类时，需要在变量名、函数名或类名前添加 ",{"type":18,"tag":36,"props":217,"children":219},{"className":218},[],[220],{"type":28,"value":221},"模块名.",{"type":28,"value":223}," 作为前缀，但是当使用 ",{"type":18,"tag":36,"props":225,"children":227},{"className":226},[],[228],{"type":28,"value":229},"as 别名",{"type":28,"value":231}," 时，此时的 ",{"type":18,"tag":36,"props":233,"children":235},{"className":234},[],[236],{"type":28,"value":221},{"type":28,"value":238}," 前缀需要更改为 ",{"type":18,"tag":36,"props":240,"children":242},{"className":241},[],[243],{"type":28,"value":244},"别名.",{"type":28,"value":246}," 的形式。",{"type":18,"tag":30,"props":248,"children":249},{},[250,252,258],{"type":28,"value":251},"若不想在每次导入模块时都创建一个新的命名空间，而是仅将具体的定义导入当前的命名空间中，这时可以使用 ",{"type":18,"tag":36,"props":253,"children":255},{"className":254},[],[256],{"type":28,"value":257},"form ... import",{"type":28,"value":259}," 语句。当使用该语句导入后，不需要再添加前缀，可以直接通过具体的变量、函数和类名等访问即可。其语法结构如下：",{"type":18,"tag":164,"props":261,"children":263},{"code":262,"language":167,"meta":7,"className":168,"style":7},"from module_name as member\n",[264],{"type":18,"tag":36,"props":265,"children":266},{"__ignoreMap":7},[267],{"type":18,"tag":174,"props":268,"children":269},{"class":176,"line":177},[270],{"type":18,"tag":174,"props":271,"children":272},{},[273],{"type":28,"value":262},{"type":18,"tag":55,"props":275,"children":276},{},[277,287],{"type":18,"tag":59,"props":278,"children":279},{},[280,285],{"type":18,"tag":36,"props":281,"children":283},{"className":282},[],[284],{"type":28,"value":194},{"type":28,"value":286}," ：模块名称，区分大小写",{"type":18,"tag":59,"props":288,"children":289},{},[290,296,298],{"type":18,"tag":36,"props":291,"children":293},{"className":292},[],[294],{"type":28,"value":295},"member",{"type":28,"value":297}," ：指定需要导入的变量、函数或者类，若要导入全部定义，可以使用通配符 ",{"type":18,"tag":36,"props":299,"children":301},{"className":300},[],[302],{"type":28,"value":303},"*",{"type":18,"tag":94,"props":305,"children":306},{},[307],{"type":18,"tag":30,"props":308,"children":309},{},[310,312,317,319,325],{"type":28,"value":311},"使用通配符 ",{"type":18,"tag":36,"props":313,"children":315},{"className":314},[],[316],{"type":28,"value":303},{"type":28,"value":318}," 导入全部定义后，想查看具体导入了那些定义们可以通过显示 ",{"type":18,"tag":36,"props":320,"children":322},{"className":321},[],[323],{"type":28,"value":324},"dir()",{"type":28,"value":326},"函数的值来查看。",{"type":18,"tag":30,"props":328,"children":329},{},[330,332,338],{"type":28,"value":331},"例如，执行",{"type":18,"tag":36,"props":333,"children":335},{"className":334},[],[336],{"type":28,"value":337},"print(dir())",{"type":28,"value":339},"语句，将显示以下定义：",{"type":18,"tag":164,"props":341,"children":343},{"code":342,"language":167,"meta":7,"className":168,"style":7},"from requests import api, exceptions,auth  \n  \nprint(dir())  \n  \n'''  \n['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'api', 'auth', 'exceptions']  \n'''\n",[344],{"type":18,"tag":36,"props":345,"children":346},{"__ignoreMap":7},[347,355,364,373,381,390,399],{"type":18,"tag":174,"props":348,"children":349},{"class":176,"line":177},[350],{"type":18,"tag":174,"props":351,"children":352},{},[353],{"type":28,"value":354},"from requests import api, exceptions,auth  \n",{"type":18,"tag":174,"props":356,"children":358},{"class":176,"line":357},2,[359],{"type":18,"tag":174,"props":360,"children":361},{},[362],{"type":28,"value":363},"  \n",{"type":18,"tag":174,"props":365,"children":367},{"class":176,"line":366},3,[368],{"type":18,"tag":174,"props":369,"children":370},{},[371],{"type":28,"value":372},"print(dir())  \n",{"type":18,"tag":174,"props":374,"children":376},{"class":176,"line":375},4,[377],{"type":18,"tag":174,"props":378,"children":379},{},[380],{"type":28,"value":363},{"type":18,"tag":174,"props":382,"children":384},{"class":176,"line":383},5,[385],{"type":18,"tag":174,"props":386,"children":387},{},[388],{"type":28,"value":389},"'''  \n",{"type":18,"tag":174,"props":391,"children":393},{"class":176,"line":392},6,[394],{"type":18,"tag":174,"props":395,"children":396},{},[397],{"type":28,"value":398},"['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'api', 'auth', 'exceptions']  \n",{"type":18,"tag":174,"props":400,"children":402},{"class":176,"line":401},7,[403],{"type":18,"tag":174,"props":404,"children":405},{},[406],{"type":28,"value":407},"'''\n",{"type":18,"tag":30,"props":409,"children":410},{},[411,413,419,421,426],{"type":28,"value":412},"在使用 ",{"type":18,"tag":36,"props":414,"children":416},{"className":415},[],[417],{"type":28,"value":418},"from ... import",{"type":28,"value":420}," 语句导入模块中的定义时，需要保证所导入的内容在当前的命名空间中是唯一的，否则将出现冲突，后导入的同名变量、函数或者类会覆盖先导入的。此时需要使用 ",{"type":18,"tag":36,"props":422,"children":424},{"className":423},[],[425],{"type":28,"value":130},{"type":28,"value":427}," 语句导入。",{"type":18,"tag":70,"props":429,"children":431},{"id":430},"模块搜索目录",[432],{"type":28,"value":430},{"type":18,"tag":30,"props":434,"children":435},{},[436,437,442],{"type":28,"value":124},{"type":18,"tag":36,"props":438,"children":440},{"className":439},[],[441],{"type":28,"value":130},{"type":28,"value":443}," 语句导入模块时，注意文件所在的位置目标，默认情况下，会按照以下顺序进行查询：",{"type":18,"tag":445,"props":446,"children":447},"ol",{},[448,453,458],{"type":18,"tag":59,"props":449,"children":450},{},[451],{"type":28,"value":452},"在当前目录（即执行的Python脚本文件所在目录）中进行查找",{"type":18,"tag":59,"props":454,"children":455},{},[456],{"type":28,"value":457},"在PYTHONPATH（环境变量）下的每个目录中进行查找",{"type":18,"tag":59,"props":459,"children":460},{},[461],{"type":28,"value":462},"在Python的默认安装目录中进行查找",{"type":18,"tag":30,"props":464,"children":465},{},[466,468,474,476,482],{"type":28,"value":467},"以上各个目录的具体位置保存标准模块 ",{"type":18,"tag":36,"props":469,"children":471},{"className":470},[],[472],{"type":28,"value":473},"sys",{"type":28,"value":475}," 的 ",{"type":18,"tag":36,"props":477,"children":479},{"className":478},[],[480],{"type":28,"value":481},"sys.path",{"type":28,"value":483}," 变量中，可以通过下述代码查询代码：",{"type":18,"tag":164,"props":485,"children":487},{"code":486,"language":167,"meta":7,"className":168,"style":7},"import sys\nprint(sys.path)\n",[488],{"type":18,"tag":36,"props":489,"children":490},{"__ignoreMap":7},[491,499],{"type":18,"tag":174,"props":492,"children":493},{"class":176,"line":177},[494],{"type":18,"tag":174,"props":495,"children":496},{},[497],{"type":28,"value":498},"import sys\n",{"type":18,"tag":174,"props":500,"children":501},{"class":176,"line":357},[502],{"type":18,"tag":174,"props":503,"children":504},{},[505],{"type":28,"value":506},"print(sys.path)\n",{"type":18,"tag":30,"props":508,"children":509},{},[510],{"type":18,"tag":79,"props":511,"children":512},{},[513,515,520],{"type":28,"value":514},"三种将指定目录添加到 ",{"type":18,"tag":36,"props":516,"children":518},{"className":517},[],[519],{"type":28,"value":481},{"type":28,"value":521}," 的方式",{"type":18,"tag":30,"props":523,"children":524},{},[525],{"type":18,"tag":79,"props":526,"children":527},{},[528],{"type":28,"value":529},"1. 临时添加",{"type":18,"tag":30,"props":531,"children":532},{},[533,535,541,543,548],{"type":28,"value":534},"临时添加表示在导入模块的Python文件中进行添加。例如要将 ",{"type":18,"tag":36,"props":536,"children":538},{"className":537},[],[539],{"type":28,"value":540},"D:\u002Fprogram\u002Fpython\u002Fcode",{"type":28,"value":542}," 目录添加到 ",{"type":18,"tag":36,"props":544,"children":546},{"className":545},[],[547],{"type":28,"value":481},{"type":28,"value":549}," 中，可以使用如下代码：",{"type":18,"tag":164,"props":551,"children":553},{"code":552,"language":167,"meta":7,"className":168,"style":7},"import sys\nsys.path.addend('D:\u002Fprogram\u002Fpython\u002Fcode')\n",[554],{"type":18,"tag":36,"props":555,"children":556},{"__ignoreMap":7},[557,564],{"type":18,"tag":174,"props":558,"children":559},{"class":176,"line":177},[560],{"type":18,"tag":174,"props":561,"children":562},{},[563],{"type":28,"value":498},{"type":18,"tag":174,"props":565,"children":566},{"class":176,"line":357},[567],{"type":18,"tag":174,"props":568,"children":569},{},[570],{"type":28,"value":571},"sys.path.addend('D:\u002Fprogram\u002Fpython\u002Fcode')\n",{"type":18,"tag":94,"props":573,"children":574},{},[575],{"type":18,"tag":30,"props":576,"children":577},{},[578],{"type":28,"value":579},"通过该方法添加的目录只在执行当前文件的窗口中有效，窗口关闭后即失效。",{"type":18,"tag":30,"props":581,"children":582},{},[583],{"type":18,"tag":79,"props":584,"children":585},{},[586,588,594],{"type":28,"value":587},"2. 增加 ",{"type":18,"tag":36,"props":589,"children":591},{"className":590},[],[592],{"type":28,"value":593},".pth",{"type":28,"value":595}," 文件",{"type":18,"tag":30,"props":597,"children":598},{},[599,601,607,609,614,616,621,623,628],{"type":28,"value":600},"在Python安装目录下的 ",{"type":18,"tag":36,"props":602,"children":604},{"className":603},[],[605],{"type":28,"value":606},"Lib\\site-packages",{"type":28,"value":608}," 子目录中，创建一个扩展名为 ",{"type":18,"tag":36,"props":610,"children":612},{"className":611},[],[613],{"type":28,"value":593},{"type":28,"value":615}," 的文件，文件名任意，在该文件中添加需要导入模块所在的目录，例如要将 ",{"type":18,"tag":36,"props":617,"children":619},{"className":618},[],[620],{"type":28,"value":540},{"type":28,"value":622}," 目录导入 ",{"type":18,"tag":36,"props":624,"children":626},{"className":625},[],[627],{"type":28,"value":593},{"type":28,"value":629}," 文件，则只需在该文件中添加目录：",{"type":18,"tag":164,"props":631,"children":635},{"code":632,"language":633,"meta":7,"className":634,"style":7},"D:\u002Fprogram\u002Fpython\u002Fcode\n","path","language-path shiki shiki-themes github-dark",[636],{"type":18,"tag":36,"props":637,"children":638},{"__ignoreMap":7},[639],{"type":18,"tag":174,"props":640,"children":641},{"class":176,"line":177},[642],{"type":18,"tag":174,"props":643,"children":644},{},[645],{"type":28,"value":632},{"type":18,"tag":94,"props":647,"children":648},{},[649],{"type":18,"tag":30,"props":650,"children":651},{},[652,654,659],{"type":28,"value":653},"创建 ",{"type":18,"tag":36,"props":655,"children":657},{"className":656},[],[658],{"type":28,"value":593},{"type":28,"value":660}," 文件后，需要重新打开要执行的导入模块的Python文件，否则新添加的目录不起作用，该方法添加的目录只在当前版本的Python中有效。",{"type":18,"tag":30,"props":662,"children":663},{},[664],{"type":18,"tag":79,"props":665,"children":666},{},[667],{"type":28,"value":668},"3. 在PYTHONPATH环境变量中添加",{"type":18,"tag":30,"props":670,"children":671},{},[672,674,680,682,688,690,696],{"type":28,"value":673},"在电脑上找到 ",{"type":18,"tag":36,"props":675,"children":677},{"className":676},[],[678],{"type":28,"value":679},"环境变量",{"type":28,"value":681}," 进入，若在环境变量的列表中未发现 ",{"type":18,"tag":36,"props":683,"children":685},{"className":684},[],[686],{"type":28,"value":687},"PYTHONPATH",{"type":28,"value":689}," ，则需要自己创建一个，否则可以直接打开该变量，在编辑该变量的变量值，目录之间使用 ",{"type":18,"tag":36,"props":691,"children":693},{"className":692},[],[694],{"type":28,"value":695},";",{"type":28,"value":697}," 间隔。如下所示：",{"type":18,"tag":30,"props":699,"children":700},{},[701],{"type":18,"tag":702,"props":703,"children":706},"img",{"alt":704,"src":705},"Pasted image 20250712145702","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250712145702.png",[],{"type":18,"tag":94,"props":708,"children":709},{},[710],{"type":18,"tag":30,"props":711,"children":712},{},[713],{"type":28,"value":714},"在环境变量中添加模块目录后，需要重新打开要执行的导入模块的Python文件，否则新添加的目录不起作用，该方法可以同时适用于多个Python版本。",{"type":18,"tag":23,"props":716,"children":718},{"id":717},"python中的包",[719],{"type":28,"value":720},"Python中的包",{"type":18,"tag":30,"props":722,"children":723},{},[724],{"type":28,"value":725},"包（package）：包是一个分层次的目录结构，它将一组功能相近的模块组织在一个目录下，这样既可以起到规范代码的作用，又能避免模块名重名引起的冲突。",{"type":18,"tag":94,"props":727,"children":728},{},[729],{"type":18,"tag":30,"props":730,"children":731},{},[732,734,740],{"type":28,"value":733},"包简单理解为文件夹，只不过在该文件夹下必须存在一个名称为 ",{"type":18,"tag":36,"props":735,"children":737},{"className":736},[],[738],{"type":28,"value":739},"__init__.py",{"type":28,"value":741}," 的文件",{"type":18,"tag":70,"props":743,"children":745},{"id":744},"python程序的包结构",[746],{"type":28,"value":747},"Python程序的包结构",{"type":18,"tag":30,"props":749,"children":750},{},[751],{"type":28,"value":752},"在实际项目开发中，通常情况下会创建多个包用于存储不同类的文件。例如在开发一个网站时，可以创建如下所示包结构：",{"type":18,"tag":164,"props":754,"children":756},{"code":755},"项目名shop\n   │\n   ├── admin（用于保存后台文件的包）  \n   │     |\n   |     └──—__init__.py  forms.py  views.py\n   |\n   └── home（用于保存前台文件的包）  \n   |     │\n   |     └── __init__.py  forms.py  views.py \n   |\n   └── templates（用于保存模板文件的包）  \n   │\n   └── __init__.py  \n   │\n   └──models.py\n   |\n   |\nmanage.py(入口程序)\n",[757],{"type":18,"tag":36,"props":758,"children":759},{"__ignoreMap":7},[760],{"type":28,"value":755},{"type":18,"tag":70,"props":762,"children":764},{"id":763},"创建和使用包",[765],{"type":28,"value":763},{"type":18,"tag":30,"props":767,"children":768},{},[769,771,776,778,783],{"type":28,"value":770},"创建包只需两步，首先新建文件夹，然后在该文件夹中创建一个名称为 ",{"type":18,"tag":36,"props":772,"children":774},{"className":773},[],[775],{"type":28,"value":739},{"type":28,"value":777}," 的文件即可。在 ",{"type":18,"tag":36,"props":779,"children":781},{"className":780},[],[782],{"type":28,"value":739},{"type":28,"value":784}," 文件中可以不编写任何代码，在该文件夹的程序在导包时会自动执行。",{"type":18,"tag":94,"props":786,"children":787},{},[788],{"type":18,"tag":30,"props":789,"children":790},{},[791,796],{"type":18,"tag":36,"props":792,"children":794},{"className":793},[],[795],{"type":28,"value":739},{"type":28,"value":797}," 文件是一个模块文件，模块名为对应的包名",{"type":18,"tag":30,"props":799,"children":800},{},[801,803,808,810,816,818,824,826,832,834,839],{"type":28,"value":802},"创建包后，就可以在包中创建相应的模块了。然后使用",{"type":18,"tag":36,"props":804,"children":806},{"className":805},[],[807],{"type":28,"value":130},{"type":28,"value":809},"语句从包中加载模块。通常由三种方式可以从包中加载模块，例如需要调用",{"type":18,"tag":36,"props":811,"children":813},{"className":812},[],[814],{"type":28,"value":815},"info",{"type":28,"value":817},"包中",{"type":18,"tag":36,"props":819,"children":821},{"className":820},[],[822],{"type":28,"value":823},"demo",{"type":28,"value":825},"模块下的变量",{"type":18,"tag":36,"props":827,"children":829},{"className":828},[],[830],{"type":28,"value":831},"say",{"type":28,"value":833},"，则调用",{"type":18,"tag":36,"props":835,"children":837},{"className":836},[],[838],{"type":28,"value":831},{"type":28,"value":840},"的方式如下：",{"type":18,"tag":30,"props":842,"children":843},{},[844,846,852,854,860],{"type":28,"value":845},"通过 ",{"type":18,"tag":36,"props":847,"children":849},{"className":848},[],[850],{"type":28,"value":851},"import 完整包名.完整模块名",{"type":28,"value":853}," 的方式加载指定的模块，调用时必须添加前缀（即",{"type":18,"tag":36,"props":855,"children":857},{"className":856},[],[858],{"type":28,"value":859},"完整包名.模块名",{"type":28,"value":861}," ）",{"type":18,"tag":164,"props":863,"children":865},{"code":864,"language":167,"meta":7,"className":168,"style":7},"import info.demo\nprint(info.demo.say)\n",[866],{"type":18,"tag":36,"props":867,"children":868},{"__ignoreMap":7},[869,877],{"type":18,"tag":174,"props":870,"children":871},{"class":176,"line":177},[872],{"type":18,"tag":174,"props":873,"children":874},{},[875],{"type":28,"value":876},"import info.demo\n",{"type":18,"tag":174,"props":878,"children":879},{"class":176,"line":357},[880],{"type":18,"tag":174,"props":881,"children":882},{},[883],{"type":28,"value":884},"print(info.demo.say)\n",{"type":18,"tag":30,"props":886,"children":887},{},[888,889,895,897,902],{"type":28,"value":845},{"type":18,"tag":36,"props":890,"children":892},{"className":891},[],[893],{"type":28,"value":894},"from 完整包名 import 模块名",{"type":28,"value":896}," 的形式加载指定模块。调用时只需加模块名前缀（即 ",{"type":18,"tag":36,"props":898,"children":900},{"className":899},[],[901],{"type":28,"value":130},{"type":28,"value":903}," 后面的内容）",{"type":18,"tag":164,"props":905,"children":907},{"code":906,"language":167,"meta":7,"className":168,"style":7},"from info import demo\nprint(demo.say)\n",[908],{"type":18,"tag":36,"props":909,"children":910},{"__ignoreMap":7},[911,919],{"type":18,"tag":174,"props":912,"children":913},{"class":176,"line":177},[914],{"type":18,"tag":174,"props":915,"children":916},{},[917],{"type":28,"value":918},"from info import demo\n",{"type":18,"tag":174,"props":920,"children":921},{"class":176,"line":357},[922],{"type":18,"tag":174,"props":923,"children":924},{},[925],{"type":28,"value":926},"print(demo.say)\n",{"type":18,"tag":30,"props":928,"children":929},{},[930,931,937,939,945],{"type":28,"value":845},{"type":18,"tag":36,"props":932,"children":934},{"className":933},[],[935],{"type":28,"value":936},"from 完整包名.模块名 import 定义名",{"type":28,"value":938}," 的形式加载指定的模块。可直接调用函数，可导入多个定义名，定义名与定义名之间以 ",{"type":18,"tag":36,"props":940,"children":942},{"className":941},[],[943],{"type":28,"value":944},",",{"type":28,"value":946}," 间隔",{"type":18,"tag":164,"props":948,"children":950},{"code":949,"language":167,"meta":7,"className":168,"style":7},"from info.dmeo import sys\nprint(say)\n",[951],{"type":18,"tag":36,"props":952,"children":953},{"__ignoreMap":7},[954,962],{"type":18,"tag":174,"props":955,"children":956},{"class":176,"line":177},[957],{"type":18,"tag":174,"props":958,"children":959},{},[960],{"type":28,"value":961},"from info.dmeo import sys\n",{"type":18,"tag":174,"props":963,"children":964},{"class":176,"line":357},[965],{"type":18,"tag":174,"props":966,"children":967},{},[968],{"type":28,"value":969},"print(say)\n",{"type":18,"tag":94,"props":971,"children":972},{},[973],{"type":18,"tag":30,"props":974,"children":975},{},[976,978,984,986,991,993,999],{"type":28,"value":977},"在通过 ",{"type":18,"tag":36,"props":979,"children":981},{"className":980},[],[982],{"type":28,"value":983},"from 包名.模块名 import 定义名",{"type":28,"value":985}," 的形式加载指定模块时，可以使用",{"type":18,"tag":36,"props":987,"children":989},{"className":988},[],[990],{"type":28,"value":303},{"type":28,"value":992},"代替定义名，表示加载该模块中的不以下划线 ",{"type":18,"tag":36,"props":994,"children":996},{"className":995},[],[997],{"type":28,"value":998},"_",{"type":28,"value":1000}," 开头的定义。",{"type":18,"tag":23,"props":1002,"children":1004},{"id":1003},"以主程序的形式执行",[1005],{"type":28,"value":1003},{"type":18,"tag":30,"props":1007,"children":1008},{},[1009,1011,1017,1019,1024,1026,1032],{"type":28,"value":1010},"在每个模块的定义中都包括一个记录模块名称的变量 ",{"type":18,"tag":36,"props":1012,"children":1014},{"className":1013},[],[1015],{"type":28,"value":1016},"__name__",{"type":28,"value":1018},"，程序可以检查该变量，以确定它们在哪个模块中被执行。如果一个模块不是被导入其他程序中执行，那么它们可能在解释器的顶级模块中被执行。顶级模块的",{"type":18,"tag":36,"props":1020,"children":1022},{"className":1021},[],[1023],{"type":28,"value":1016},{"type":28,"value":1025}," 变量的值为 ",{"type":18,"tag":36,"props":1027,"children":1029},{"className":1028},[],[1030],{"type":28,"value":1031},"__main__",{"type":28,"value":1033},"，如下示例：",{"type":18,"tag":30,"props":1035,"children":1036},{},[1037,1039,1044],{"type":28,"value":1038},"定义一个需要被调用的模块",{"type":18,"tag":36,"props":1040,"children":1042},{"className":1041},[],[1043],{"type":28,"value":815},{"type":28,"value":1045},"：",{"type":18,"tag":164,"props":1047,"children":1049},{"code":1048,"language":167,"meta":7,"className":168,"style":7},"def func():\n    print(\"hello world!\")\nprint(\"接下来调试一下代码\")\nfunc()\n",[1050],{"type":18,"tag":36,"props":1051,"children":1052},{"__ignoreMap":7},[1053,1061,1069,1077],{"type":18,"tag":174,"props":1054,"children":1055},{"class":176,"line":177},[1056],{"type":18,"tag":174,"props":1057,"children":1058},{},[1059],{"type":28,"value":1060},"def func():\n",{"type":18,"tag":174,"props":1062,"children":1063},{"class":176,"line":357},[1064],{"type":18,"tag":174,"props":1065,"children":1066},{},[1067],{"type":28,"value":1068},"    print(\"hello world!\")\n",{"type":18,"tag":174,"props":1070,"children":1071},{"class":176,"line":366},[1072],{"type":18,"tag":174,"props":1073,"children":1074},{},[1075],{"type":28,"value":1076},"print(\"接下来调试一下代码\")\n",{"type":18,"tag":174,"props":1078,"children":1079},{"class":176,"line":375},[1080],{"type":18,"tag":174,"props":1081,"children":1082},{},[1083],{"type":28,"value":1084},"func()\n",{"type":18,"tag":30,"props":1086,"children":1087},{},[1088],{"type":28,"value":1089},"当我在其他程序中调用此模块时，发现与预期不符：",{"type":18,"tag":164,"props":1091,"children":1093},{"code":1092,"language":167,"meta":7,"className":168,"style":7},"import info\nprint(\"调用模块函数：\",info.func())\n\"\"\"\n执行结果为：\n接下来调试一下代码\nhello world!\n调用模块函数：hello world!\n\"\"\"\n",[1094],{"type":18,"tag":36,"props":1095,"children":1096},{"__ignoreMap":7},[1097,1105,1113,1121,1129,1137,1145,1153],{"type":18,"tag":174,"props":1098,"children":1099},{"class":176,"line":177},[1100],{"type":18,"tag":174,"props":1101,"children":1102},{},[1103],{"type":28,"value":1104},"import info\n",{"type":18,"tag":174,"props":1106,"children":1107},{"class":176,"line":357},[1108],{"type":18,"tag":174,"props":1109,"children":1110},{},[1111],{"type":28,"value":1112},"print(\"调用模块函数：\",info.func())\n",{"type":18,"tag":174,"props":1114,"children":1115},{"class":176,"line":366},[1116],{"type":18,"tag":174,"props":1117,"children":1118},{},[1119],{"type":28,"value":1120},"\"\"\"\n",{"type":18,"tag":174,"props":1122,"children":1123},{"class":176,"line":375},[1124],{"type":18,"tag":174,"props":1125,"children":1126},{},[1127],{"type":28,"value":1128},"执行结果为：\n",{"type":18,"tag":174,"props":1130,"children":1131},{"class":176,"line":383},[1132],{"type":18,"tag":174,"props":1133,"children":1134},{},[1135],{"type":28,"value":1136},"接下来调试一下代码\n",{"type":18,"tag":174,"props":1138,"children":1139},{"class":176,"line":392},[1140],{"type":18,"tag":174,"props":1141,"children":1142},{},[1143],{"type":28,"value":1144},"hello world!\n",{"type":18,"tag":174,"props":1146,"children":1147},{"class":176,"line":401},[1148],{"type":18,"tag":174,"props":1149,"children":1150},{},[1151],{"type":28,"value":1152},"调用模块函数：hello world!\n",{"type":18,"tag":174,"props":1154,"children":1156},{"class":176,"line":1155},8,[1157],{"type":18,"tag":174,"props":1158,"children":1159},{},[1160],{"type":28,"value":1120},{"type":18,"tag":30,"props":1162,"children":1163},{},[1164],{"type":28,"value":1165},"这是我们只需修改模块代码，将其设置为以主程序形式执行即可，修改如下：",{"type":18,"tag":164,"props":1167,"children":1169},{"code":1168,"language":167,"meta":7,"className":168,"style":7},"def func():\n    print(\"hello world!\")\nif __name__ == '__main__':\n    print(\"接下来调试一下代码\")\n    func()\n",[1170],{"type":18,"tag":36,"props":1171,"children":1172},{"__ignoreMap":7},[1173,1180,1187,1195,1203],{"type":18,"tag":174,"props":1174,"children":1175},{"class":176,"line":177},[1176],{"type":18,"tag":174,"props":1177,"children":1178},{},[1179],{"type":28,"value":1060},{"type":18,"tag":174,"props":1181,"children":1182},{"class":176,"line":357},[1183],{"type":18,"tag":174,"props":1184,"children":1185},{},[1186],{"type":28,"value":1068},{"type":18,"tag":174,"props":1188,"children":1189},{"class":176,"line":366},[1190],{"type":18,"tag":174,"props":1191,"children":1192},{},[1193],{"type":28,"value":1194},"if __name__ == '__main__':\n",{"type":18,"tag":174,"props":1196,"children":1197},{"class":176,"line":375},[1198],{"type":18,"tag":174,"props":1199,"children":1200},{},[1201],{"type":28,"value":1202},"    print(\"接下来调试一下代码\")\n",{"type":18,"tag":174,"props":1204,"children":1205},{"class":176,"line":383},[1206],{"type":18,"tag":174,"props":1207,"children":1208},{},[1209],{"type":28,"value":1210},"    func()\n",{"type":18,"tag":23,"props":1212,"children":1214},{"id":1213},"引用其他模块",[1215],{"type":28,"value":1213},{"type":18,"tag":30,"props":1217,"children":1218},{},[1219,1221],{"type":28,"value":1220},"Python安装路径下的Doc目录内的html文件夹是对Python的使用帮助文档。\nPython第三方模块官网   ",{"type":18,"tag":1222,"props":1223,"children":1227},"a",{"href":1224,"rel":1225},"http:\u002F\u002Fpypi.python.org\u002Fpypi",[1226],"nofollow",[1228],{"type":28,"value":1229},"点击访问",{"type":18,"tag":30,"props":1231,"children":1232},{},[1233],{"type":28,"value":1234},"安装和卸载第三方模块的命令：",{"type":18,"tag":164,"props":1236,"children":1240},{"code":1237,"language":1238,"meta":7,"className":1239,"style":7},"pip install 模块名[=版本]     # 安装\npip uninstall 模块名[=版本]   # 卸载\npip list                     # 查看已安装列表\n","shell","language-shell shiki shiki-themes github-dark",[1241],{"type":18,"tag":36,"props":1242,"children":1243},{"__ignoreMap":7},[1244,1270,1291],{"type":18,"tag":174,"props":1245,"children":1246},{"class":176,"line":177},[1247,1253,1259,1264],{"type":18,"tag":174,"props":1248,"children":1250},{"style":1249},"--shiki-default:#B392F0",[1251],{"type":28,"value":1252},"pip",{"type":18,"tag":174,"props":1254,"children":1256},{"style":1255},"--shiki-default:#9ECBFF",[1257],{"type":28,"value":1258}," install",{"type":18,"tag":174,"props":1260,"children":1261},{"style":1255},[1262],{"type":28,"value":1263}," 模块名[=版本]",{"type":18,"tag":174,"props":1265,"children":1267},{"style":1266},"--shiki-default:#6A737D",[1268],{"type":28,"value":1269},"     # 安装\n",{"type":18,"tag":174,"props":1271,"children":1272},{"class":176,"line":357},[1273,1277,1282,1286],{"type":18,"tag":174,"props":1274,"children":1275},{"style":1249},[1276],{"type":28,"value":1252},{"type":18,"tag":174,"props":1278,"children":1279},{"style":1255},[1280],{"type":28,"value":1281}," uninstall",{"type":18,"tag":174,"props":1283,"children":1284},{"style":1255},[1285],{"type":28,"value":1263},{"type":18,"tag":174,"props":1287,"children":1288},{"style":1266},[1289],{"type":28,"value":1290},"   # 卸载\n",{"type":18,"tag":174,"props":1292,"children":1293},{"class":176,"line":366},[1294,1298,1303],{"type":18,"tag":174,"props":1295,"children":1296},{"style":1249},[1297],{"type":28,"value":1252},{"type":18,"tag":174,"props":1299,"children":1300},{"style":1255},[1301],{"type":28,"value":1302}," list",{"type":18,"tag":174,"props":1304,"children":1305},{"style":1266},[1306],{"type":28,"value":1307},"                     # 查看已安装列表\n",{"type":18,"tag":30,"props":1309,"children":1310},{},[1311],{"type":28,"value":1312},"模块导入时的顺序推荐：",{"type":18,"tag":445,"props":1314,"children":1315},{},[1316,1321,1326],{"type":18,"tag":59,"props":1317,"children":1318},{},[1319],{"type":28,"value":1320},"导入Python提供的标准模块",{"type":18,"tag":59,"props":1322,"children":1323},{},[1324],{"type":28,"value":1325},"导入第三方模块",{"type":18,"tag":59,"props":1327,"children":1328},{},[1329],{"type":28,"value":1330},"导入自定义模块",{"type":18,"tag":1332,"props":1333,"children":1334},"style",{},[1335],{"type":28,"value":1336},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":7,"searchDepth":357,"depth":357,"links":1338},[1339,1340,1341,1342,1343],{"id":25,"depth":366,"text":25},{"id":46,"depth":366,"text":46},{"id":717,"depth":366,"text":720},{"id":1003,"depth":366,"text":1003},{"id":1213,"depth":366,"text":1213},"markdown","content:blog:2025-07-08-python-模块.md","content","blog\u002F2025-07-08-python-模块.md","blog\u002F2025-07-08-python-模块","md",1780801018429]