Module Not Found
Lastmod: 2023-01-26

Overview

Traceback (most recent call last):
  File "afile.py", line 1, in <module>
    import doesnotexist
ImportError: No module named doesnotexist

When you run a Python application, script, or interactive terminal, you see an error that looks like the above.

Initial Steps Overview

  1. Check module exists

  2. Check command line loading

  3. Check module is correctly set up

  4. Check installation method

  5. Module in a subfolder?

  6. Double import trap?

  7. Virtualenv activated?

Detailed Steps

1) Check module exists

First, determine that the module exists on your system, and where.

There are a number of ways to determine this if you are unsure how to proceed.

1.1) Use locatedb

If you have a system with locatedb installed on it and have access to update it, and are looking for a module called modulename, you can try running:

sudo updatedb
locate modulename | grep -w modulename$

which should output possible locations where the modules is installed.

If locatedb is not installed, or not up to date/under your control, then you can try and use find to search for the module in a similar way. Or you can look at using other tools available to you in your environment.

1.2) Use the sys.path

If you know the version of Python you are using on the command line is correct (see step 2.1), then you can check the default path where Python modules are searched for by adding the following to the file that is trying to load the module:

import sys
print("sys.path:\n" + "\n".join(sys.path))
exit()

You could run this from the Python Interpreter, however doing so will lead to the first entry being blank instead of showing the current working directory that your script would check. The output will show you the folders that python searches through looking for Python packages in their immediate subfolders.

The sys.path value is figured out on startup by Python running a site.py located under the Python installation. This dynamically picks up relevant folders based on the code in that file.

2) Check command line

After you have determined that the module exists, check that the module loads when you run Python on the command line.

You can do this by running Python on the command line, and typing at the prompt:

import modulename

if the prompt returns without a message, then the module was loaded OK.

If this doesn’t solve your problem because the program you were running was not running on the command line, OR you still the same error, consider the following possibilities:

2.1) You are using the wrong Python version

It may be that you are using Python version 2.x instead of 3.x, or vice versa. Or even a different minor or point version of Python. Different Python versions can look in different locations for their libraries, and so using the wrong version can mean that the library can’t be found.

You can check your version directly from your script by adding the following snippet just above your failing import:

import sys
print("sys.version: " + sys.version)
print(str(sys.version_info))
exit()

If your Python module was in a folder with a version number in it, eg:

/Library/Python/3.7/site-packages

then this may be the problem you are facing.

2.2) Your PYTHONPATH is misconfigured

If your module is in a non-standard location, then it’s possible your PYTHONPATH needs to be configured, or reconfigured.

For more information on this, see here.

3) Check the module is correctly set up

If you are using a pre-3.3 version of Python, then it is a requirement that a Python module contains a file called __init__.py.

The inclusion of the file is often forgotten about when creating modules on-the-fly, so is a frequent cause of failure.

You may also want to consider whether the user you are running as has the right permissions to access the files and folders (see step 4).

4) Check installation method

If you installed the module using pip or some similar package management method, then consider:

  • Did you install as the root user (using sudo or similar)

  • Did you install as a user other than the user running the Python import?

5) Module in a subfolder?

Is the module you are trying to load in a subfolder?

If so, you may need to more carefully qualify your module import. See here for more background.

6) Double import trap?

If you have PYTHONPATH set in your environment, or are adding to your sys.path within your code or configuration, you may be falling victim to this trap, which can cause havoc with relative imports and the like. See here for more background.

7) Virtualenv activated?

If your python code runs in a virtualenv, you may need to activate it for this error to be resolved. See solution A for more on this.

Solutions List

A) Activate your virtualenv

Solutions Detail

A) Activate your virtualenv

Typically, this will require you to run:

virtualenv .
source bin/activate

before you re-run your code.

Check Resolution

The application or steps followed at the start no longer result in an error.

Further Information

This is an excellent resource in this context, for background and troubleshooting: Import traps

General primer on Python packages:

Virtualenvs

Owner

Ian Miell

comments powered by Disqus