[{"data":1,"prerenderedAt":844},["ShallowReactive",2],{"doc-\u002Fglossary\u002Fwhat-is-a-package-in-python":3},{"id":4,"title":5,"body":6,"description":837,"extension":838,"meta":839,"navigation":75,"path":840,"seo":841,"stem":842,"__hash__":843},"content\u002Fglossary\u002Fwhat-is-a-package-in-python.md","What Is a Package in Python?",{"type":7,"value":8,"toc":816},"minimark",[9,13,30,33,41,149,154,186,189,194,197,200,233,236,249,262,266,269,293,296,325,328,341,352,356,359,362,370,372,391,394,401,403,409,412,416,419,422,439,442,446,449,452,463,470,473,490,497,504,513,516,524,538,541,545,551,554,580,583,605,609,612,637,640,697,700,723,734,738,743,746,750,753,760,763,767,770,776,782,786,812],[10,11,5],"h1",{"id":12},"what-is-a-package-in-python",[14,15,16,17,21,22,25,26,29],"p",{},"A ",[18,19,20],"strong",{},"package"," in Python is a way to organize related code.",[23,24],"br",{},"\nIn simple terms, a package is usually a ",[18,27,28],{},"folder"," that contains Python modules and sometimes other package folders.",[14,31,32],{},"This helps keep code grouped in a clear structure instead of putting everything into one large file.",[14,34,35,36,40],{},"Beginners often use packages before they know the term. For example, when you write an ",[37,38,39],"code",{},"import"," statement, you may already be using a module that belongs to a package.",[42,43,48],"pre",{"className":44,"code":45,"language":46,"meta":47,"style":47},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import math\nimport json\n\nprint(math.sqrt(16))\nprint(json.dumps({\"name\": \"Sam\"}))\n","python","",[37,49,50,62,70,77,107],{"__ignoreMap":47},[51,52,55,58],"span",{"class":53,"line":54},"line",1,[51,56,39],{"class":57},"sVHd0",[51,59,61],{"class":60},"su5hD"," math\n",[51,63,65,67],{"class":53,"line":64},2,[51,66,39],{"class":57},[51,68,69],{"class":60}," json\n",[51,71,73],{"class":53,"line":72},3,[51,74,76],{"emptyLinePlaceholder":75},true,"\n",[51,78,80,84,88,92,95,98,100,104],{"class":53,"line":79},4,[51,81,83],{"class":82},"sptTA","print",[51,85,87],{"class":86},"sP7_E","(",[51,89,91],{"class":90},"slqww","math",[51,93,94],{"class":86},".",[51,96,97],{"class":90},"sqrt",[51,99,87],{"class":86},[51,101,103],{"class":102},"srdBf","16",[51,105,106],{"class":86},"))\n",[51,108,110,112,114,117,119,122,125,129,133,135,138,141,144,146],{"class":53,"line":109},5,[51,111,83],{"class":82},[51,113,87],{"class":86},[51,115,116],{"class":90},"json",[51,118,94],{"class":86},[51,120,121],{"class":90},"dumps",[51,123,124],{"class":86},"({",[51,126,128],{"class":127},"sjJ54","\"",[51,130,132],{"class":131},"s_sjI","name",[51,134,128],{"class":127},[51,136,137],{"class":86},":",[51,139,140],{"class":127}," \"",[51,142,143],{"class":131},"Sam",[51,145,128],{"class":127},[51,147,148],{"class":86},"}))\n",[14,150,151],{},[18,152,153],{},"Output:",[42,155,157],{"className":44,"code":156,"language":46,"meta":47,"style":47},"4.0\n{\"name\": \"Sam\"}\n",[37,158,159,164],{"__ignoreMap":47},[51,160,161],{"class":53,"line":54},[51,162,163],{"class":102},"4.0\n",[51,165,166,169,171,173,175,177,179,181,183],{"class":53,"line":64},[51,167,168],{"class":86},"{",[51,170,128],{"class":127},[51,172,132],{"class":131},[51,174,128],{"class":127},[51,176,137],{"class":86},[51,178,140],{"class":127},[51,180,143],{"class":131},[51,182,128],{"class":127},[51,184,185],{"class":86},"}\n",[14,187,188],{},"A package is a way to organize Python code. Beginners often use packages before they fully understand them.",[190,191,193],"h2",{"id":192},"what-a-package-means-in-python","What a package means in Python",[14,195,196],{},"A package helps organize related Python code.",[14,198,199],{},"Here is the basic idea:",[201,202,203,209,215,225,228],"ul",{},[204,205,206,207],"li",{},"A package is usually a ",[18,208,28],{},[204,210,211,212],{},"That folder contains one or more ",[18,213,214],{},"modules",[204,216,16,217,220,221,224],{},[18,218,219],{},"module"," is usually a single ",[37,222,223],{},".py"," file",[204,226,227],{},"Packages make larger programs easier to manage",[204,229,230,231],{},"You often notice packages when using ",[37,232,39],{},[14,234,235],{},"For example:",[201,237,238,243],{},[204,239,240,242],{},[37,241,91],{}," is a module",[204,244,245,246,248],{},"A folder containing several related ",[37,247,223],{}," files can be a package",[14,250,251,252,257,258,94],{},"If you are not fully sure what a module is, see ",[253,254,256],"a",{"href":255},"\u002Fglossary\u002Fwhat-is-a-module-in-python","what is a module in Python"," or ",[253,259,261],{"href":260},"\u002Flearn\u002Fpython-modules-explained","Python modules explained",[190,263,265],{"id":264},"package-vs-module","Package vs module",[14,267,268],{},"A package and a module are not the same thing.",[201,270,271,276,281,287],{},[204,272,16,273,275],{},[18,274,219],{}," is usually one Python file",[204,277,16,278,280],{},[18,279,20],{}," is a collection of modules in a folder",[204,282,283,284],{},"You import a module with code like ",[37,285,286],{},"import module_name",[204,288,289,290],{},"You can import from a package with code like ",[37,291,292],{},"import package.module",[14,294,295],{},"Example:",[42,297,299],{"className":44,"code":298,"language":46,"meta":47,"style":47},"import json\nimport xml.etree.ElementTree\n",[37,300,301,307],{"__ignoreMap":47},[51,302,303,305],{"class":53,"line":54},[51,304,39],{"class":57},[51,306,69],{"class":60},[51,308,309,311,314,316,320,322],{"class":53,"line":64},[51,310,39],{"class":57},[51,312,313],{"class":60}," xml",[51,315,94],{"class":86},[51,317,319],{"class":318},"skxfh","etree",[51,321,94],{"class":86},[51,323,324],{"class":318},"ElementTree\n",[14,326,327],{},"In this example:",[201,329,330,335],{},[204,331,332,334],{},[37,333,116],{}," is imported directly",[204,336,337,340],{},[37,338,339],{},"xml.etree.ElementTree"," shows a name inside a larger package structure",[14,342,343,344,257,348,94],{},"This page is only defining the term. If you want the full beginner explanation of imports, read ",[253,345,347],{"href":346},"\u002Flearn\u002Fhow-import-works-in-python","how import works in Python",[253,349,351],{"href":350},"\u002Fhow-to\u002Fhow-to-import-a-module-in-python","how to import a module in Python",[190,353,355],{"id":354},"simple-package-structure","Simple package structure",[14,357,358],{},"A package often looks like a folder containing multiple Python files.",[14,360,361],{},"Example structure:",[42,363,368],{"className":364,"code":366,"language":367,"meta":47},[365],"language-text","my_project\u002F\n└── tools\u002F\n    ├── __init__.py\n    ├── math_helpers.py\n    └── string_helpers.py\n","text",[37,369,366],{"__ignoreMap":47},[14,371,327],{},[201,373,374,380,385],{},[204,375,376,379],{},[37,377,378],{},"tools"," is the package folder",[204,381,382,242],{},[37,383,384],{},"math_helpers.py",[204,386,387,390],{},[37,388,389],{},"string_helpers.py"," is another module",[14,392,393],{},"Related code is grouped together inside one folder.",[14,395,396,397,400],{},"A package can also contain ",[18,398,399],{},"subpackages",", which are package folders inside other package folders.",[14,402,295],{},[42,404,407],{"className":405,"code":406,"language":367,"meta":47},[365],"my_project\u002F\n└── tools\u002F\n    ├── __init__.py\n    ├── math_helpers.py\n    └── text\u002F\n        ├── __init__.py\n        └── string_helpers.py\n",[37,408,406],{"__ignoreMap":47},[14,410,411],{},"This makes it easier to organize larger projects.",[190,413,415],{"id":414},"why-packages-are-useful","Why packages are useful",[14,417,418],{},"Packages are useful because they help structure code clearly.",[14,420,421],{},"They help by:",[201,423,424,427,430,433,436],{},[204,425,426],{},"Keeping code organized",[204,428,429],{},"Grouping related features together",[204,431,432],{},"Reducing confusion in larger programs",[204,434,435],{},"Making code easier to reuse",[204,437,438],{},"Making code easier to share",[14,440,441],{},"Without packages, a project can quickly become messy if every feature is placed in one file.",[190,443,445],{"id":444},"common-examples-beginners-already-use","Common examples beginners already use",[14,447,448],{},"Beginners often use packages and modules from Python’s standard library.",[14,450,451],{},"Common examples include:",[201,453,454,458],{},[204,455,456],{},[37,457,116],{},[204,459,460],{},[37,461,462],{},"os",[14,464,465,466,469],{},"You may also install third-party tools with ",[37,467,468],{},"pip",". Many of those are distributed as packages.",[14,471,472],{},"Important beginner note:",[201,474,475,482,485],{},[204,476,477,478,481],{},"You do ",[18,479,480],{},"not"," need to build your own package before you can use one",[204,483,484],{},"You can use packages just by importing them",[204,486,487,488],{},"You can install many external packages with ",[37,489,468],{},[14,491,492,493,94],{},"If you want to do that next, see ",[253,494,496],{"href":495},"\u002Fhow-to\u002Fhow-to-install-a-python-package-with-pip","how to install a Python package with pip",[190,498,500,501],{"id":499},"important-beginner-note-about-__init__py","Important beginner note about ",[37,502,503],{},"__init__.py",[14,505,506,507,510,511,94],{},"Older explanations often say that a package ",[18,508,509],{},"must"," contain a file named ",[37,512,503],{},[14,514,515],{},"That file is still very common and still useful.",[14,517,518,519,94,521,523],{},"In modern Python, some packages can work without ",[37,520,503],{},[23,522],{},"\nFor beginner learning, the important thing to know is:",[201,525,526,532,535],{},[204,527,528,529,531],{},"You will often see ",[37,530,503],{}," in package folders",[204,533,534],{},"It is normal for package examples to include it",[204,536,537],{},"Its presence often helps show that a folder is meant to be part of a package structure",[14,539,540],{},"So if you see this file, do not worry. It is a normal part of many Python package examples.",[190,542,544],{"id":543},"when-this-term-is-usually-confusing","When this term is usually confusing",[14,546,547,548,550],{},"The word ",[18,549,20],{}," is often confusing because beginners mix it up with other Python terms.",[14,552,553],{},"Common confusion points:",[201,555,556,568,571,577],{},[204,557,558,559,561,562,564,565],{},"Mixing up ",[18,560,20],{},", ",[18,563,219],{},", and ",[18,566,567],{},"library",[204,569,570],{},"Thinking every imported name is a package",[204,572,573,574,576],{},"Thinking ",[37,575,468],{}," only installs modules",[204,578,579],{},"Confusing an ordinary folder with an importable package",[14,581,582],{},"A useful way to remember it:",[201,584,585,593,599],{},[204,586,587,590,591,224],{},[18,588,589],{},"Module"," = usually one ",[37,592,223],{},[204,594,595,598],{},[18,596,597],{},"Package"," = a folder that groups related modules",[204,600,601,604],{},[18,602,603],{},"Library"," = a broader term for reusable code",[190,606,608],{"id":607},"common-mistakes","Common mistakes",[14,610,611],{},"These are common mistakes beginners make when learning this term:",[201,613,614,620,628,631],{},[204,615,616,617,619],{},"Confusing a single ",[37,618,223],{}," file with a package",[204,621,573,622,624,625,627],{},[18,623,20],{}," and ",[18,626,567],{}," mean exactly the same thing",[204,629,630],{},"Not realizing that packages help organize multiple modules",[204,632,633,634,636],{},"Seeing ",[37,635,292],{}," and not knowing that it often matches a folder structure",[14,638,639],{},"If you run into import problems while working with packages, these commands can help:",[42,641,645],{"className":642,"code":643,"language":644,"meta":47,"style":47},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python --version\npython -c \"import json; print(json.__file__)\"\npython -c \"import os; print(os.__name__)\"\npython -c \"import sys; print(sys.path)\"\n","bash",[37,646,647,656,671,684],{"__ignoreMap":47},[51,648,649,652],{"class":53,"line":54},[51,650,46],{"class":651},"sbgvK",[51,653,655],{"class":654},"stzsN"," --version\n",[51,657,658,660,663,665,668],{"class":53,"line":64},[51,659,46],{"class":651},[51,661,662],{"class":654}," -c",[51,664,140],{"class":127},[51,666,667],{"class":131},"import json; print(json.__file__)",[51,669,670],{"class":127},"\"\n",[51,672,673,675,677,679,682],{"class":53,"line":72},[51,674,46],{"class":651},[51,676,662],{"class":654},[51,678,140],{"class":127},[51,680,681],{"class":131},"import os; print(os.__name__)",[51,683,670],{"class":127},[51,685,686,688,690,692,695],{"class":53,"line":79},[51,687,46],{"class":651},[51,689,662],{"class":654},[51,691,140],{"class":127},[51,693,694],{"class":131},"import sys; print(sys.path)",[51,696,670],{"class":127},[14,698,699],{},"What these do:",[201,701,702,708,713,718],{},[204,703,704,707],{},[37,705,706],{},"python --version"," shows which Python version you are using",[204,709,710,712],{},[37,711,667],{}," shows where the imported code comes from",[204,714,715,717],{},[37,716,681],{}," shows the imported module name",[204,719,720,722],{},[37,721,694],{}," shows where Python looks for importable code",[14,724,725,726,257,730,94],{},"If Python cannot find a package or module, see ",[253,727,729],{"href":728},"\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix","ModuleNotFoundError: No module named x",[253,731,733],{"href":732},"\u002Ferrors\u002Fimporterror-no-module-named-x-fix","ImportError: No module named x",[190,735,737],{"id":736},"faq","FAQ",[739,740,742],"h3",{"id":741},"is-a-package-the-same-as-a-module-in-python","Is a package the same as a module in Python?",[14,744,745],{},"No. A module is usually one Python file. A package is a folder that groups related modules.",[739,747,749],{"id":748},"is-every-folder-a-python-package","Is every folder a Python package?",[14,751,752],{},"No. A folder becomes useful as a package when Python can import from it.",[739,754,756,757,759],{"id":755},"do-python-packages-always-need-__init__py","Do Python packages always need ",[37,758,503],{},"?",[14,761,762],{},"Not always in modern Python, but beginners will often see that file in package examples.",[739,764,766],{"id":765},"what-is-the-difference-between-a-package-and-a-library","What is the difference between a package and a library?",[14,768,769],{},"A package is a Python code organization unit. A library is a broader term for reusable code and may contain one or more packages.",[739,771,773,774,759],{"id":772},"can-i-install-packages-with-pip","Can I install packages with ",[37,775,468],{},[14,777,778,779,781],{},"Yes. Many third-party tools installed with ",[37,780,468],{}," are distributed as packages.",[190,783,785],{"id":784},"see-also","See also",[201,787,788,793,797,802,807],{},[204,789,790],{},[253,791,792],{"href":255},"What is a module in Python?",[204,794,795],{},[253,796,261],{"href":260},[204,798,799],{},[253,800,801],{"href":346},"How import works in Python",[204,803,804],{},[253,805,806],{"href":350},"How to import a module in Python",[204,808,809],{},[253,810,811],{"href":495},"How to install a Python package with pip",[813,814,815],"style",{},"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 .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":64,"depth":64,"links":817},[818,819,820,821,822,823,825,826,827,836],{"id":192,"depth":64,"text":193},{"id":264,"depth":64,"text":265},{"id":354,"depth":64,"text":355},{"id":414,"depth":64,"text":415},{"id":444,"depth":64,"text":445},{"id":499,"depth":64,"text":824},"Important beginner note about __init__.py",{"id":543,"depth":64,"text":544},{"id":607,"depth":64,"text":608},{"id":736,"depth":64,"text":737,"children":828},[829,830,831,833,834],{"id":741,"depth":72,"text":742},{"id":748,"depth":72,"text":749},{"id":755,"depth":72,"text":832},"Do Python packages always need __init__.py?",{"id":765,"depth":72,"text":766},{"id":772,"depth":72,"text":835},"Can I install packages with pip?",{"id":784,"depth":64,"text":785},"Master what is a package in python in our comprehensive Python beginner guide.","md",{},"\u002Fglossary\u002Fwhat-is-a-package-in-python",{"title":5,"description":837},"glossary\u002Fwhat-is-a-package-in-python","plJQM12dREgcyD_U59ahFyEs90e3GnTg_jOjcZizaIo",1777585468119]