How to Fix ModuleNotFoundError No Module Named Error In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so you're trying to run a python file and you get this annoying module not found error right I've been there especially when I was new to python here on the screen I have this basic Python program that Imports the package called numpy first let's open the terminal in vs code and try running the program type python name of your file followed by Dot py and hit enter what happens you get an error that says no module named numpy your package might be different maybe it's pandas or something else here's the deal this error shows up because numpy isn't installed so let's fix that to install a package use the PIP command if you're on Python 3 which you likely are use pip3 so go ahead and type pip 3 install numpy and hit enter you'll see some text flying by as it installs I'm doing numpy because that's what my program uses but you should replace that with whatever package you're missing you might see some warnings but focus on the message that says successfully installed numpy that means you did it right I know I know there might be complaints about an outdated version of pip but let's ignore that for now alright let's clear the terminal and run the program again and look at that no more module not found error this time the program works just fine in my example I'm using numpy for working with arrays in this program I basically created an array with nine items and rearranged them into a 3X3 grid and there you have it problem solved but what if you installed the module and you still get module not found error with no module named pandas or some other package name to fix it first make sure you have python extension installed in your vs code go to extensions type python click on first search results and click on install button next to the python extension I'll go ahead and open my python project where I get the module not found message and that pesky no module name error okay here it is I am going to click on python folder and click select folder to open it as a project in my visual studio code so the other reason you might get this error is because you're trying to run your Python program in a different python environment you see when you install a module it's installed into a python environment each module is installed separately for each version of python so to make it work and find your module you have to run your Python program from the same python environment into which that module was installed because we just installed python extension we now have a new Option here in Visual Studio code look at the bottom right it says Python and your current version number to switch python environment click on this button here then select python version from this drop down in Visual Studio code you will probably have multiple python versions installed maybe it's Python 2.7 or python 3.11 and a few others it's possible to have a large number of python environments here because of all the previous times you've installed python in the past or maybe it came with some piece of software that forced you to install a specific version of python anyway change the python environment use pip command to install your module then run the program again just make sure you're installing your modules into the same environment that you are using to actually run your python file this is really all you need to know to fix the module not found error in Python
Info
Channel: Ghost Together
Views: 15,902
Rating: undefined out of 5
Keywords: python, ModuleNotFoundError, No Module Named, fix python error, py, vscode, vs code, terminal, command line, visual studio code
Id: OAywo1HbEEc
Channel Id: undefined
Length: 3min 12sec (192 seconds)
Published: Tue Sep 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.