Bootstrap FreeKB - Python (Scripting) - Resolve ModuleNotFoundError
Python (Scripting) - Resolve ModuleNotFoundError

Updated:   |  Python (Scripting) articles

Let's say you are getting something like this.

ModuleNotFoundError: No module named 'module name'

 

The import keyword is used to import a module. There are several built in modules that are part of the Python Standard Library included with your Python installation, such as os (Operating System) and re (Regular Expression) and platform. For example, here is how you would import and then use the platform module.

#!/usr/bin/python3
import platform

print(f"Platform = {platform.system()}")

 

Running this script should return something like this.

Platform = Linux

 

What has happened here is that the platform.py module was used. The platform.py module should be found somewhere in sys.path.

ModuleNotFoundError occurs when a module (such as example.py) is not located in sys.path. Here is how you can print the system paths.

#!/usr/bin/python
import sys

for path in sys.path:
  print(path)

 

Which should return something like this. This means the module (such as example.py) is not located in any of the system path directories.

/home/john.doe
/usr/lib64/python27.zip
/usr/lib64/python2.7
/usr/lib64/python2.7/plat-linux2
/usr/lib64/python2.7/lib-tk
/usr/lib64/python2.7/lib-old
/usr/lib64/python2.7/lib-dynload
/usr/lib64/python2.7/site-packages
/usr/lib/python2.7/site-packages

 

If you have the module installed into a directory not included in the system path directories, you can append the directory that the module resides in to your sys.path.

#!/usr/bin/python
import sys
sys.path.append(/path/to/directory) <- the directory that contains shared_functions.py
import shared_functions

 

Taking this a step further, you could do something like this to see if there are any files below the system paths containing the module name, such as "paramiko".

#!/usr/bin/python3
import os
import re
import sys

string = "paramiko"

for path in sys.path:
  print(f"path = {path}")

  matched_files = []

  for root, dirs, files in os.walk(path):
    for name in files:
      if re.match(string, name):
        matched_files.append(os.path.join(root, name))

for matched_file in matched_files:
  print(f"the following file contains {string}: {matched_file}")

 

Which could return something like this.

path = /usr/lib64/python27.zip
path = /usr/lib64/python2.7
path = /usr/lib64/python2.7/plat-linux2
path = /usr/lib64/python2.7/lib-tk
path = /usr/lib64/python2.7/lib-old
path = /usr/lib64/python2.7/lib-dynload
path = /usr/lib64/python2.7/site-packages
path = /usr/lib64/python2.7/site-packages/gtk-2.0
path = /usr/lib/python2.7/site-packages
the following file contains paramiko:/usr/lib/python2.7/site-packages/ansible/module_utils/compat/paramiko.py
the following file contains paramiko:/usr/lib/python2.7/site-packages/ansible/module_utils/compat/paramiko.pyc
the following file contains paramiko:/usr/lib/python2.7/site-packages/ansible/module_utils/compat/paramiko.pyo
the following file contains paramiko:/usr/lib/python2.7/site-packages/ansible/plugins/connection/paramiko_ssh.py
the following file contains paramiko:/usr/lib/python2.7/site-packages/ansible/plugins/connection/paramiko_ssh.pyc
the following file contains paramiko:/usr/lib/python2.7/site-packages/ansible/plugins/connection/paramiko_ssh.pyo

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter b886c5 in the box below so that we can be sure you are a human.