Solving Python Error – ImportError: No module named

ImportError: No module named is an error which occurs when your package is not visible in sys.path.

In this article I am going to explain why the error is happening and how to solve it without getting the error again. Also we are going to check out other solutions which may work for your particular case.

Explaining the Error : ImportError: No module named

The error happens when your package is not visible in sys.path.

First, if you are getting the error this means you are trying to import a package not seen by sys.path.

For example you are trying to import a module called inmo.py which resides inside a folder called

doyin which lives inside a folder called targi.

You can use the line of code inside your script.

                                                                       #
import targi.doyin.inmo
                                                                       #

The line is correct but you can still get the same error message.

Bellow we will take care of the error using multiple possible solutions according to your needs.

Solution : using PYTHONPATH

The error we described above rarely happens in windows. Either way, We have a solution for both Linux and windows.

The solution is to use the environment variable PYTHONPATH.

On Windows open the command line and run the following command:

                                                                       #
SET PYTHONPATH=C:/path/to/folder
                                                                       #

On Linux open the shell and run the following command:

                                                                       #
export PYTHONPATH=/path/to/folder
                                                                       #

folder is the directory containing project. In the example we used above folder is the parent directory of targi.

This is genuinely the only solution of this issue that I have tried and has worked for me.

I could not find any other solution and do not wish to make stuff up.

If this article has been useful for your particular case, consider donating to our Kofi account, there is a red button at the top of this page.

Summing-up

I hope this article has been helpful and helped you solve this error, the first solution should be enough and should work for Windows and Linux. I hope you continue coding and learning Python, errors are part of the fun even when we hate them, cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/