Python getting module named

How To Fix ModuleNotFoundError

Including module at the top of your Python program is not enough.

It must also be actually installed using pip command that comes with Python installation.

The pip command is the Python’s package (module) manager.

But even after installing module it might still not work due to wrong Python environment.

In this tutorial I’ll show you exactly how to fix this error in detail. Let’s go! But first.

It took hours to put this material together. Please help me out by sharing it 🙂

Click button below to share link on WhatsApp or Discord or with friends:

I really hope that the reason you get ModuleNotFoundError No Module Named error is simply because module you’re trying to include is not installed. If that is not the case, you might be installing it into another Python environment, not the one you’re running your program from.

This is the most common error you will get into, especially when learning Python.

To fix ModuleNotFoundError No Module Named Error , follow these steps:

  • Install the module with pip command from your project folder.
  • Make sure module name is not misspelled.
  • Learn about existence of multiple Python environments.
  • Make sure your pip command installs module into correct environment.
  • Uninstall previous version of Python, if installed.
  • Reinstall the most recent version of Python again.
  • Restart your computer after reinstalling Python.
  • Make sure module name is correct.

About that last point, in rare cases, some module import names don’t match pip command’s package name. Simply you need to follow official documentation for that module.

And a lot of the time it happens because you have multiple versions of Python installed.

It’s not uncommon to have multiple Python environments on one computer, especially Linux.

It doesn’t help Mac gets shipped with Python 2.7 by default. I’ll explain.

The most common reason for ModuleNotFoundError error is: you installed a module in one environment. Usually, whaterver your pip command is wired to. But you are trying to execute the program with python or python3 commands, which may be associated with another Python environment, not the one that was used to install the module.

This is why ModuleNotFoundError happens even if you installed the module with pip.

(Yes the module was installed. But to what environment?)

Did you know that you might have pip , pip2 and pip3 commands? All of which might be configured to install the module into a different environment.

Standard pip command inherits the environment of the most recently installed Python, overwriting previous configuration, simply by installing new Python. Each new version of Python has a slightly different Windows installer, which may or may not ask you to install pip .

Читайте также:  Вертикальная линия в питоне

This might be a good idea when installing Python for the first time. But with another version of Python already installed, clicking on checkbox to install pip during installation, you might overwrite previous pip that was configured to install modules to a previous version of Python.)

Learning how to use Anaconda (conda) which allows you to switch Python environments on the command line is often used as the ultimate solution. But installing conda might be a bit complex for beginners. Visual Studio Code’s Python extension can do the same.

It took hours to put this material together. Please help me out by sharing it 🙂

Click button below to share link on WhatsApp or Discord or with friends:

There are other much more complex cases. But usually that means your development environment or Python environment are messed up in some way. I recommend using VSCode, together with its Python extension, which allows you to automatically identify all existing Python environments, and swap them with a simple drop down menu that appears in the blue status bar at the bottom next to «Python» button.

However, if you work on a more complex project, or your Python isn’t configured correctly, you might want to read the rest of this article to understand how Python adds modules to your program. And then try to resolve it with this new knowledge.

In which case, for visual speed learners, here’s a video showing how to fix it:

This error is encountered for other popular modules. A separate tutorial exists for each @ utils, mysql, jwt, docker, matplotlib, selenium, pandas, pygame, tkinter, cv2, numpy, tensorflow, feras, requests, pip, pil, api.

How does importing Python modules work?

To add a new module use import keyword followed by package name:

If module can be found ideally this should be enough to start using it.

But if all goes wrong you may receive one of the following two errors:

  • ModuleNotFoundError: No module named ‘modulename’
  • ImportError: cannot import name ‘modulename’

But how does Python know where to look for modules?

Well, a lot happens in one line. And it takes a bit dissecting to understand.

Let’s break down the process to see what Python will do to locate your module:

  1. It will locate the filename associated with imported package.
  2. Load and initialize (if required) the module.
  3. Add necessary names in local namespace and current program scope.
  4. Next Python interpreter will try to resolve modulename
  5. sys.modules is the dictionary map Python will use to lookup module names that have already been loaded (another module could have already loaded it, etc.) and if so, it will become available in the scope from which you’re importing it.
  6. If previous step fails, Python will use Python Standard Library looking. The PSL contains all built-in modules written in C language thatp provide file system access, math library, and various system tools dealing with most common issues.
  7. If previous step fails, Python will try to resolve the module under sys.path which is probably where things went wrong when you received ModuleNotFoundError no module named error.
Читайте также:  Php mysql fetch one column

How To Fix ModuleNotFoundError (no module named) Error

The ModuleNotFoundError: No module named error is raised when Python either cannot find the module you’re trying to import, the name of the package being imported was misspelled, or the module doesn’t exist on your hard drive.

Basically all it means is that you need to install the module/package before it can be imported into your program (with import keyword.)

