[{"data":1,"prerenderedAt":1773},["ShallowReactive",2],{"doc-\u002Flearn\u002Fhow-import-works-in-python":3},{"id":4,"title":5,"body":6,"description":1766,"extension":1767,"meta":1768,"navigation":88,"path":1769,"seo":1770,"stem":1771,"__hash__":1772},"content\u002Flearn\u002Fhow-import-works-in-python.md","How Import Works in Python",{"type":7,"value":8,"toc":1723},"minimark",[9,13,21,27,32,120,123,138,144,166,170,176,179,202,216,219,230,238,242,245,252,286,289,292,303,309,342,345,348,356,359,365,405,408,411,419,425,465,468,476,480,486,551,553,561,564,587,590,604,608,611,614,639,645,692,698,753,755,771,774,788,791,808,811,825,832,836,839,842,856,862,865,868,893,904,916,920,923,926,937,940,946,995,1000,1029,1031,1052,1055,1067,1070,1077,1085,1088,1165,1168,1180,1183,1192,1195,1206,1209,1213,1216,1220,1234,1237,1280,1283,1286,1290,1293,1296,1307,1314,1317,1333,1336,1339,1350,1354,1357,1360,1372,1383,1390,1394,1399,1406,1410,1413,1442,1445,1497,1505,1509,1512,1529,1532,1585,1588,1602,1606,1616,1622,1629,1635,1638,1646,1650,1653,1656,1660,1663,1666,1672,1675,1678,1682,1719],[10,11,5],"h1",{"id":12},"how-import-works-in-python",[14,15,16,20],"p",{},[17,18,19],"code",{},"import"," lets you use code from another Python file or from Python’s built-in and installed modules.",[14,22,23,24,26],{},"This is one of the most important Python basics. Once you understand how ",[17,25,19],{}," works, it becomes much easier to read other people’s code and organize your own code into separate files.",[28,29,31],"h2",{"id":30},"quick-example","Quick example",[33,34,39],"pre",{"className":35,"code":36,"language":37,"meta":38,"style":38},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import math\nprint(math.sqrt(16))\n\nfrom math import sqrt\nprint(sqrt(25))\n","python","",[17,40,41,53,83,90,104],{"__ignoreMap":38},[42,43,46,49],"span",{"class":44,"line":45},"line",1,[42,47,19],{"class":48},"sVHd0",[42,50,52],{"class":51},"su5hD"," math\n",[42,54,56,60,64,68,71,74,76,80],{"class":44,"line":55},2,[42,57,59],{"class":58},"sptTA","print",[42,61,63],{"class":62},"sP7_E","(",[42,65,67],{"class":66},"slqww","math",[42,69,70],{"class":62},".",[42,72,73],{"class":66},"sqrt",[42,75,63],{"class":62},[42,77,79],{"class":78},"srdBf","16",[42,81,82],{"class":62},"))\n",[42,84,86],{"class":44,"line":85},3,[42,87,89],{"emptyLinePlaceholder":88},true,"\n",[42,91,93,96,99,101],{"class":44,"line":92},4,[42,94,95],{"class":48},"from",[42,97,98],{"class":51}," math ",[42,100,19],{"class":48},[42,102,103],{"class":51}," sqrt\n",[42,105,107,109,111,113,115,118],{"class":44,"line":106},5,[42,108,59],{"class":58},[42,110,63],{"class":62},[42,112,73],{"class":66},[42,114,63],{"class":62},[42,116,117],{"class":78},"25",[42,119,82],{"class":62},[14,121,122],{},"Output:",[33,124,126],{"className":35,"code":125,"language":37,"meta":38,"style":38},"4.0\n5.0\n",[17,127,128,133],{"__ignoreMap":38},[42,129,130],{"class":44,"line":45},[42,131,132],{"class":78},"4.0\n",[42,134,135],{"class":44,"line":55},[42,136,137],{"class":78},"5.0\n",[14,139,140,141,143],{},"Use ",[17,142,19],{}," when you want to access code from another module.",[145,146,147,157],"ul",{},[148,149,150,153,154],"li",{},[17,151,152],{},"import math"," keeps the module name, so you write ",[17,155,156],{},"math.sqrt(...)",[148,158,159,162,163],{},[17,160,161],{},"from math import sqrt"," imports one specific name, so you write ",[17,164,165],{},"sqrt(...)",[28,167,169],{"id":168},"what-import-means","What import means",[14,171,172,173,175],{},"In Python, ",[17,174,19],{}," brings code from another module into your program.",[14,177,178],{},"A few useful terms:",[145,180,181,193,199],{},[148,182,183,184,188,189,192],{},"A ",[185,186,187],"strong",{},"module"," is usually one ",[17,190,191],{},".py"," file",[148,194,183,195,198],{},[185,196,197],{},"package"," is a folder that contains Python modules",[148,200,201],{},"An imported module is usually loaded once, then reused",[14,203,204,205,207,208,211,212,215],{},"For example, Python’s standard library includes modules like ",[17,206,67],{},", ",[17,209,210],{},"random",", and ",[17,213,214],{},"json",". You can also import your own files.",[14,217,218],{},"When Python imports a module, it:",[145,220,221,224,227],{},[148,222,223],{},"finds the module",[148,225,226],{},"runs its top-level code once",[148,228,229],{},"makes its functions, classes, and variables available",[14,231,232,233,70],{},"If you are new to this topic, you may also want to read ",[234,235,237],"a",{"href":236},"\u002Flearn\u002Fpython-modules-explained","Python modules explained",[28,239,241],{"id":240},"basic-import-forms","Basic import forms",[14,243,244],{},"These are the main import patterns beginners see most often.",[246,247,249],"h3",{"id":248},"import-module_name",[17,250,251],{},"import module_name",[33,253,255],{"className":35,"code":254,"language":37,"meta":38,"style":38},"import math\n\nprint(math.sqrt(9))\n",[17,256,257,263,267],{"__ignoreMap":38},[42,258,259,261],{"class":44,"line":45},[42,260,19],{"class":48},[42,262,52],{"class":51},[42,264,265],{"class":44,"line":55},[42,266,89],{"emptyLinePlaceholder":88},[42,268,269,271,273,275,277,279,281,284],{"class":44,"line":85},[42,270,59],{"class":58},[42,272,63],{"class":62},[42,274,67],{"class":66},[42,276,70],{"class":62},[42,278,73],{"class":66},[42,280,63],{"class":62},[42,282,283],{"class":78},"9",[42,285,82],{"class":62},[14,287,288],{},"This form is usually the easiest to read.",[14,290,291],{},"Why:",[145,293,294,297,300],{},[148,295,296],{},"it shows where the function came from",[148,298,299],{},"it avoids name conflicts",[148,301,302],{},"it makes code clearer for beginners",[246,304,306],{"id":305},"from-module_name-import-name",[17,307,308],{},"from module_name import name",[33,310,312],{"className":35,"code":311,"language":37,"meta":38,"style":38},"from math import sqrt\n\nprint(sqrt(9))\n",[17,313,314,324,328],{"__ignoreMap":38},[42,315,316,318,320,322],{"class":44,"line":45},[42,317,95],{"class":48},[42,319,98],{"class":51},[42,321,19],{"class":48},[42,323,103],{"class":51},[42,325,326],{"class":44,"line":55},[42,327,89],{"emptyLinePlaceholder":88},[42,329,330,332,334,336,338,340],{"class":44,"line":85},[42,331,59],{"class":58},[42,333,63],{"class":62},[42,335,73],{"class":66},[42,337,63],{"class":62},[42,339,283],{"class":78},[42,341,82],{"class":62},[14,343,344],{},"This is shorter.",[14,346,347],{},"It is useful when:",[145,349,350,353],{},[148,351,352],{},"you only need one or two names",[148,354,355],{},"the imported name is very common in your code",[14,357,358],{},"The trade-off is that you no longer see the module name in each call.",[246,360,362],{"id":361},"import-module_name-as-alias",[17,363,364],{},"import module_name as alias",[33,366,368],{"className":35,"code":367,"language":37,"meta":38,"style":38},"import math as m\n\nprint(m.sqrt(9))\n",[17,369,370,382,386],{"__ignoreMap":38},[42,371,372,374,376,379],{"class":44,"line":45},[42,373,19],{"class":48},[42,375,98],{"class":51},[42,377,378],{"class":48},"as",[42,380,381],{"class":51}," m\n",[42,383,384],{"class":44,"line":55},[42,385,89],{"emptyLinePlaceholder":88},[42,387,388,390,392,395,397,399,401,403],{"class":44,"line":85},[42,389,59],{"class":58},[42,391,63],{"class":62},[42,393,394],{"class":66},"m",[42,396,70],{"class":62},[42,398,73],{"class":66},[42,400,63],{"class":62},[42,402,283],{"class":78},[42,404,82],{"class":62},[14,406,407],{},"This gives the module a shorter name.",[14,409,410],{},"This is helpful when:",[145,412,413,416],{},[148,414,415],{},"the module name is long",[148,417,418],{},"the alias is widely understood",[246,420,422],{"id":421},"from-module_name-import-name-as-alias",[17,423,424],{},"from module_name import name as alias",[33,426,428],{"className":35,"code":427,"language":37,"meta":38,"style":38},"from math import sqrt as square_root\n\nprint(square_root(9))\n",[17,429,430,446,450],{"__ignoreMap":38},[42,431,432,434,436,438,441,443],{"class":44,"line":45},[42,433,95],{"class":48},[42,435,98],{"class":51},[42,437,19],{"class":48},[42,439,440],{"class":51}," sqrt ",[42,442,378],{"class":48},[42,444,445],{"class":51}," square_root\n",[42,447,448],{"class":44,"line":55},[42,449,89],{"emptyLinePlaceholder":88},[42,451,452,454,456,459,461,463],{"class":44,"line":85},[42,453,59],{"class":58},[42,455,63],{"class":62},[42,457,458],{"class":66},"square_root",[42,460,63],{"class":62},[42,462,283],{"class":78},[42,464,82],{"class":62},[14,466,467],{},"This is useful when:",[145,469,470,473],{},[148,471,472],{},"you want a clearer local name",[148,474,475],{},"you need to avoid a naming conflict",[28,477,479],{"id":478},"simple-example-with-a-standard-library-module","Simple example with a standard library module",[14,481,482,483,485],{},"A good first example is the ",[17,484,67],{}," module.",[33,487,489],{"className":35,"code":488,"language":37,"meta":38,"style":38},"import math\n\nnumber = 16\nresult = math.sqrt(number)\n\nprint(result)\n",[17,490,491,497,501,513,535,539],{"__ignoreMap":38},[42,492,493,495],{"class":44,"line":45},[42,494,19],{"class":48},[42,496,52],{"class":51},[42,498,499],{"class":44,"line":55},[42,500,89],{"emptyLinePlaceholder":88},[42,502,503,506,510],{"class":44,"line":85},[42,504,505],{"class":51},"number ",[42,507,509],{"class":508},"smGrS","=",[42,511,512],{"class":78}," 16\n",[42,514,515,518,520,523,525,527,529,532],{"class":44,"line":92},[42,516,517],{"class":51},"result ",[42,519,509],{"class":508},[42,521,522],{"class":51}," math",[42,524,70],{"class":62},[42,526,73],{"class":66},[42,528,63],{"class":62},[42,530,531],{"class":66},"number",[42,533,534],{"class":62},")\n",[42,536,537],{"class":44,"line":106},[42,538,89],{"emptyLinePlaceholder":88},[42,540,542,544,546,549],{"class":44,"line":541},6,[42,543,59],{"class":58},[42,545,63],{"class":62},[42,547,548],{"class":66},"result",[42,550,534],{"class":62},[14,552,122],{},[33,554,555],{"className":35,"code":132,"language":37,"meta":38,"style":38},[17,556,557],{"__ignoreMap":38},[42,558,559],{"class":44,"line":45},[42,560,132],{"class":78},[14,562,563],{},"Here:",[145,565,566,571,577],{},[148,567,568,570],{},[17,569,67],{}," is the module",[148,572,573,576],{},[17,574,575],{},"sqrt()"," is a function inside that module",[148,578,579,580,583,584],{},"the dot in ",[17,581,582],{},"math.sqrt"," is called ",[185,585,586],{},"dot notation",[14,588,589],{},"Dot notation helps show exactly where a function comes from. That makes code easier to understand.",[14,591,592,593,596,597,600,601,603],{},"For example, ",[17,594,595],{},"math.sqrt(9)"," is clearer than just ",[17,598,599],{},"sqrt(9)"," if the reader does not already know where ",[17,602,73],{}," came from.",[28,605,607],{"id":606},"importing-your-own-python-file","Importing your own Python file",[14,609,610],{},"You can also import your own files.",[14,612,613],{},"Suppose you have this file structure:",[33,615,617],{"className":35,"code":616,"language":37,"meta":38,"style":38},"helpers.py\nmain.py\n",[17,618,619,630],{"__ignoreMap":38},[42,620,621,624,626],{"class":44,"line":45},[42,622,623],{"class":51},"helpers",[42,625,70],{"class":62},[42,627,629],{"class":628},"skxfh","py\n",[42,631,632,635,637],{"class":44,"line":55},[42,633,634],{"class":51},"main",[42,636,70],{"class":62},[42,638,629],{"class":628},[246,640,642],{"id":641},"helperspy",[17,643,644],{},"helpers.py",[33,646,648],{"className":35,"code":647,"language":37,"meta":38,"style":38},"def greet(name):\n    return f\"Hello, {name}!\"\n",[17,649,650,669],{"__ignoreMap":38},[42,651,652,656,660,662,666],{"class":44,"line":45},[42,653,655],{"class":654},"sbsja","def",[42,657,659],{"class":658},"sGLFI"," greet",[42,661,63],{"class":62},[42,663,665],{"class":664},"sFwrP","name",[42,667,668],{"class":62},"):\n",[42,670,671,674,677,681,684,686,689],{"class":44,"line":55},[42,672,673],{"class":48},"    return",[42,675,676],{"class":654}," f",[42,678,680],{"class":679},"s_sjI","\"Hello, ",[42,682,683],{"class":78},"{",[42,685,665],{"class":51},[42,687,688],{"class":78},"}",[42,690,691],{"class":679},"!\"\n",[246,693,695],{"id":694},"mainpy",[17,696,697],{},"main.py",[33,699,701],{"className":35,"code":700,"language":37,"meta":38,"style":38},"import helpers\n\nmessage = helpers.greet(\"Sam\")\nprint(message)\n",[17,702,703,710,714,742],{"__ignoreMap":38},[42,704,705,707],{"class":44,"line":45},[42,706,19],{"class":48},[42,708,709],{"class":51}," helpers\n",[42,711,712],{"class":44,"line":55},[42,713,89],{"emptyLinePlaceholder":88},[42,715,716,719,721,724,726,729,731,735,738,740],{"class":44,"line":85},[42,717,718],{"class":51},"message ",[42,720,509],{"class":508},[42,722,723],{"class":51}," helpers",[42,725,70],{"class":62},[42,727,728],{"class":66},"greet",[42,730,63],{"class":62},[42,732,734],{"class":733},"sjJ54","\"",[42,736,737],{"class":679},"Sam",[42,739,734],{"class":733},[42,741,534],{"class":62},[42,743,744,746,748,751],{"class":44,"line":92},[42,745,59],{"class":58},[42,747,63],{"class":62},[42,749,750],{"class":66},"message",[42,752,534],{"class":62},[14,754,122],{},[33,756,758],{"className":35,"code":757,"language":37,"meta":38,"style":38},"Hello, Sam!\n",[17,759,760],{"__ignoreMap":38},[42,761,762,765,768],{"class":44,"line":45},[42,763,764],{"class":51},"Hello",[42,766,767],{"class":62},",",[42,769,770],{"class":51}," Sam!\n",[14,772,773],{},"This works because:",[145,775,776,783],{},[148,777,778,780,781],{},[17,779,644],{}," is in the same folder as ",[17,782,697],{},[148,784,785,786],{},"the module name is the file name without ",[17,787,191],{},[14,789,790],{},"So:",[145,792,793,800],{},[148,794,795,797,798],{},[17,796,644],{}," becomes ",[17,799,623],{},[148,801,802,797,805],{},[17,803,804],{},"tools.py",[17,806,807],{},"tools",[14,809,810],{},"Use simple file names:",[145,812,813,816,819,822],{},[148,814,815],{},"lowercase is best",[148,817,818],{},"avoid spaces",[148,820,821],{},"avoid special characters",[148,823,824],{},"do not start the file name with a number",[14,826,827,828,70],{},"If you want more step-by-step examples, see ",[234,829,831],{"href":830},"\u002Fhow-to\u002Fhow-to-import-a-module-in-python","how to import a module in Python",[28,833,835],{"id":834},"how-python-finds-modules","How Python finds modules",[14,837,838],{},"When you write an import statement, Python has to find that module.",[14,840,841],{},"At a beginner level, the main idea is simple. Python checks places such as:",[145,843,844,847,850,853],{},[148,845,846],{},"built-in modules",[148,848,849],{},"installed packages",[148,851,852],{},"folders in the module search path",[148,854,855],{},"your current project folder",[14,857,858,859,70],{},"That search path is stored in ",[17,860,861],{},"sys.path",[14,863,864],{},"You do not need to memorize the internals yet, but it helps to know that Python only imports modules it can find in one of those locations.",[14,866,867],{},"You can inspect the search path with this command:",[33,869,873],{"className":870,"code":871,"language":872,"meta":38,"style":38},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python -c \"import sys; print(sys.path)\"\n","bash",[17,874,875],{"__ignoreMap":38},[42,876,877,880,884,887,890],{"class":44,"line":45},[42,878,37],{"class":879},"sbgvK",[42,881,883],{"class":882},"stzsN"," -c",[42,885,886],{"class":733}," \"",[42,888,889],{"class":679},"import sys; print(sys.path)",[42,891,892],{"class":733},"\"\n",[14,894,895,896,899,900,70],{},"If a module cannot be found, you will often get a ",[17,897,898],{},"ModuleNotFoundError",". For help with that, see ",[234,901,903],{"href":902},"\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix","ModuleNotFoundError: No module named X",[14,905,906,907,911,912,70],{},"If you want to learn more about the search path itself, see ",[234,908,910],{"href":909},"\u002Fstandard-library\u002Fsys.path-explained","sys.path explained"," and the ",[234,913,915],{"href":914},"\u002Fstandard-library\u002Fpython-sys-module-overview","Python sys module overview",[28,917,919],{"id":918},"what-happens-when-a-module-is-imported","What happens when a module is imported",[14,921,922],{},"Importing a module does more than just “copy code.”",[14,924,925],{},"When Python imports a module:",[145,927,928,931,934],{},[148,929,930],{},"it runs the module’s top-level code",[148,932,933],{},"it defines the functions and classes in that file",[148,935,936],{},"it stores the loaded module so it can be reused",[14,938,939],{},"Example:",[246,941,943],{"id":942},"example_modulepy",[17,944,945],{},"example_module.py",[33,947,949],{"className":35,"code":948,"language":37,"meta":38,"style":38},"print(\"example_module is running\")\n\ndef say_hello():\n    print(\"Hello\")\n",[17,950,951,966,970,980],{"__ignoreMap":38},[42,952,953,955,957,959,962,964],{"class":44,"line":45},[42,954,59],{"class":58},[42,956,63],{"class":62},[42,958,734],{"class":733},[42,960,961],{"class":679},"example_module is running",[42,963,734],{"class":733},[42,965,534],{"class":62},[42,967,968],{"class":44,"line":55},[42,969,89],{"emptyLinePlaceholder":88},[42,971,972,974,977],{"class":44,"line":85},[42,973,655],{"class":654},[42,975,976],{"class":658}," say_hello",[42,978,979],{"class":62},"():\n",[42,981,982,985,987,989,991,993],{"class":44,"line":92},[42,983,984],{"class":58},"    print",[42,986,63],{"class":62},[42,988,734],{"class":733},[42,990,764],{"class":679},[42,992,734],{"class":733},[42,994,534],{"class":62},[246,996,998],{"id":997},"mainpy-1",[17,999,697],{},[33,1001,1003],{"className":35,"code":1002,"language":37,"meta":38,"style":38},"import example_module\n\nexample_module.say_hello()\n",[17,1004,1005,1012,1016],{"__ignoreMap":38},[42,1006,1007,1009],{"class":44,"line":45},[42,1008,19],{"class":48},[42,1010,1011],{"class":51}," example_module\n",[42,1013,1014],{"class":44,"line":55},[42,1015,89],{"emptyLinePlaceholder":88},[42,1017,1018,1021,1023,1026],{"class":44,"line":85},[42,1019,1020],{"class":51},"example_module",[42,1022,70],{"class":62},[42,1024,1025],{"class":66},"say_hello",[42,1027,1028],{"class":62},"()\n",[14,1030,122],{},[33,1032,1034],{"className":35,"code":1033,"language":37,"meta":38,"style":38},"example_module is running\nHello\n",[17,1035,1036,1047],{"__ignoreMap":38},[42,1037,1038,1041,1044],{"class":44,"line":45},[42,1039,1040],{"class":51},"example_module ",[42,1042,1043],{"class":508},"is",[42,1045,1046],{"class":51}," running\n",[42,1048,1049],{"class":44,"line":55},[42,1050,1051],{"class":51},"Hello\n",[14,1053,1054],{},"Notice what happened:",[145,1056,1057,1064],{},[148,1058,1059,1060,1063],{},"the ",[17,1061,1062],{},"print()"," at the top level ran during import",[148,1065,1066],{},"the function became available after the import",[14,1068,1069],{},"This can surprise beginners. If a file has code at the top level, that code runs when the file is imported.",[14,1071,1072,1073,1076],{},"Also, repeated imports in the same program usually do ",[185,1074,1075],{},"not"," rerun the whole file, because Python caches imported modules.",[28,1078,1080,1081,1084],{"id":1079},"why-__name__-__main__-matters","Why ",[17,1082,1083],{},"__name__ == '__main__'"," matters",[14,1086,1087],{},"This pattern helps prevent test code or demo code from running during import.",[33,1089,1091],{"className":35,"code":1090,"language":37,"meta":38,"style":38},"def greet(name):\n    return f\"Hello, {name}!\"\n\nif __name__ == \"__main__\":\n    print(greet(\"Sam\"))\n",[17,1092,1093,1105,1121,1125,1147],{"__ignoreMap":38},[42,1094,1095,1097,1099,1101,1103],{"class":44,"line":45},[42,1096,655],{"class":654},[42,1098,659],{"class":658},[42,1100,63],{"class":62},[42,1102,665],{"class":664},[42,1104,668],{"class":62},[42,1106,1107,1109,1111,1113,1115,1117,1119],{"class":44,"line":55},[42,1108,673],{"class":48},[42,1110,676],{"class":654},[42,1112,680],{"class":679},[42,1114,683],{"class":78},[42,1116,665],{"class":51},[42,1118,688],{"class":78},[42,1120,691],{"class":679},[42,1122,1123],{"class":44,"line":85},[42,1124,89],{"emptyLinePlaceholder":88},[42,1126,1127,1130,1134,1137,1139,1142,1144],{"class":44,"line":92},[42,1128,1129],{"class":48},"if",[42,1131,1133],{"class":1132},"s_hVV"," __name__",[42,1135,1136],{"class":508}," ==",[42,1138,886],{"class":733},[42,1140,1141],{"class":679},"__main__",[42,1143,734],{"class":733},[42,1145,1146],{"class":62},":\n",[42,1148,1149,1151,1153,1155,1157,1159,1161,1163],{"class":44,"line":106},[42,1150,984],{"class":58},[42,1152,63],{"class":62},[42,1154,728],{"class":66},[42,1156,63],{"class":62},[42,1158,734],{"class":733},[42,1160,737],{"class":679},[42,1162,734],{"class":733},[42,1164,82],{"class":62},[14,1166,1167],{},"If you run the file directly:",[33,1169,1171],{"className":870,"code":1170,"language":872,"meta":38,"style":38},"python helpers.py\n",[17,1172,1173],{"__ignoreMap":38},[42,1174,1175,1177],{"class":44,"line":45},[42,1176,37],{"class":879},[42,1178,1179],{"class":679}," helpers.py\n",[14,1181,1182],{},"then the code inside this block runs.",[14,1184,1185,1186,1188,1189,1191],{},"But if another file imports ",[17,1187,623],{},", this block does ",[185,1190,1075],{}," run.",[14,1193,1194],{},"That makes it useful for:",[145,1196,1197,1200,1203],{},[148,1198,1199],{},"quick tests",[148,1201,1202],{},"example usage",[148,1204,1205],{},"demo output inside a module",[14,1207,1208],{},"Without this pattern, top-level test code can run every time the module is imported.",[28,1210,1212],{"id":1211},"common-beginner-import-mistakes","Common beginner import mistakes",[14,1214,1215],{},"Here are some of the most common problems beginners run into.",[246,1217,1219],{"id":1218},"naming-your-file-like-a-standard-library-module","Naming your file like a standard library module",[14,1221,1222,1223,207,1226,1229,1230,1233],{},"If you create a file called ",[17,1224,1225],{},"random.py",[17,1227,1228],{},"json.py",", or ",[17,1231,1232],{},"sys.py",", it can conflict with Python’s real module of the same name.",[14,1235,1236],{},"Example problem:",[33,1238,1240],{"className":35,"code":1239,"language":37,"meta":38,"style":38},"# random.py\nimport random\nprint(random.randint(1, 10))\n",[17,1241,1242,1248,1255],{"__ignoreMap":38},[42,1243,1244],{"class":44,"line":45},[42,1245,1247],{"class":1246},"sutJx","# random.py\n",[42,1249,1250,1252],{"class":44,"line":55},[42,1251,19],{"class":48},[42,1253,1254],{"class":51}," random\n",[42,1256,1257,1259,1261,1263,1265,1268,1270,1273,1275,1278],{"class":44,"line":85},[42,1258,59],{"class":58},[42,1260,63],{"class":62},[42,1262,210],{"class":66},[42,1264,70],{"class":62},[42,1266,1267],{"class":66},"randint",[42,1269,63],{"class":62},[42,1271,1272],{"class":78},"1",[42,1274,767],{"class":62},[42,1276,1277],{"class":78}," 10",[42,1279,82],{"class":62},[14,1281,1282],{},"This can break because your file may be imported instead of the standard library module.",[14,1284,1285],{},"Use unique file names for your own scripts.",[246,1287,1289],{"id":1288},"importing-from-the-wrong-folder","Importing from the wrong folder",[14,1291,1292],{},"If your file is not in the expected location, Python may not find it.",[14,1294,1295],{},"Common causes:",[145,1297,1298,1301,1304],{},[148,1299,1300],{},"the file is in a different folder",[148,1302,1303],{},"you are running the script from the wrong directory",[148,1305,1306],{},"your project structure is not what you think it is",[246,1308,1310,1311],{"id":1309},"using-from-module-import","Using ",[17,1312,1313],{},"from module import *",[14,1315,1316],{},"Wildcard imports make code harder to read.",[33,1318,1320],{"className":35,"code":1319,"language":37,"meta":38,"style":38},"from math import *\n",[17,1321,1322],{"__ignoreMap":38},[42,1323,1324,1326,1328,1330],{"class":44,"line":45},[42,1325,95],{"class":48},[42,1327,98],{"class":51},[42,1329,19],{"class":48},[42,1331,1332],{"class":508}," *\n",[14,1334,1335],{},"This imports many names without showing where they came from.",[14,1337,1338],{},"That can cause confusion because:",[145,1340,1341,1344,1347],{},[148,1342,1343],{},"readers cannot easily tell where a name came from",[148,1345,1346],{},"names can overwrite each other",[148,1348,1349],{},"debugging becomes harder",[246,1351,1353],{"id":1352},"forgetting-to-install-a-package","Forgetting to install a package",[14,1355,1356],{},"Some modules are built in, but many are not.",[14,1358,1359],{},"For example, this may fail if the package is not installed:",[33,1361,1363],{"className":35,"code":1362,"language":37,"meta":38,"style":38},"import requests\n",[17,1364,1365],{"__ignoreMap":38},[42,1366,1367,1369],{"class":44,"line":45},[42,1368,19],{"class":48},[42,1370,1371],{"class":51}," requests\n",[14,1373,1374,1375,1378,1379,70],{},"If needed, install it with ",[17,1376,1377],{},"pip",". See ",[234,1380,1382],{"href":1381},"\u002Fhow-to\u002Fhow-to-install-a-python-package-with-pip","how to install a Python package with pip",[14,1384,1385,1386,70],{},"If you use virtual environments, also see ",[234,1387,1389],{"href":1388},"\u002Fhow-to\u002Fhow-to-create-and-use-a-virtual-environment-in-python","how to create and use a virtual environment in Python",[246,1391,1393],{"id":1392},"expecting-import-to-copy-code","Expecting import to copy code",[14,1395,1396,1398],{},[17,1397,19],{}," does not paste code into your file.",[14,1400,1401,1402,1405],{},"Instead, Python loads the module and lets you access its contents. That is why ",[17,1403,1404],{},"math.sqrt()"," still uses the module name, and why top-level code in the module can run during import.",[28,1407,1409],{"id":1408},"best-practices-for-beginners","Best practices for beginners",[14,1411,1412],{},"These habits will help you avoid many import problems.",[145,1414,1415,1422,1428,1433,1436],{},[148,1416,1417,1418,1421],{},"Prefer ",[17,1419,1420],{},"import module"," when possible",[148,1423,140,1424,1427],{},[17,1425,1426],{},"from module import name"," when you only need one or two names",[148,1429,1430,1431],{},"Avoid ",[17,1432,1313],{},[148,1434,1435],{},"Keep module file names simple and lowercase",[148,1437,1438,1439],{},"Put test or demo code inside ",[17,1440,1441],{},"if __name__ == \"__main__\":",[14,1443,1444],{},"A good beginner-friendly style looks like this:",[33,1446,1448],{"className":35,"code":1447,"language":37,"meta":38,"style":38},"import math\n\narea = math.pi * 5 ** 2\nprint(area)\n",[17,1449,1450,1456,1460,1486],{"__ignoreMap":38},[42,1451,1452,1454],{"class":44,"line":45},[42,1453,19],{"class":48},[42,1455,52],{"class":51},[42,1457,1458],{"class":44,"line":55},[42,1459,89],{"emptyLinePlaceholder":88},[42,1461,1462,1465,1467,1469,1471,1474,1477,1480,1483],{"class":44,"line":85},[42,1463,1464],{"class":51},"area ",[42,1466,509],{"class":508},[42,1468,522],{"class":51},[42,1470,70],{"class":62},[42,1472,1473],{"class":628},"pi",[42,1475,1476],{"class":508}," *",[42,1478,1479],{"class":78}," 5",[42,1481,1482],{"class":508}," **",[42,1484,1485],{"class":78}," 2\n",[42,1487,1488,1490,1492,1495],{"class":44,"line":92},[42,1489,59],{"class":58},[42,1491,63],{"class":62},[42,1493,1494],{"class":66},"area",[42,1496,534],{"class":62},[14,1498,1499,1500,1502,1503,70],{},"This is easy to read because it is clear that ",[17,1501,1473],{}," came from ",[17,1504,67],{},[28,1506,1508],{"id":1507},"common-causes-of-import-problems","Common causes of import problems",[14,1510,1511],{},"If your import is not working, check these first:",[145,1513,1514,1517,1520,1523,1526],{},[148,1515,1516],{},"the module file is not in the expected folder",[148,1518,1519],{},"your file name conflicts with a standard library module or installed package",[148,1521,1522],{},"the package is not installed",[148,1524,1525],{},"the spelling or capitalization is wrong in the import statement",[148,1527,1528],{},"top-level code in the imported file runs and causes unexpected behavior",[14,1530,1531],{},"Useful commands:",[33,1533,1535],{"className":870,"code":1534,"language":872,"meta":38,"style":38},"python --version\npython your_script.py\npython -c \"import sys; print(sys.path)\"\npython -c \"import math; print(math.__file__ if hasattr(math, '__file__') else 'built-in module')\"\npip show requests\n",[17,1536,1537,1544,1551,1563,1576],{"__ignoreMap":38},[42,1538,1539,1541],{"class":44,"line":45},[42,1540,37],{"class":879},[42,1542,1543],{"class":882}," --version\n",[42,1545,1546,1548],{"class":44,"line":55},[42,1547,37],{"class":879},[42,1549,1550],{"class":679}," your_script.py\n",[42,1552,1553,1555,1557,1559,1561],{"class":44,"line":85},[42,1554,37],{"class":879},[42,1556,883],{"class":882},[42,1558,886],{"class":733},[42,1560,889],{"class":679},[42,1562,892],{"class":733},[42,1564,1565,1567,1569,1571,1574],{"class":44,"line":92},[42,1566,37],{"class":879},[42,1568,883],{"class":882},[42,1570,886],{"class":733},[42,1572,1573],{"class":679},"import math; print(math.__file__ if hasattr(math, '__file__') else 'built-in module')",[42,1575,892],{"class":733},[42,1577,1578,1580,1583],{"class":44,"line":106},[42,1579,1377],{"class":879},[42,1581,1582],{"class":679}," show",[42,1584,1371],{"class":679},[14,1586,1587],{},"These help you check:",[145,1589,1590,1593,1596,1599],{},[148,1591,1592],{},"which Python version you are using",[148,1594,1595],{},"whether the script runs in the environment you expect",[148,1597,1598],{},"where Python is looking for modules",[148,1600,1601],{},"whether a package is installed",[28,1603,1605],{"id":1604},"faq","FAQ",[246,1607,1609,1610,1612,1613,1615],{"id":1608},"what-is-the-difference-between-import-math-and-from-math-import-sqrt","What is the difference between ",[17,1611,152],{}," and ",[17,1614,161],{},"?",[14,1617,1618,153,1620,70],{},[17,1619,152],{},[17,1621,1404],{},[14,1623,1624,1626,1627,70],{},[17,1625,161],{}," imports one name directly, so you write ",[17,1628,575],{},[246,1630,1632,1633,1615],{"id":1631},"why-does-python-say-modulenotfounderror","Why does Python say ",[17,1634,898],{},[14,1636,1637],{},"Usually Python cannot find the module file or package, or the package is not installed in the current environment.",[14,1639,1640,1641,1645],{},"See also ",[234,1642,1644],{"href":1643},"\u002Ferrors\u002Fimporterror-no-module-named-x-fix","ImportError: No module named X"," if you are troubleshooting an import problem.",[246,1647,1649],{"id":1648},"does-python-run-an-imported-file-every-time","Does Python run an imported file every time?",[14,1651,1652],{},"Normally no.",[14,1654,1655],{},"Python caches imported modules, so repeated imports in the same run usually reuse the loaded module.",[246,1657,1659],{"id":1658},"can-i-import-my-own-python-file","Can I import my own Python file?",[14,1661,1662],{},"Yes.",[14,1664,1665],{},"If the file is in the right folder and has a valid name, Python can import it as a module.",[246,1667,1669,1670,1615],{"id":1668},"should-beginners-use-from-module-import","Should beginners use ",[17,1671,1313],{},[14,1673,1674],{},"Usually no.",[14,1676,1677],{},"It makes code harder to read because it hides where names came from.",[28,1679,1681],{"id":1680},"see-also","See also",[145,1683,1684,1688,1693,1698,1703,1707,1711,1715],{},[148,1685,1686],{},[234,1687,237],{"href":236},[148,1689,1690],{},[234,1691,1692],{"href":830},"How to import a module in Python",[148,1694,1695],{},[234,1696,1697],{"href":1381},"How to install a Python package with pip",[148,1699,1700],{},[234,1701,1702],{"href":1388},"How to create and use a virtual environment in Python",[148,1704,1705],{},[234,1706,903],{"href":902},[148,1708,1709],{},[234,1710,1644],{"href":1643},[148,1712,1713],{},[234,1714,915],{"href":914},[148,1716,1717],{},[234,1718,910],{"href":909},[1720,1721,1722],"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 .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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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 .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}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}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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}",{"title":38,"searchDepth":55,"depth":55,"links":1724},[1725,1726,1727,1733,1734,1738,1739,1743,1745,1753,1754,1755,1765],{"id":30,"depth":55,"text":31},{"id":168,"depth":55,"text":169},{"id":240,"depth":55,"text":241,"children":1728},[1729,1730,1731,1732],{"id":248,"depth":85,"text":251},{"id":305,"depth":85,"text":308},{"id":361,"depth":85,"text":364},{"id":421,"depth":85,"text":424},{"id":478,"depth":55,"text":479},{"id":606,"depth":55,"text":607,"children":1735},[1736,1737],{"id":641,"depth":85,"text":644},{"id":694,"depth":85,"text":697},{"id":834,"depth":55,"text":835},{"id":918,"depth":55,"text":919,"children":1740},[1741,1742],{"id":942,"depth":85,"text":945},{"id":997,"depth":85,"text":697},{"id":1079,"depth":55,"text":1744},"Why __name__ == '__main__' matters",{"id":1211,"depth":55,"text":1212,"children":1746},[1747,1748,1749,1751,1752],{"id":1218,"depth":85,"text":1219},{"id":1288,"depth":85,"text":1289},{"id":1309,"depth":85,"text":1750},"Using from module import *",{"id":1352,"depth":85,"text":1353},{"id":1392,"depth":85,"text":1393},{"id":1408,"depth":55,"text":1409},{"id":1507,"depth":55,"text":1508},{"id":1604,"depth":55,"text":1605,"children":1756},[1757,1759,1761,1762,1763],{"id":1608,"depth":85,"text":1758},"What is the difference between import math and from math import sqrt?",{"id":1631,"depth":85,"text":1760},"Why does Python say ModuleNotFoundError?",{"id":1648,"depth":85,"text":1649},{"id":1658,"depth":85,"text":1659},{"id":1668,"depth":85,"text":1764},"Should beginners use from module import *?",{"id":1680,"depth":55,"text":1681},"Master how import works in python in our comprehensive Python beginner guide.","md",{},"\u002Flearn\u002Fhow-import-works-in-python",{"title":5,"description":1766},"learn\u002Fhow-import-works-in-python","Dy6beggA2Q2Qrjafc_QobeW3VlxOwC-tjH-KbCkuovA",1777585493058]