This is the common error we face after importing any specific library or copying the code from the portal or trying to run the script that we have written very long.
ModuleNotFoundError: No module named <PackageName>
Why this error?
This error of ModuleNotFoundError: No module named '' generally occurs because the package that we want is not successfully imported by python.
So the simplest and straightforward solution to this issue is to debug the error, by reading the error carefully and installing that package with a simple pip install.
Three-Step Solution to this error
Step 1:- Open Terminal/ CMD depending on your OS
Step 2:- First Check whether the Pip/Brew is installed or not ( simply type pip or brew and hit enter ) If getting an error install read below
Step 3:- Run this command in the Terminal
Check if the Package Install
Use pip show <PackageName>
Install the package using the above command, it will definitely fix the issue.
Still not Resolved the Error?
Then you need to troubleshoot the issue for the different following result
Installation process for Different Setup
Conda
#To install a package in the currently active environment
conda install package-name
#To install a specific package version into a specific named environment
conda install package-name=2.3.4 -n some-environment
You can also use Offline Install if the .tar file of that package is available
conda install /path-to-package/package-filename.tar.bz2/
For more can refer original Doc https://docs.conda.io/projects/conda/en/latest/user-guide/concepts/installing-with-conda.html
Spyder
If you want to install a package in spyder, means you are already set up conda environment, like
conda create -n spyder env -y
conda activate spyder -env
If this is done then installing any package is simple
Run conda install numpy -y
https://docs.spyder-ide.org/current/_images/faq-run-environment.png
For more delta read Official Documentation https://docs.spyder-ide.org/current/faq.html
Jupyter
If you want to install the package in Jupyter then you need to use
Or
But for this Jupyter must be installed already
If not read Install Jupyter
MAC OS /m1.zsh
In Mac m1 or in Older versions also installing python package methods is the same, In macOS, we have a .zsh terminal, but then also needs the same process as Linux/ubuntu.
Check whether Python is installed or not
python --version
Check Where python is installed
which python
which python3
Two ways to install the package
Using Brew
Check Brew in Present brew --version
Using Pip
Check whether the pip is present or not pip --version
And if you have python2 and python3 both try to use pip3 --version
Windows
In windows, this process is quite a similar check for pip, and if git bash then no issue.
Otherwise use this code
Linux ubuntu bash
In Linux Python and pip come preinstall if not you can use
To install pip in any Linux distro
Note:- If you are using the Ubuntu AWS ec2 instance you need to update the system and then have to try installing pip
PyCharm
Pycharm is a cross-platform IDE that provides consistent experience on the Windows, macOS, and Linux operating systems.
Here also it is simple if you have already PyCham installed, just follow the below steps
Package Installation in PyCharm IDE
- Open Pycharm and Go to File
- Click on Settings
- On the left side, You will find the Projects Option. Click & Expend it
- Click on Interpreter
- A list of the installed packages will be shown
- On Right Side, You will see a + button. Click This to install a new package
- A window will appear with the Search bar, then search your package name and hit the install button.
Readme:- https://www.jetbrains.com/help/pycharm/package-installation-issues.html#results
Django
There is no special installation needed
Xcode
Vscode
Nowadays VsCode is the First Choice of any developer, and installing the library in vs code is not different than a simple pip install.
If you are using git bash, just open the terminal check pip is installed or not, and run the pip install package.
If using windows read the window installation guide given above.
Google Collab
Google Colab is online
!pip install package_name
import package_name
Common Error and how to solve it
Spelling Error
Package
Package Name Change
Some package
Module change with version
User Defined Module
User defines modules are the module that is created by yourself or the file code present in the same folder.
So if no module error is for this file name then you don't need to install any package from the pip as the code that you are trying to use is already there in the folder.
For this reason, there may be other reasons like
- Not using the correct path
- Using absolute path
- The file name is not correct, you misspelled/ done a typo in the name
- Casing issue
Python 2 versus python3
This may be the potential Error if you are using python3 and python2 simultaneously, environment is different for both, like python2 filename.py run the code in python2 environment and python3 filename,,py run the code in python3.
In the same way installation of libraries is both environment is also different, so for install the package in any particular version you need to specify the pip.
pip install <PackageName>
pip3 install <PackageName>
Try this it will help.
Install Pip or upgrade it
Many times this is also one of the issues that you are using python2 and the library is not compatible with that version.
Another reason might be that the pip is not installed or not upgraded, and that stopping you from the
Clear the cache pip
Debugging in Python
Debugging any language code is not a complex process, just read the error, see the line number and check for that. In our Case for module error, we just need to read the error and need to see if the library we are importing is present in our system or if its name is changed or if troubleshooting will solve our issue.
Here read:- Art of Debugging the code
How to install All the Packages that are needed to install for any particular project
If you have cloned any GitHub project or got a python file from someone, The simplest way to install all the required libraries is using pip freeze.
Use pip freeze > requirement.txt
This will create a requirement file that contains the name of all the libraries that are needed to run any particular python code.
To install all the liberties use
pip install -r requirement.txt
For python3
pip3 install -r requirement.txt
There are a lot of pip commands you can learn here.
Conclusion
So we definitely think, this article helps you to fully understand about the error and why the error occurs in the first place. This module error is very frustrating for all beginners and if you are dealing with any GitHub project and trying to learn and explore that, you will get this error quite often
There may be different reasons for this error, like spelling mistakes in the package, If the name is correct then check the casing, the package owner changes the name of the package while updating the library, Also if you are trying to use your own file from some different path check the path.
So Will wrap up here, As I think we have covered all the possible issues, why who what about the error, I think things are clear, further also for more such errors you can search on our blog.
Happy learning, Keep Coding Keep Growing.
How to install Pip
In Window
Check pip is present or not
In Linux
Check pip is present or not
In macOS
Check pip is present or not
All pip Commands
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
inspect Inspect the python environment.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
config Manage local and global configuration.
search Search PyPI for packages.
cache Inspect and manage pip's wheel cache.
index Inspect information available from package indexes.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
debug Show information useful for debugging.
help Show help for commands.
General Options:
-h, --help Show help.
--debug Let unhandled exceptions propagate outside the main subroutine, instead of
logging them to stderr.
--isolated Run pip in an isolated mode, ignoring environment variables and user
configuration.
--require-virtualenv Allow pip to only run in a virtual environment; exit with an error otherwise.
--python <python> Run pip with the specified Python interpreter.
-v, --verbose Give more output. Option is additive, and can be used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output. Option is additive, and can be used up to 3 times
(corresponding to WARNING, ERROR, and CRITICAL logging levels).
--log <path> Path to a verbose appending log.
--no-input Disable prompting for input.
--proxy <proxy> Specify a proxy in the form scheme://[user:passwd@]proxy.server:port.
--retries <retries> Maximum number of retries each connection should attempt (default 5 times).
--timeout <sec> Set the socket timeout (default 15 seconds).
--exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe,
(b)ackup, (a)bort.
--trusted-host <hostname> Mark this host or host:port pair as trusted, even though it does not have valid
or any HTTPS.
--cert <path> Path to PEM-encoded CA certificate bundle. If provided, overrides the default.
See 'SSL Certificate Verification' in pip documentation for more information.
--client-cert <path> Path to SSL client certificate, a single file containing the private key and
the certificate in PEM format.
--cache-dir <dir> Store the cache data in <dir>.
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine whether a new version of pip is
available for download. Implied with --no-index.
--no-color Suppress colored output.
--no-python-version-warning
Silence deprecation warnings for upcoming unsupported Pythons.
--use-feature <feature> Enable new functionality, that may be backward incompatible.
--use-deprecated <feature> Enable deprecated functionality, that will be removed in the future.