Splunk Dev

"ModuleNotFoundError: No module named 'sn_sec_util'" error on loading python custom module

gsabhay77
Explorer

I have created a custom python script named "sn_sec_util.py" in the bin folder of the splunk app. I want to load this file in the python REST handler "my_submit.py" which is also in the bin folder.

But I am facing the issue at the import statement mentioning "ModuleNotFoundError: No module named 'sn_sec_util".

Here is the file "my_submit.py" with import statement of the module "sn_sec_util.py" in the line 4.

import sys
import splunk
import logging, logging.handlers
import sn_sec_util as snutil
from urllib.parse import unquote

class Submit(splunk.rest.BaseRestHandler):

    def handle_submit(self):
         payload = self.request['payload']
         sessionKey = self.sessionKey
    handle_POST = handle_submit 
Labels (2)
0 Karma

tpavlik_splunk
Splunk Employee
Splunk Employee

Hey gsabhay77,

Your ability to import other modules will depend on your PYTHONPATH which is specific to your system where Splunk is running. The easiest way to debug the path that the script is running from is to output sys.path:

import sys
print(sys.path) // ['', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8', ...]

Without getting too much into your system setup if you simply need the directory that your my_submit.py file resides in to be added to the path you could do so by adding the current absolute path that the file is running from using:

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)))

Adding those lines to the top of your script should hopefully resolve your issue.

0 Karma
Get Updates on the Splunk Community!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...