But of course there is more to it than that.

This article will explore this error in more depth.

But what if it’s not that simple?

If you’re getting import errors for a more complex reason, consider the structure of package dependencies. Usually it’s something as follows. (Note that packages can include other packages, or «sub-packages».)

└── project ├── package │ ├── something.py └── otherpackage ├── chicken.py ├── rooster.py └── subpackage └── eggs.py

This is where discerning between absolute and relative packages may matter.

Given example above you can use an absolute import:

Another example of an absolute import:

import otherpackage.chicken import otherpackage.rooster

And here’s an absolute path to the location of the subpackage eggs :

import otherpackage.subpackage.eggs

A relative import are a lot more common and therefore more prone to import errors. Below are 3 examples of Python’s relative imports:

# importing from the scope of something.py 

from ..otherpackage import rooster

from ..otherpackage import chicken from ..otherpackage.chicken import lay_egg_function

Here is another example of a relative import:

# importing from the scope of chicken.py 

from . import chicken

from .rooster import peck_seeds_function

How to fix ModuleNotFoundError and ImportError?

Now that we’re somewhat more familiar with how Python imports packages, and we’ve looked at absolute and relative imports, it’s time to take a closer look at ModuleNotFoundError (and ImportError) and see how it can be fixed.

In a large majority of cases these errors occur because Python interpreter fails to resolve the module name in sys.path (the place Python looks in after module look up fails in both sys.modules and the Standard Library.

Using the from keyword follows the same pattern. When the look up fails, Python will throw the ModuleNotFoundError for import keyword. And ImportError for look ups in a from keyword.

Final solution to fixing ModuleNotFoundError No Module Named error:

One way to avoid import errors is to avoid relative paths altogether.

  1. Avoid using relative paths
  2. Try using absolute path in your import s
  3. Or use built-in PYTHONPATH system variable:

If you are running your Python app in Docker or Vagrant, this last point might be the key to solving most of your module errors.

Go to your bash and execute the following directive on the command line:

# On Linux/Unix/MacOS/Terminal 

export PYTHONPATH="$:/path/to/project/"

On Windows

set PYTHONPATH=%PYTHONPATH%;C:\path\to\project\

(Obviously change the path to your own project)

This will link up your project directory to PYTHONPATH and whenever you use absolute imports, nothing should get in the way of avoiding the ModuleNotFoundError error.

I really hope that it’s not that complex in your case 🙂

The error ModuleNotFoundError no module named can happen for a number of reasons, including incorrect module names, incorrect module paths, and unintended circular dependencies. In this tutorial, we’ll take a look at some of the most common causes of this error and how to fix them.

What are Python modules and why do we need to import them?

You might have just started out with Python. You wrote basic Python programs.

To advance your application, at some point, you are most likely need to start including Python modules, which can be AI, math or physics libraries.

You probably don’t want to spend time writing packages yourself. Simply because many modules that do the very thing you need and do it efficiently.

These modules are freely available. Thankfully programmers that came before us took the time to create them and distribute them for free.

And so this is why we have to import modules in Python.

Here’s some terminology and background info:

  1. A python module is just a file with .py extension
  2. A python package is a folder that contains at least one python module. When it comes to creating your own python modules a folder requires __init__.py file for it to be considered a package.
  3. A python package can contain other packages (sub-packages)
  4. You will import a package when you need to add useful functionality written by someone else, to save time writing your application.

Module Doesn’t Exist (It Wasn’t Installed Yet)

One common cause of the ModuleNotFoundError: No module named error is simply that the module you’re trying to import doesn’t exist. This can happen if you’ve misspelled the module name, or if you’re trying to import a module that’s not in your Python path.

To fix this, make sure you’re using the correct module name, and that the module is in your Python path (or at least in your project directory where you’re executing your main .py file from.)

Circular Dependency

Another common cause of this error is a circular dependency. That is, two modules that are trying to import each other. This will cause an infinite loop, and eventually raise the ModuleNotFoundError: No module named error.

To fix this, make sure that there are no circular dependencies in your code. Finally, make sure that all modules are imported in the correct order. If one module imports another before it’s been fully initialized, this can also lead to the ModuleNotFoundError: No module named error.

Other Causes For ModuleNotFoundError (no module named) Error

There are many other causes of the «ModuleNotFoundError: No module named» error, but these are some of the most common. If you’re seeing this error in your own code, make sure to check for these potential causes and fix them accordingly. With a little troubleshooting, you should be able to get your code up and running in no time!

Other ModuleNotFoundError Tutorials (not all modules are made alike!)

Hope this helped someone out there 🙂

This is the most common error you will get into, especially when learning Python.

It took hours to put this material together. Please help me out by sharing it 🙂

Click button below to share link on WhatsApp or Discord or with friends:

Click and share link to this article to help other Pythonistas!

Источник

Оцените статью