[{"data":1,"prerenderedAt":866},["ShallowReactive",2],{"doc-\u002Fglossary\u002Fwhat-is-a-module-in-python":3},{"id":4,"title":5,"body":6,"description":859,"extension":860,"meta":861,"navigation":248,"path":862,"seo":863,"stem":864,"__hash__":865},"content\u002Fglossary\u002Fwhat-is-a-module-in-python.md","What Is a Module in Python?",{"type":7,"value":8,"toc":837},"minimark",[9,13,22,25,44,51,54,62,76,81,88,95,98,121,124,130,134,137,140,166,169,173,176,179,222,225,324,327,339,346,350,355,361,364,379,390,394,404,407,424,427,467,470,479,486,491,494,498,501,508,541,547,589,591,607,610,632,635,644,648,651,672,682,685,689,692,741,751,755,759,762,768,772,775,778,782,785,789,791,802,806,833],[10,11,5],"h1",{"id":12},"what-is-a-module-in-python",[14,15,16,17,21],"p",{},"A ",[18,19,20],"strong",{},"module"," in Python is a file that contains code you can reuse.",[14,23,24],{},"That file can contain:",[26,27,28,32,35,38,41],"ul",{},[29,30,31],"li",{},"functions",[29,33,34],{},"variables",[29,36,37],{},"classes",[29,39,40],{},"import statements",[29,42,43],{},"code that runs when the file is imported or executed",[14,45,46,47,50],{},"You use a module by ",[18,48,49],{},"importing"," it into another Python file.",[14,52,53],{},"A simple way to think about it is this:",[26,55,56,59],{},[29,57,58],{},"one file stores useful code",[29,60,61],{},"another file uses that code",[14,63,64,65,69,70,75],{},"This page explains what a module is and why it matters. If you want the full mechanics of ",[66,67,68],"code",{},"import",", see ",[71,72,74],"a",{"href":73},"\u002Flearn\u002Fhow-import-works-in-python\u002F","how import works in Python",".",[77,78,80],"h2",{"id":79},"simple-definition","Simple definition",[14,82,83,84,87],{},"A Python module is usually a single ",[66,85,86],{},".py"," file.",[14,89,90,91,94],{},"For example, if you have a file named ",[66,92,93],{},"helpers.py",", that file can be a module. Another file can import it and use code from it.",[14,96,97],{},"Here is the basic idea:",[26,99,100,105,113],{},[29,101,102,104],{},[66,103,93],{}," contains reusable code",[29,106,107,110,111],{},[66,108,109],{},"main.py"," imports ",[66,112,93],{},[29,114,115,117,118,120],{},[66,116,109],{}," uses what ",[66,119,93],{}," provides",[14,122,123],{},"In beginner terms, this is the most important idea:",[125,126,127],"blockquote",{},[14,128,129],{},"A module is a Python file that helps you organize and reuse code.",[77,131,133],{"id":132},"why-modules-are-useful","Why modules are useful",[14,135,136],{},"Modules make Python programs easier to work with.",[14,138,139],{},"They help because:",[26,141,142,148,154,160],{},[29,143,144,147],{},[18,145,146],{},"They organize code"," into smaller files",[29,149,150,153],{},[18,151,152],{},"They reduce repetition"," because you can reuse the same code",[29,155,156,159],{},[18,157,158],{},"They improve readability"," by separating different parts of a program",[29,161,162,165],{},[18,163,164],{},"They make projects easier to maintain"," as your code grows",[14,167,168],{},"Without modules, you may end up putting everything into one large file. That quickly becomes hard to read and update.",[77,170,172],{"id":171},"what-can-be-inside-a-module","What can be inside a module",[14,174,175],{},"A module can contain many kinds of Python code.",[14,177,178],{},"Common examples include:",[26,180,181,194,205,216],{},[29,182,183,186,187,190,191],{},[18,184,185],{},"Functions"," such as ",[66,188,189],{},"greet()"," or ",[66,192,193],{},"calculate_total()",[29,195,196,186,199,190,202],{},[18,197,198],{},"Variables",[66,200,201],{},"TAX_RATE",[66,203,204],{},"app_name",[29,206,207,186,210,190,213],{},[18,208,209],{},"Classes",[66,211,212],{},"User",[66,214,215],{},"Car",[29,217,218,221],{},[18,219,220],{},"Imports and supporting code"," used by the module",[14,223,224],{},"Example module:",[226,227,232],"pre",{"className":228,"code":229,"language":230,"meta":231,"style":231},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","# helpers.py\n\napp_name = \"My Program\"\n\ndef greet(name):\n    return f\"Hello, {name}!\"\n","python","",[66,233,234,243,250,272,277,299],{"__ignoreMap":231},[235,236,239],"span",{"class":237,"line":238},"line",1,[235,240,242],{"class":241},"sutJx","# helpers.py\n",[235,244,246],{"class":237,"line":245},2,[235,247,249],{"emptyLinePlaceholder":248},true,"\n",[235,251,253,257,261,265,269],{"class":237,"line":252},3,[235,254,256],{"class":255},"su5hD","app_name ",[235,258,260],{"class":259},"smGrS","=",[235,262,264],{"class":263},"sjJ54"," \"",[235,266,268],{"class":267},"s_sjI","My Program",[235,270,271],{"class":263},"\"\n",[235,273,275],{"class":237,"line":274},4,[235,276,249],{"emptyLinePlaceholder":248},[235,278,280,284,288,292,296],{"class":237,"line":279},5,[235,281,283],{"class":282},"sbsja","def",[235,285,287],{"class":286},"sGLFI"," greet",[235,289,291],{"class":290},"sP7_E","(",[235,293,295],{"class":294},"sFwrP","name",[235,297,298],{"class":290},"):\n",[235,300,302,306,309,312,316,318,321],{"class":237,"line":301},6,[235,303,305],{"class":304},"sVHd0","    return",[235,307,308],{"class":282}," f",[235,310,311],{"class":267},"\"Hello, ",[235,313,315],{"class":314},"srdBf","{",[235,317,295],{"class":255},[235,319,320],{"class":314},"}",[235,322,323],{"class":267},"!\"\n",[14,325,326],{},"This module contains:",[26,328,329,334],{},[29,330,331,332],{},"one variable: ",[66,333,204],{},[29,335,336,337],{},"one function: ",[66,338,189],{},[14,340,341,342,345],{},"A module does ",[18,343,344],{},"not"," have to contain only functions. Beginners often assume that, but modules can store many kinds of code.",[77,347,349],{"id":348},"module-vs-package","Module vs package",[14,351,16,352,354],{},[18,353,20],{}," is usually one Python file.",[14,356,16,357,360],{},[18,358,359],{},"package"," is a folder that groups related modules together.",[14,362,363],{},"So the difference is:",[26,365,366,374],{},[29,367,368,370,371,373],{},[18,369,20],{}," = one ",[66,372,86],{}," file",[29,375,376,378],{},[18,377,359],{}," = one folder containing related Python code",[14,380,381,382,385,386,75],{},"This page is only defining the term ",[383,384,20],"em",{},". For the full distinction, see ",[71,387,389],{"href":388},"\u002Fglossary\u002Fwhat-is-a-package-in-python\u002F","what is a package in Python",[77,391,393],{"id":392},"built-in-modules-and-your-own-modules","Built-in modules and your own modules",[14,395,396,397,190,400,403],{},"Some modules come with Python already installed. These are often called ",[18,398,399],{},"built-in",[18,401,402],{},"standard library"," modules.",[14,405,406],{},"Examples include:",[26,408,409,414,419],{},[29,410,411],{},[66,412,413],{},"math",[29,415,416],{},[66,417,418],{},"random",[29,420,421],{},[66,422,423],{},"os",[14,425,426],{},"For example:",[226,428,430],{"className":228,"code":429,"language":230,"meta":231,"style":231},"import math\n\nprint(math.sqrt(9))\n",[66,431,432,439,443],{"__ignoreMap":231},[235,433,434,436],{"class":237,"line":238},[235,435,68],{"class":304},[235,437,438],{"class":255}," math\n",[235,440,441],{"class":237,"line":245},[235,442,249],{"emptyLinePlaceholder":248},[235,444,445,449,451,454,456,459,461,464],{"class":237,"line":252},[235,446,448],{"class":447},"sptTA","print",[235,450,291],{"class":290},[235,452,413],{"class":453},"slqww",[235,455,75],{"class":290},[235,457,458],{"class":453},"sqrt",[235,460,291],{"class":290},[235,462,463],{"class":314},"9",[235,465,466],{"class":290},"))\n",[14,468,469],{},"Expected output:",[226,471,473],{"className":228,"code":472,"language":230,"meta":231,"style":231},"3.0\n",[66,474,475],{"__ignoreMap":231},[235,476,477],{"class":237,"line":238},[235,478,472],{"class":314},[14,480,481,482,75],{},"You can learn more from this ",[71,483,485],{"href":484},"\u002Fstandard-library\u002Fpython-math-module-overview\u002F","Python math module overview",[14,487,488,489,87],{},"You can also create your own module by making a ",[66,490,86],{},[14,492,493],{},"Beginners often start by importing built-in modules first. Later, when programs get larger, custom modules become very useful for organizing your own code.",[77,495,497],{"id":496},"basic-example-idea","Basic example idea",[14,499,500],{},"Here is a minimal example showing one custom module and one file that uses it.",[502,503,505,506],"h3",{"id":504},"file-1-helperspy","File 1: ",[66,507,93],{},[226,509,511],{"className":228,"code":510,"language":230,"meta":231,"style":231},"def greet(name):\n    return f\"Hello, {name}!\"\n",[66,512,513,525],{"__ignoreMap":231},[235,514,515,517,519,521,523],{"class":237,"line":238},[235,516,283],{"class":282},[235,518,287],{"class":286},[235,520,291],{"class":290},[235,522,295],{"class":294},[235,524,298],{"class":290},[235,526,527,529,531,533,535,537,539],{"class":237,"line":245},[235,528,305],{"class":304},[235,530,308],{"class":282},[235,532,311],{"class":267},[235,534,315],{"class":314},[235,536,295],{"class":255},[235,538,320],{"class":314},[235,540,323],{"class":267},[502,542,544,545],{"id":543},"file-2-mainpy","File 2: ",[66,546,109],{},[226,548,550],{"className":228,"code":549,"language":230,"meta":231,"style":231},"import helpers\n\nprint(helpers.greet(\"Ava\"))\n",[66,551,552,559,563],{"__ignoreMap":231},[235,553,554,556],{"class":237,"line":238},[235,555,68],{"class":304},[235,557,558],{"class":255}," helpers\n",[235,560,561],{"class":237,"line":245},[235,562,249],{"emptyLinePlaceholder":248},[235,564,565,567,569,572,574,577,579,582,585,587],{"class":237,"line":252},[235,566,448],{"class":447},[235,568,291],{"class":290},[235,570,571],{"class":453},"helpers",[235,573,75],{"class":290},[235,575,576],{"class":453},"greet",[235,578,291],{"class":290},[235,580,581],{"class":263},"\"",[235,583,584],{"class":267},"Ava",[235,586,581],{"class":263},[235,588,466],{"class":290},[14,590,469],{},[226,592,594],{"className":228,"code":593,"language":230,"meta":231,"style":231},"Hello, Ava!\n",[66,595,596],{"__ignoreMap":231},[235,597,598,601,604],{"class":237,"line":238},[235,599,600],{"class":255},"Hello",[235,602,603],{"class":290},",",[235,605,606],{"class":255}," Ava!\n",[14,608,609],{},"What is happening here:",[26,611,612,619,624],{},[29,613,614,616,617],{},[66,615,93],{}," is the ",[18,618,20],{},[29,620,621,623],{},[66,622,109],{}," imports that module",[29,625,626,628,629,631],{},[66,627,109],{}," uses the ",[66,630,189],{}," function from the module",[14,633,634],{},"This is one of the simplest examples of a Python module.",[14,636,637,638,190,642,75],{},"If you want to go deeper into the import statement, read ",[71,639,641],{"href":640},"\u002Flearn\u002Fpython-modules-explained\u002F","Python modules explained",[71,643,74],{"href":73},[77,645,647],{"id":646},"important-beginner-note","Important beginner note",[14,649,650],{},"A few details are easy to miss when you are new to modules.",[26,652,653,659,666,669],{},[29,654,655,656,658],{},"A module is ",[18,657,344],{}," the same as a package",[29,660,661,662,665],{},"Importing a module runs ",[18,663,664],{},"top-level code"," in that file",[29,667,668],{},"The file name matters when importing",[29,670,671],{},"You should avoid naming your file the same as a standard library module",[14,673,674,675,678,679,681],{},"For example, if you create a file named ",[66,676,677],{},"random.py",", it can conflict with Python’s built-in ",[66,680,418],{}," module.",[14,683,684],{},"That can cause confusing import problems later.",[77,686,688],{"id":687},"common-mistakes","Common mistakes",[14,690,691],{},"Beginners often run into these problems:",[26,693,694,703,714,722],{},[29,695,696,699,702],{},[18,697,698],{},"Confusing a module with a package",[700,701],"br",{},"\nA module is usually one file. A package is a folder of related modules.",[29,704,705,708,710,711,713],{},[18,706,707],{},"Thinking a module must be built in",[700,709],{},"\nBuilt-in modules are modules, but your own ",[66,712,86],{}," files can be modules too.",[29,715,716,719,721],{},[18,717,718],{},"Assuming a module can only contain functions",[700,720],{},"\nModules can also contain variables, classes, imports, and other code.",[29,723,724,727,729,730,732,733,736,737,740],{},[18,725,726],{},"Naming a file after a standard library module",[700,728],{},"\nAvoid names like ",[66,731,677],{},", ",[66,734,735],{},"json.py",", or ",[66,738,739],{},"math.py",". These can shadow the real module and cause import errors.",[14,742,743,744,75],{},"If Python cannot find the module you expect, see ",[71,745,747,748],{"href":746},"\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix\u002F","how to fix ",[66,749,750],{},"ModuleNotFoundError: No module named 'x'",[77,752,754],{"id":753},"faq","FAQ",[502,756,758],{"id":757},"is-a-module-the-same-as-a-python-file","Is a module the same as a Python file?",[14,760,761],{},"Usually, yes.",[14,763,764,765,767],{},"In beginner terms, a module is commonly a ",[66,766,86],{}," file that can be imported.",[502,769,771],{"id":770},"can-i-make-my-own-module","Can I make my own module?",[14,773,774],{},"Yes.",[14,776,777],{},"Create a Python file and import it from another file.",[502,779,781],{"id":780},"what-is-the-difference-between-a-module-and-a-package","What is the difference between a module and a package?",[14,783,784],{},"A module is one file. A package is a folder that groups modules together.",[502,786,788],{"id":787},"do-built-in-modules-count-as-modules","Do built-in modules count as modules?",[14,790,774],{},[14,792,793,794,732,796,798,799,801],{},"Modules like ",[66,795,413],{},[66,797,418],{},", and ",[66,800,423],{}," are Python modules.",[77,803,805],{"id":804},"see-also","See also",[26,807,808,812,817,822,826],{},[29,809,810],{},[71,811,641],{"href":640},[29,813,814],{},[71,815,816],{"href":73},"How import works in Python",[29,818,819],{},[71,820,821],{"href":388},"What is a package in Python?",[29,823,824],{},[71,825,485],{"href":484},[29,827,828],{},[71,829,830,831],{"href":746},"How to fix ",[66,832,750],{},[834,835,836],"style",{},"html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sFwrP, html code.shiki .sFwrP{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}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);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":231,"searchDepth":245,"depth":245,"links":838},[839,840,841,842,843,844,850,851,852,858],{"id":79,"depth":245,"text":80},{"id":132,"depth":245,"text":133},{"id":171,"depth":245,"text":172},{"id":348,"depth":245,"text":349},{"id":392,"depth":245,"text":393},{"id":496,"depth":245,"text":497,"children":845},[846,848],{"id":504,"depth":252,"text":847},"File 1: helpers.py",{"id":543,"depth":252,"text":849},"File 2: main.py",{"id":646,"depth":245,"text":647},{"id":687,"depth":245,"text":688},{"id":753,"depth":245,"text":754,"children":853},[854,855,856,857],{"id":757,"depth":252,"text":758},{"id":770,"depth":252,"text":771},{"id":780,"depth":252,"text":781},{"id":787,"depth":252,"text":788},{"id":804,"depth":245,"text":805},"Master what is a module in python in our comprehensive Python beginner guide.","md",{},"\u002Fglossary\u002Fwhat-is-a-module-in-python",{"title":5,"description":859},"glossary\u002Fwhat-is-a-module-in-python","Z48eM-EacvLNUUHwMGEDdwdzueQ7jHeHVQBZfJyRITA",1777585468118]