[{"data":1,"prerenderedAt":1618},["ShallowReactive",2],{"doc-\u002Ferrors\u002Fimporterror-in-python-causes-and-fixes":3},{"id":4,"title":5,"body":6,"description":1611,"extension":1612,"meta":1613,"navigation":146,"path":1614,"seo":1615,"stem":1616,"__hash__":1617},"content\u002Ferrors\u002Fimporterror-in-python-causes-and-fixes.md","ImportError in Python: Causes and Fixes",{"type":7,"value":8,"toc":1575},"minimark",[9,13,25,28,44,54,69,72,77,80,187,190,216,219,233,237,242,245,256,259,277,285,289,295,315,318,322,325,343,353,358,392,395,404,407,415,418,425,429,432,435,449,452,468,471,486,495,498,528,534,545,548,582,585,593,597,600,603,614,617,657,660,679,688,691,694,701,705,708,715,727,730,733,754,757,769,772,786,789,793,796,799,807,814,831,834,837,851,854,865,872,883,887,890,893,900,965,971,1010,1013,1016,1027,1034,1038,1044,1048,1051,1054,1065,1069,1072,1080,1087,1093,1096,1115,1122,1126,1129,1144,1147,1151,1154,1200,1203,1230,1233,1237,1240,1245,1248,1251,1263,1268,1273,1276,1278,1292,1296,1302,1305,1307,1336,1341,1345,1351,1374,1378,1381,1437,1440,1460,1464,1468,1473,1478,1482,1485,1489,1492,1496,1503,1505,1530,1534,1571],[10,11,5],"h1",{"id":12},"importerror-in-python-causes-and-fixes",[14,15,16,20,21,24],"p",{},[17,18,19],"code",{},"ImportError"," means Python could not complete an ",[17,22,23],{},"import"," statement.",[14,26,27],{},"This usually happens because:",[29,30,31,35,38,41],"ul",{},[32,33,34],"li",{},"the module is missing",[32,36,37],{},"the name you are trying to import does not exist",[32,39,40],{},"Python is using the wrong environment",[32,42,43],{},"your project files are named or arranged in a way that breaks imports",[14,45,46,47,49,50,53],{},"Beginners often mix up ",[17,48,19],{}," and ",[17,51,52],{},"ModuleNotFoundError",".",[29,55,56,61,66],{},[32,57,58,60],{},[17,59,52],{}," is more specific",[32,62,63,65],{},[17,64,19],{}," is broader",[32,67,68],{},"Python may find the module, but still fail to import something from it",[14,70,71],{},"This page helps you check the most common causes step by step.",[73,74,76],"h2",{"id":75},"quick-fix-checklist","Quick fix checklist",[14,78,79],{},"First confirm Python is running correctly, then check whether the module exists, the name is spelled correctly, and the import path is valid.",[81,82,87],"pre",{"className":83,"code":84,"language":85,"meta":86,"style":86},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import sys\nprint(sys.version)\nprint(sys.path)\n\n# Check the exact import you are using\nimport math\nprint(math.sqrt(9))\n","python","",[17,88,89,101,125,141,148,155,163],{"__ignoreMap":86},[90,91,94,97],"span",{"class":92,"line":93},"line",1,[90,95,23],{"class":96},"sVHd0",[90,98,100],{"class":99},"su5hD"," sys\n",[90,102,104,108,112,116,118,122],{"class":92,"line":103},2,[90,105,107],{"class":106},"sptTA","print",[90,109,111],{"class":110},"sP7_E","(",[90,113,115],{"class":114},"slqww","sys",[90,117,53],{"class":110},[90,119,121],{"class":120},"skxfh","version",[90,123,124],{"class":110},")\n",[90,126,128,130,132,134,136,139],{"class":92,"line":127},3,[90,129,107],{"class":106},[90,131,111],{"class":110},[90,133,115],{"class":114},[90,135,53],{"class":110},[90,137,138],{"class":120},"path",[90,140,124],{"class":110},[90,142,144],{"class":92,"line":143},4,[90,145,147],{"emptyLinePlaceholder":146},true,"\n",[90,149,151],{"class":92,"line":150},5,[90,152,154],{"class":153},"sutJx","# Check the exact import you are using\n",[90,156,158,160],{"class":92,"line":157},6,[90,159,23],{"class":96},[90,161,162],{"class":99}," math\n",[90,164,166,168,170,173,175,178,180,184],{"class":92,"line":165},7,[90,167,107],{"class":106},[90,169,111],{"class":110},[90,171,172],{"class":114},"math",[90,174,53],{"class":110},[90,176,177],{"class":114},"sqrt",[90,179,111],{"class":110},[90,181,183],{"class":182},"srdBf","9",[90,185,186],{"class":110},"))\n",[14,188,189],{},"What this does:",[29,191,192,198,204,210],{},[32,193,194,197],{},[17,195,196],{},"sys.version"," shows which Python version is running",[32,199,200,203],{},[17,201,202],{},"sys.path"," shows where Python looks for modules",[32,205,206,209],{},[17,207,208],{},"import math"," checks that a standard library import works",[32,211,212,215],{},[17,213,214],{},"math.sqrt(9)"," confirms the imported module is usable",[14,217,218],{},"If a simple import works but your real import fails, the problem is likely with:",[29,220,221,224,227,230],{},[32,222,223],{},"the module name",[32,225,226],{},"the imported name",[32,228,229],{},"the environment",[32,231,232],{},"your file or package structure",[73,234,236],{"id":235},"what-importerror-means","What ImportError means",[14,238,239,241],{},[17,240,19],{}," happens when Python cannot complete an import statement.",[14,243,244],{},"In simple terms:",[29,246,247,250,253],{},[32,248,249],{},"Python may fail to find a module",[32,251,252],{},"or Python may find the module but fail to load part of it",[32,254,255],{},"or Python may find the module but not the specific name you asked for",[14,257,258],{},"For example:",[29,260,261,267],{},[32,262,263,266],{},[17,264,265],{},"import some_module"," can fail if the module is not available",[32,268,269,272,273,276],{},[17,270,271],{},"from some_module import thing"," can fail if ",[17,274,275],{},"thing"," does not exist",[14,278,279,280,53],{},"If you want a broader explanation of imports, see ",[281,282,284],"a",{"href":283},"\u002Flearn\u002Fhow-import-works-in-python\u002F","how import works in Python",[73,286,288],{"id":287},"when-this-error-happens","When this error happens",[14,290,291,292,294],{},"You may see ",[17,293,19],{}," in situations like these:",[29,296,297,300,303,306,309,312],{},[32,298,299],{},"importing a module that is missing",[32,301,302],{},"importing a name that does not exist inside a module",[32,304,305],{},"using the wrong Python environment",[32,307,308],{},"running a file from the wrong folder",[32,310,311],{},"circular imports between your own files",[32,313,314],{},"naming your file the same as a standard library module",[14,316,317],{},"These are the most common beginner cases.",[73,319,321],{"id":320},"basic-example-that-causes-importerror","Basic example that causes ImportError",[14,323,324],{},"A common case is importing a name that is not available inside a module.",[81,326,328],{"className":83,"code":327,"language":85,"meta":86,"style":86},"from math import square_root\n",[17,329,330],{"__ignoreMap":86},[90,331,332,335,338,340],{"class":92,"line":93},[90,333,334],{"class":96},"from",[90,336,337],{"class":99}," math ",[90,339,23],{"class":96},[90,341,342],{"class":99}," square_root\n",[14,344,345,346,348,349,352],{},"This fails because the ",[17,347,172],{}," module exists, but ",[17,350,351],{},"square_root"," is not a valid name in that module.",[14,354,355,356,53],{},"The correct name is ",[17,357,177],{},[81,359,361],{"className":83,"code":360,"language":85,"meta":86,"style":86},"from math import sqrt\n\nprint(sqrt(9))\n",[17,362,363,374,378],{"__ignoreMap":86},[90,364,365,367,369,371],{"class":92,"line":93},[90,366,334],{"class":96},[90,368,337],{"class":99},[90,370,23],{"class":96},[90,372,373],{"class":99}," sqrt\n",[90,375,376],{"class":92,"line":103},[90,377,147],{"emptyLinePlaceholder":146},[90,379,380,382,384,386,388,390],{"class":92,"line":127},[90,381,107],{"class":106},[90,383,111],{"class":110},[90,385,177],{"class":114},[90,387,111],{"class":110},[90,389,183],{"class":182},[90,391,186],{"class":110},[14,393,394],{},"Output:",[81,396,398],{"className":83,"code":397,"language":85,"meta":86,"style":86},"3.0\n",[17,399,400],{"__ignoreMap":86},[90,401,402],{"class":92,"line":93},[90,403,397],{"class":182},[14,405,406],{},"This is an important pattern:",[29,408,409,412],{},[32,410,411],{},"Python can find the module",[32,413,414],{},"but Python cannot import the requested name",[14,416,417],{},"That is different from a missing module.",[14,419,420,421,53],{},"For a more specific version of this problem, see ",[281,422,424],{"href":423},"\u002Ferrors\u002Fimporterror-cannot-import-name-fix\u002F","ImportError: cannot import name",[73,426,428],{"id":427},"fix-1-check-the-module-and-name-carefully","Fix 1: Check the module and name carefully",[14,430,431],{},"Start with the simplest possibility: a typo.",[14,433,434],{},"Check these things:",[29,436,437,440,443,446],{},[32,438,439],{},"spelling",[32,441,442],{},"uppercase and lowercase letters",[32,444,445],{},"whether the function, class, or variable really exists",[32,447,448],{},"whether you are using the correct module",[14,450,451],{},"For example, this is wrong:",[81,453,455],{"className":83,"code":454,"language":85,"meta":86,"style":86},"from math import Sqrt\n",[17,456,457],{"__ignoreMap":86},[90,458,459,461,463,465],{"class":92,"line":93},[90,460,334],{"class":96},[90,462,337],{"class":99},[90,464,23],{"class":96},[90,466,467],{"class":99}," Sqrt\n",[14,469,470],{},"This is correct:",[81,472,474],{"className":83,"code":473,"language":85,"meta":86,"style":86},"from math import sqrt\n",[17,475,476],{"__ignoreMap":86},[90,477,478,480,482,484],{"class":92,"line":93},[90,479,334],{"class":96},[90,481,337],{"class":99},[90,483,23],{"class":96},[90,485,373],{"class":99},[14,487,488,489,49,492,494],{},"Python names are case-sensitive. ",[17,490,491],{},"Sqrt",[17,493,177],{}," are different names.",[14,496,497],{},"If you are not sure what a module contains, import the whole module first:",[81,499,501],{"className":83,"code":500,"language":85,"meta":86,"style":86},"import math\n\nprint(dir(math))\n",[17,502,503,509,513],{"__ignoreMap":86},[90,504,505,507],{"class":92,"line":93},[90,506,23],{"class":96},[90,508,162],{"class":99},[90,510,511],{"class":92,"line":103},[90,512,147],{"emptyLinePlaceholder":146},[90,514,515,517,519,522,524,526],{"class":92,"line":127},[90,516,107],{"class":106},[90,518,111],{"class":110},[90,520,521],{"class":106},"dir",[90,523,111],{"class":110},[90,525,172],{"class":114},[90,527,186],{"class":110},[14,529,530,533],{},[17,531,532],{},"dir()"," shows the available names inside the module.",[14,535,536,537,49,541,53],{},"You can also inspect help text with Python’s built-in tools. See ",[281,538,540],{"href":539},"\u002Freference\u002Fpython-dir-function-explained\u002F","Python dir() explained",[281,542,544],{"href":543},"\u002Freference\u002Fpython-help-function-explained\u002F","Python help() explained",[14,546,547],{},"A safe pattern is:",[81,549,551],{"className":83,"code":550,"language":85,"meta":86,"style":86},"import math\n\nprint(math.sqrt(16))\n",[17,552,553,559,563],{"__ignoreMap":86},[90,554,555,557],{"class":92,"line":93},[90,556,23],{"class":96},[90,558,162],{"class":99},[90,560,561],{"class":92,"line":103},[90,562,147],{"emptyLinePlaceholder":146},[90,564,565,567,569,571,573,575,577,580],{"class":92,"line":127},[90,566,107],{"class":106},[90,568,111],{"class":110},[90,570,172],{"class":114},[90,572,53],{"class":110},[90,574,177],{"class":114},[90,576,111],{"class":110},[90,578,579],{"class":182},"16",[90,581,186],{"class":110},[14,583,584],{},"This helps you confirm:",[29,586,587,590],{},[32,588,589],{},"the module imports correctly",[32,591,592],{},"the member name is valid",[73,594,596],{"id":595},"fix-2-check-your-environment-and-installed-packages","Fix 2: Check your environment and installed packages",[14,598,599],{},"Sometimes the package is installed, but not in the Python environment you are actually using.",[14,601,602],{},"This often happens when:",[29,604,605,608,611],{},[32,606,607],{},"your editor uses a different interpreter",[32,609,610],{},"your terminal uses a different Python installation",[32,612,613],{},"you forgot to activate a virtual environment",[14,615,616],{},"Useful checks:",[81,618,622],{"className":619,"code":620,"language":621,"meta":86,"style":86},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python --version\npython -m pip --version\npython -m pip list\n","bash",[17,623,624,633,646],{"__ignoreMap":86},[90,625,626,629],{"class":92,"line":93},[90,627,85],{"class":628},"sbgvK",[90,630,632],{"class":631},"stzsN"," --version\n",[90,634,635,637,640,644],{"class":92,"line":103},[90,636,85],{"class":628},[90,638,639],{"class":631}," -m",[90,641,643],{"class":642},"s_sjI"," pip",[90,645,632],{"class":631},[90,647,648,650,652,654],{"class":92,"line":127},[90,649,85],{"class":628},[90,651,639],{"class":631},[90,653,643],{"class":642},[90,655,656],{"class":642}," list\n",[14,658,659],{},"A good habit is to install packages like this:",[81,661,663],{"className":619,"code":662,"language":621,"meta":86,"style":86},"python -m pip install package_name\n",[17,664,665],{"__ignoreMap":86},[90,666,667,669,671,673,676],{"class":92,"line":93},[90,668,85],{"class":628},[90,670,639],{"class":631},[90,672,643],{"class":642},[90,674,675],{"class":642}," install",[90,677,678],{"class":642}," package_name\n",[14,680,681,682,685,686,53],{},"This makes ",[17,683,684],{},"pip"," run with the same Python interpreter as ",[17,687,85],{},[14,689,690],{},"If you are using a virtual environment, activate it before running your script.",[14,692,693],{},"If imports work in one editor but not another, they may be using different interpreters.",[14,695,696,697,53],{},"If you need help with setup, see ",[281,698,700],{"href":699},"\u002Fhow-to\u002Fhow-to-create-and-use-a-virtual-environment-in-python\u002F","how to create and use a virtual environment in Python",[73,702,704],{"id":703},"fix-3-check-your-file-and-folder-names","Fix 3: Check your file and folder names",[14,706,707],{},"A local file can shadow a real module.",[14,709,710,711,714],{},"For example, if your file is named ",[17,712,713],{},"random.py",", this can break code like:",[81,716,718],{"className":83,"code":717,"language":85,"meta":86,"style":86},"import random\n",[17,719,720],{"__ignoreMap":86},[90,721,722,724],{"class":92,"line":93},[90,723,23],{"class":96},[90,725,726],{"class":99}," random\n",[14,728,729],{},"Python may import your local file instead of the standard library module.",[14,731,732],{},"Problem filenames include:",[29,734,735,739,744,749],{},[32,736,737],{},[17,738,713],{},[32,740,741],{},[17,742,743],{},"json.py",[32,745,746],{},[17,747,748],{},"os.py",[32,750,751],{},[17,752,753],{},"sys.py",[14,755,756],{},"Rename the file to something else, such as:",[29,758,759,764],{},[32,760,761],{},[17,762,763],{},"my_random_script.py",[32,765,766],{},[17,767,768],{},"json_example.py",[14,770,771],{},"After renaming, you may also need to remove cached files:",[29,773,774,779],{},[32,775,776],{},[17,777,778],{},"__pycache__\u002F",[32,780,781,782,785],{},"old ",[17,783,784],{},".pyc"," files",[14,787,788],{},"This is a very common beginner mistake.",[73,790,792],{"id":791},"fix-4-check-package-structure-and-relative-imports","Fix 4: Check package structure and relative imports",[14,794,795],{},"Imports can fail if your files are run from the wrong location.",[14,797,798],{},"Imagine a project like this:",[81,800,805],{"className":801,"code":803,"language":804,"meta":86},[802],"language-text","project\u002F\n    app\u002F\n        __init__.py\n        main.py\n        utils.py\n","text",[17,806,803],{"__ignoreMap":86},[14,808,809,810,813],{},"If ",[17,811,812],{},"main.py"," contains:",[81,815,817],{"className":83,"code":816,"language":85,"meta":86,"style":86},"from app import utils\n",[17,818,819],{"__ignoreMap":86},[90,820,821,823,826,828],{"class":92,"line":93},[90,822,334],{"class":96},[90,824,825],{"class":99}," app ",[90,827,23],{"class":96},[90,829,830],{"class":99}," utils\n",[14,832,833],{},"running the file from the correct place matters.",[14,835,836],{},"Often this works better from the project root:",[81,838,840],{"className":619,"code":839,"language":621,"meta":86,"style":86},"python -m app.main\n",[17,841,842],{"__ignoreMap":86},[90,843,844,846,848],{"class":92,"line":93},[90,845,85],{"class":628},[90,847,639],{"class":631},[90,849,850],{"class":642}," app.main\n",[14,852,853],{},"Why this helps:",[29,855,856,859,862],{},[32,857,858],{},"Python treats the code as part of a package",[32,860,861],{},"import paths are resolved more consistently",[32,863,864],{},"relative import problems are reduced",[14,866,867,868,871],{},"In some setups, an ",[17,869,870],{},"__init__.py"," file is also needed to mark a folder as a package.",[14,873,874,875,49,879,53],{},"If you are learning the basics, see ",[281,876,878],{"href":877},"\u002Fhow-to\u002Fhow-to-import-a-module-in-python\u002F","how to import a module in Python",[281,880,882],{"href":881},"\u002Fstandard-library\u002Fsys.path-explained","sys.path explained",[73,884,886],{"id":885},"fix-5-look-for-circular-imports","Fix 5: Look for circular imports",[14,888,889],{},"A circular import happens when two files import each other.",[14,891,892],{},"Example:",[894,895,897],"h3",{"id":896},"apy",[17,898,899],{},"a.py",[81,901,903],{"className":83,"code":902,"language":85,"meta":86,"style":86},"from b import hello_from_b\n\ndef hello_from_a():\n    return \"Hello from A\"\n\nprint(hello_from_b())\n",[17,904,905,917,921,934,949,953],{"__ignoreMap":86},[90,906,907,909,912,914],{"class":92,"line":93},[90,908,334],{"class":96},[90,910,911],{"class":99}," b ",[90,913,23],{"class":96},[90,915,916],{"class":99}," hello_from_b\n",[90,918,919],{"class":92,"line":103},[90,920,147],{"emptyLinePlaceholder":146},[90,922,923,927,931],{"class":92,"line":127},[90,924,926],{"class":925},"sbsja","def",[90,928,930],{"class":929},"sGLFI"," hello_from_a",[90,932,933],{"class":110},"():\n",[90,935,936,939,943,946],{"class":92,"line":143},[90,937,938],{"class":96},"    return",[90,940,942],{"class":941},"sjJ54"," \"",[90,944,945],{"class":642},"Hello from A",[90,947,948],{"class":941},"\"\n",[90,950,951],{"class":92,"line":150},[90,952,147],{"emptyLinePlaceholder":146},[90,954,955,957,959,962],{"class":92,"line":157},[90,956,107],{"class":106},[90,958,111],{"class":110},[90,960,961],{"class":114},"hello_from_b",[90,963,964],{"class":110},"())\n",[894,966,968],{"id":967},"bpy",[17,969,970],{},"b.py",[81,972,974],{"className":83,"code":973,"language":85,"meta":86,"style":86},"from a import hello_from_a\n\ndef hello_from_b():\n    return hello_from_a()\n",[17,975,976,988,992,1001],{"__ignoreMap":86},[90,977,978,980,983,985],{"class":92,"line":93},[90,979,334],{"class":96},[90,981,982],{"class":99}," a ",[90,984,23],{"class":96},[90,986,987],{"class":99}," hello_from_a\n",[90,989,990],{"class":92,"line":103},[90,991,147],{"emptyLinePlaceholder":146},[90,993,994,996,999],{"class":92,"line":127},[90,995,926],{"class":925},[90,997,998],{"class":929}," hello_from_b",[90,1000,933],{"class":110},[90,1002,1003,1005,1007],{"class":92,"line":143},[90,1004,938],{"class":96},[90,1006,930],{"class":114},[90,1008,1009],{"class":110},"()\n",[14,1011,1012],{},"This can fail because one module is only partly loaded when the other tries to use it.",[14,1014,1015],{},"Ways to fix circular imports:",[29,1017,1018,1021,1024],{},[32,1019,1020],{},"move shared code into a third file",[32,1022,1023],{},"delay an import until inside a function",[32,1025,1026],{},"redesign the module structure so files do not depend on each other directly",[14,1028,1029,1030,1033],{},"For example, moving shared code into ",[17,1031,1032],{},"common.py"," is often the cleanest fix.",[73,1035,1037],{"id":1036},"how-to-debug-importerror-step-by-step","How to debug ImportError step by step",[14,1039,1040,1041,1043],{},"When you see ",[17,1042,19],{},", use this checklist.",[894,1045,1047],{"id":1046},"_1-read-the-full-traceback","1. Read the full traceback",[14,1049,1050],{},"Do not stop at the last line.",[14,1052,1053],{},"Look for:",[29,1055,1056,1059,1062],{},[32,1057,1058],{},"the exact module name",[32,1060,1061],{},"the exact imported name",[32,1063,1064],{},"the file where the problem starts",[894,1066,1068],{"id":1067},"_2-decide-what-kind-of-import-problem-it-is","2. Decide what kind of import problem it is",[14,1070,1071],{},"Ask:",[29,1073,1074,1077],{},[32,1075,1076],{},"Is Python unable to find the module at all?",[32,1078,1079],{},"Or did Python find the module, but fail to import a name from it?",[14,1081,1082,1083,53],{},"If the module itself is missing, compare with ",[281,1084,1086],{"href":1085},"\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix\u002F","ModuleNotFoundError: No module named X",[894,1088,1090,1091],{"id":1089},"_3-print-syspath","3. Print ",[17,1092,202],{},[14,1094,1095],{},"This shows where Python is searching for modules.",[81,1097,1099],{"className":619,"code":1098,"language":621,"meta":86,"style":86},"python -c \"import sys; print(sys.path)\"\n",[17,1100,1101],{"__ignoreMap":86},[90,1102,1103,1105,1108,1110,1113],{"class":92,"line":93},[90,1104,85],{"class":628},[90,1106,1107],{"class":631}," -c",[90,1109,942],{"class":941},[90,1111,1112],{"class":642},"import sys; print(sys.path)",[90,1114,948],{"class":941},[14,1116,1117,1118,53],{},"You can also learn more in the ",[281,1119,1121],{"href":1120},"\u002Fstandard-library\u002Fpython-sys-module-overview","Python sys module overview",[894,1123,1125],{"id":1124},"_4-check-for-conflicting-filenames","4. Check for conflicting filenames",[14,1127,1128],{},"Look in your current folder for files named like real modules:",[29,1130,1131,1135,1139],{},[32,1132,1133],{},[17,1134,743],{},[32,1136,1137],{},[17,1138,713],{},[32,1140,1141],{},[17,1142,1143],{},"math.py",[14,1145,1146],{},"These can cause confusing import behavior.",[894,1148,1150],{"id":1149},"_5-test-the-import-in-a-very-small-file","5. Test the import in a very small file",[14,1152,1153],{},"Create a separate file with only the import:",[81,1155,1157],{"className":83,"code":1156,"language":85,"meta":86,"style":86},"import math\nprint(math.__file__)\nprint(math.sqrt(25))\n",[17,1158,1159,1165,1181],{"__ignoreMap":86},[90,1160,1161,1163],{"class":92,"line":93},[90,1162,23],{"class":96},[90,1164,162],{"class":99},[90,1166,1167,1169,1171,1173,1175,1179],{"class":92,"line":103},[90,1168,107],{"class":106},[90,1170,111],{"class":110},[90,1172,172],{"class":114},[90,1174,53],{"class":110},[90,1176,1178],{"class":1177},"s_hVV","__file__",[90,1180,124],{"class":110},[90,1182,1183,1185,1187,1189,1191,1193,1195,1198],{"class":92,"line":127},[90,1184,107],{"class":106},[90,1186,111],{"class":110},[90,1188,172],{"class":114},[90,1190,53],{"class":110},[90,1192,177],{"class":114},[90,1194,111],{"class":110},[90,1196,1197],{"class":182},"25",[90,1199,186],{"class":110},[14,1201,1202],{},"Or for another module:",[81,1204,1206],{"className":83,"code":1205,"language":85,"meta":86,"style":86},"import module_name\nprint(module_name.__file__)\n",[17,1207,1208,1215],{"__ignoreMap":86},[90,1209,1210,1212],{"class":92,"line":93},[90,1211,23],{"class":96},[90,1213,1214],{"class":99}," module_name\n",[90,1216,1217,1219,1221,1224,1226,1228],{"class":92,"line":103},[90,1218,107],{"class":106},[90,1220,111],{"class":110},[90,1222,1223],{"class":114},"module_name",[90,1225,53],{"class":110},[90,1227,1178],{"class":1177},[90,1229,124],{"class":110},[14,1231,1232],{},"This helps you see what Python is actually importing.",[73,1234,1236],{"id":1235},"related-errors-to-compare","Related errors to compare",[14,1238,1239],{},"Some errors look similar but mean different things.",[894,1241,1243],{"id":1242},"modulenotfounderror",[17,1244,52],{},[14,1246,1247],{},"Python cannot find the module at all.",[14,1249,1250],{},"Example idea:",[81,1252,1254],{"className":83,"code":1253,"language":85,"meta":86,"style":86},"import package_that_is_not_installed\n",[17,1255,1256],{"__ignoreMap":86},[90,1257,1258,1260],{"class":92,"line":93},[90,1259,23],{"class":96},[90,1261,1262],{"class":99}," package_that_is_not_installed\n",[14,1264,1265,1266,53],{},"See ",[281,1267,1086],{"href":1085},[894,1269,1271],{"id":1270},"importerror-cannot-import-name",[17,1272,424],{},[14,1274,1275],{},"Python found the module, but not the name you requested.",[14,1277,892],{},[81,1279,1280],{"className":83,"code":327,"language":85,"meta":86,"style":86},[17,1281,1282],{"__ignoreMap":86},[90,1283,1284,1286,1288,1290],{"class":92,"line":93},[90,1285,334],{"class":96},[90,1287,337],{"class":99},[90,1289,23],{"class":96},[90,1291,342],{"class":99},[14,1293,1265,1294,53],{},[281,1295,424],{"href":423},[894,1297,1299],{"id":1298},"attributeerror-module-has-no-attribute",[17,1300,1301],{},"AttributeError: module has no attribute",[14,1303,1304],{},"The import succeeded, but you used a name that does not exist after importing.",[14,1306,892],{},[81,1308,1310],{"className":83,"code":1309,"language":85,"meta":86,"style":86},"import math\nprint(math.square_root(9))\n",[17,1311,1312,1318],{"__ignoreMap":86},[90,1313,1314,1316],{"class":92,"line":93},[90,1315,23],{"class":96},[90,1317,162],{"class":99},[90,1319,1320,1322,1324,1326,1328,1330,1332,1334],{"class":92,"line":103},[90,1321,107],{"class":106},[90,1323,111],{"class":110},[90,1325,172],{"class":114},[90,1327,53],{"class":110},[90,1329,351],{"class":114},[90,1331,111],{"class":110},[90,1333,183],{"class":182},[90,1335,186],{"class":110},[14,1337,1265,1338,53],{},[281,1339,1301],{"href":1340},"\u002Ferrors\u002Fattributeerror-module-has-no-attribute-fix\u002F",[73,1342,1344],{"id":1343},"common-causes","Common causes",[14,1346,1347,1348,1350],{},"Here are the most common reasons for ",[17,1349,19],{},":",[29,1352,1353,1356,1359,1362,1365,1368,1371],{},[32,1354,1355],{},"misspelled module name",[32,1357,1358],{},"misspelled imported function or class name",[32,1360,1361],{},"package not installed",[32,1363,1364],{},"wrong virtual environment",[32,1366,1367],{},"local file shadows a real module",[32,1369,1370],{},"circular import between project files",[32,1372,1373],{},"incorrect package or relative import",[73,1375,1377],{"id":1376},"useful-debugging-commands","Useful debugging commands",[14,1379,1380],{},"These commands are often enough to find the problem:",[81,1382,1384],{"className":619,"code":1383,"language":621,"meta":86,"style":86},"python --version\npython -m pip --version\npython -m pip list\npython -c \"import sys; print(sys.path)\"\npython -c \"import module_name; print(module_name.__file__)\"\n",[17,1385,1386,1392,1402,1412,1424],{"__ignoreMap":86},[90,1387,1388,1390],{"class":92,"line":93},[90,1389,85],{"class":628},[90,1391,632],{"class":631},[90,1393,1394,1396,1398,1400],{"class":92,"line":103},[90,1395,85],{"class":628},[90,1397,639],{"class":631},[90,1399,643],{"class":642},[90,1401,632],{"class":631},[90,1403,1404,1406,1408,1410],{"class":92,"line":127},[90,1405,85],{"class":628},[90,1407,639],{"class":631},[90,1409,643],{"class":642},[90,1411,656],{"class":642},[90,1413,1414,1416,1418,1420,1422],{"class":92,"line":143},[90,1415,85],{"class":628},[90,1417,1107],{"class":631},[90,1419,942],{"class":941},[90,1421,1112],{"class":642},[90,1423,948],{"class":941},[90,1425,1426,1428,1430,1432,1435],{"class":92,"line":150},[90,1427,85],{"class":628},[90,1429,1107],{"class":631},[90,1431,942],{"class":941},[90,1433,1434],{"class":642},"import module_name; print(module_name.__file__)",[90,1436,948],{"class":941},[14,1438,1439],{},"What they help you check:",[29,1441,1442,1445,1451,1454,1457],{},[32,1443,1444],{},"which Python version is running",[32,1446,1447,1448,1450],{},"which ",[17,1449,684],{}," belongs to that Python",[32,1452,1453],{},"which packages are installed",[32,1455,1456],{},"where Python searches for modules",[32,1458,1459],{},"which file Python actually imported",[73,1461,1463],{"id":1462},"faq","FAQ",[894,1465,1467],{"id":1466},"what-is-the-difference-between-importerror-and-modulenotfounderror","What is the difference between ImportError and ModuleNotFoundError?",[14,1469,1470,1472],{},[17,1471,52],{}," means Python could not find the module.",[14,1474,1475,1477],{},[17,1476,19],{}," is broader and can also happen when a module is found but a requested name cannot be imported.",[894,1479,1481],{"id":1480},"why-does-my-import-work-in-one-editor-but-not-another","Why does my import work in one editor but not another?",[14,1483,1484],{},"The editors may be using different Python interpreters or virtual environments.",[894,1486,1488],{"id":1487},"can-my-filename-cause-importerror","Can my filename cause ImportError?",[14,1490,1491],{},"Yes. If your file is named like a real module, Python may import your file instead of the intended module.",[894,1493,1495],{"id":1494},"how-do-i-check-where-python-is-importing-from","How do I check where Python is importing from?",[14,1497,1498,1499,1502],{},"Import the module and print ",[17,1500,1501],{},"module.__file__"," to see its location.",[14,1504,892],{},[81,1506,1508],{"className":83,"code":1507,"language":85,"meta":86,"style":86},"import math\nprint(math.__file__)\n",[17,1509,1510,1516],{"__ignoreMap":86},[90,1511,1512,1514],{"class":92,"line":93},[90,1513,23],{"class":96},[90,1515,162],{"class":99},[90,1517,1518,1520,1522,1524,1526,1528],{"class":92,"line":103},[90,1519,107],{"class":106},[90,1521,111],{"class":110},[90,1523,172],{"class":114},[90,1525,53],{"class":110},[90,1527,1178],{"class":1177},[90,1529,124],{"class":110},[73,1531,1533],{"id":1532},"see-also","See also",[29,1535,1536,1540,1544,1549,1554,1559,1563,1567],{},[32,1537,1538],{},[281,1539,1086],{"href":1085},[32,1541,1542],{},[281,1543,424],{"href":423},[32,1545,1546],{},[281,1547,1548],{"href":283},"How import works in Python",[32,1550,1551],{},[281,1552,1553],{"href":877},"How to import a module in Python",[32,1555,1556],{},[281,1557,1558],{"href":699},"How to create and use a virtual environment in Python",[32,1560,1561],{},[281,1562,540],{"href":539},[32,1564,1565],{},[281,1566,882],{"href":881},[32,1568,1569],{},[281,1570,1301],{"href":1340},[1572,1573,1574],"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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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 .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_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 .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":86,"searchDepth":103,"depth":103,"links":1576},[1577,1578,1579,1580,1581,1582,1583,1584,1585,1589,1597,1602,1603,1604,1610],{"id":75,"depth":103,"text":76},{"id":235,"depth":103,"text":236},{"id":287,"depth":103,"text":288},{"id":320,"depth":103,"text":321},{"id":427,"depth":103,"text":428},{"id":595,"depth":103,"text":596},{"id":703,"depth":103,"text":704},{"id":791,"depth":103,"text":792},{"id":885,"depth":103,"text":886,"children":1586},[1587,1588],{"id":896,"depth":127,"text":899},{"id":967,"depth":127,"text":970},{"id":1036,"depth":103,"text":1037,"children":1590},[1591,1592,1593,1595,1596],{"id":1046,"depth":127,"text":1047},{"id":1067,"depth":127,"text":1068},{"id":1089,"depth":127,"text":1594},"3. Print sys.path",{"id":1124,"depth":127,"text":1125},{"id":1149,"depth":127,"text":1150},{"id":1235,"depth":103,"text":1236,"children":1598},[1599,1600,1601],{"id":1242,"depth":127,"text":52},{"id":1270,"depth":127,"text":424},{"id":1298,"depth":127,"text":1301},{"id":1343,"depth":103,"text":1344},{"id":1376,"depth":103,"text":1377},{"id":1462,"depth":103,"text":1463,"children":1605},[1606,1607,1608,1609],{"id":1466,"depth":127,"text":1467},{"id":1480,"depth":127,"text":1481},{"id":1487,"depth":127,"text":1488},{"id":1494,"depth":127,"text":1495},{"id":1532,"depth":103,"text":1533},"Master importerror in python causes and fixes in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Fimporterror-in-python-causes-and-fixes",{"title":5,"description":1611},"errors\u002Fimporterror-in-python-causes-and-fixes","5oykHFp5vqern46CBvoAzW5dk1q3_NKR9xZfhcVoA-Y",1777585479567]