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()}")

 

So if you are getting No module named 'platform' this means there is no file named platform.py in your 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 platform.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 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

 

Sometimes this also occurs if there is a .pyc file (Python cache file) in the same directory as the module you are attempting to import. For example, let's say you have both platform.py and platform.pyc.

/usr/lib/python2.7/site-packages/platform.py
/usr/lib/python2.7/site-packages/platform.pyc

 

Thus sometimes you can resolve this by removing the .pyc file.

rm /usr/lib/python2.7/site-packages/platform.pyc

 




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 80c42c in the box below so that we can be sure you are a human.