How to fix ModuleNotFoundError: No module named ' ' in Python on Windows, Linux, and macOS

Jan 12, 2022 · 6 minute read

Installing python modules

The first thing to do is make sure that you have installed the module or package for which you're getting the error. By default, python includes a few modules and packages some popular examples being json time and os. However, there are also packages that have been created by python's community of developers to make these packages easy to use developers add them to the python package index which is a repository of software for the Python programming language. To install packages from the python package index we use a command-line utility called pip.

How to use pip on macOS

To use pip on macOS, open up your terminal emulator by hitting command plus space on your keyboard to open up the spotlight search field. Type terminal and hit enter. On the new window that opens up, type pip the version of python that you're using to run your program with space install space the name of the package or module that you want to install then hit enter. If you don't see any errors on the screen then your package was installed successfully.

How to use pip on Linux

For Linux, the process is the same with the exception that opening up your terminal emulated program will differ across multiple Linux distributions depending on your desktop environment and system configuration. So I will assume you know how to do that.

How to use pip on Windows

To use pip on windows open up the start menu by clicking on the windows logo or hitting the windows key on your keyboard. Then type cmd and hit enter. On the new window that opens up type py space - the version of python that you're using to run your program with space -m space pip space install space the name of the package or module that you want to install and then hit enter. If you don't see any errors on the screen then your package was installed successfully.

How to use pip with an IDE

It should be noted that if you are using a text editor like vs code or an integrated development environment like pycharm, you can also use a built-in terminal emulator to install packages using pip.

Error installing a package

If you get an error saying that the command cannot be found double-check the spelling and spacing of the command. If everything looks correct, then this means that you do not have pip installed. This process can vary depending on your operating system, so look into doing that first, then come back here and try it again. If the module or package that you are using is not available in the python package index repository, then refer to the developer's instructions for installing the package. If after successfully installing the package using pip you are still getting the same error, take a closer look at the output of the pip install command; more specifically look at the installation path. You want to be sure that the version of Python and the path matches up to the version of python that you are using to run your program. In this case python 39 means, we installed selenium using python 3.9 which means that it won't work if we try running our program with python 3.6.

Common macOS and Linux problem

A common problem that people experience with mac os and Linux systems is forgetting to specify the version of python when using the pip command. This is important because, by default, these systems can come pre-installed with python 2.7. So the default configuration when using pip without specifying a version number is to use Python 2.7 to install packages. This means that if you're using python 3 to run your program, that package you are importing will not be found in the python 3 installations. To fix this issue, we specify the version of python we want to install the module or package with. If you have multiple major releases of python 3 installed like python 3.6 3.7 3.8 and 3.9, then you can specify those versions to install your packages to see if you have multiple versions of python installed.

How to check for multiple Python installations on Windows

On Windows, you can click on the start menu and then look up "apps and features". Click on the apps and features option and on the new window that pops up scroll down and see if you have multiple versions of Python installed.

How to check for multiple Python installations on macOS

To see if you have multiple versions of Python installed on macOS, you can open up the finder application then go to the application section. If you see more than one version of python installed then go ahead and use the appropriate one when using pip.

How to check for multiple Python installations on Linux

To see if you have more than one version of Python installed on Linux, you can use the ls command to filter out python installations in the bin directory. Then if you have multiple versions of python installed, go ahead and use the appropriate one when using pip.

How to specify the version of Python to use on macOS and Linux

In other words, if you are using python 3.9 to run your program on macOS and Linux you would use pip 3.9 space install space the name of the package or module that you want to install.

How to specify the version of Python to use on Windows

On Windows, you will use py space -3.9 space -m space pip space install space and the name of the package or module that you want to install.

Take note of virtual environments

If at this point you are still getting the same error taking note of any virtual environments that you might be using in your project. If you are not using an IDE to code then chances are that you will know if you are using a virtual environment. However, if you are, often IDEs will create a virtual environment automatically when you create a new project. So if you are using your operating system's terminal emulator to install packages using pip as we discussed before. Those packages will not be available in your project's virtual environment which is the same as installing the modules or package in the wrong python installation path. To fix this, we either manually start the virtual environment in our operating system's terminal emulator; which can differ depending on the tool used to create the virtual environment. Or simply we can use a terminal emulator that is built into your ide to install packages using pip. This is because these built-in terminal emulators will automatically initiate the virtual environment when the project has been loaded. In addition, if you install packages in a virtual environment and are trying to run the program outside of that virtual environment; you won't have access to the third-party packages or modules installed in the virtual environment. So the error will persist. To fix this, you need to either start the virtual environment first or install those missing packages globally on your system as we discussed at the beginning.