Splunk Dev

How to import Python module from subdirectory in appserver/controllers?

twesthead
Path Finder

Dear Splunk,

I have quite a few custom endpoints in my web.conf file.
This leads to many Python scripts in my appserver/controllers directory and many helper scripts for functions used by several endpoints.
I would like to tidy up this directory by putting some of the scripts in local packages or subdirectories.
As an example, here's my file structure:

appserver
--->controllers
------->my_script.py
------->mylib
---------->mymod.py

My code:

#mymod.py
def scream():
    return 'YEAH!'

#my_script.py
import splunk.appserver.mrsparkle.controllers as controllers
from splunk.appserver.mrsparkle.lib.decorators import expose_page

import mylib.mymod as mm

class Controller(controllers.BaseController):
    @expose_page(must_login=False, methods=['GET']) 
    def scream(self, **kwargs) :
        return mm.scream()

Now, if I add a empty __init__.py file to /mylib, mylib behaves like a package, and if I test the script locally with splunk's python interpreter in CLI: splunk cmd python my_script.py , I am able to import to import my module and use the scream() function from my_script.py.

However, the import doesn't work one I try to run the script through the web service: http://localhost:8000/en-US/custom/my_app/my_script/scream throws a 404 Error.

What's the Splunk-approved way of organizing code in my controllers directory ?

0 Karma
1 Solution

dominiquevocat
SplunkTrust
SplunkTrust

You need to extend the python path 😞

        import sys
        sys.path.append(os.path.join(os.environ['SPLUNK_HOME'],'etc','apps','YourAppNameHere','appserver','controllers')) #build local path and add it to the python path so we can load modules, hack!

View solution in original post

0 Karma

dominiquevocat
SplunkTrust
SplunkTrust

You need to extend the python path 😞

        import sys
        sys.path.append(os.path.join(os.environ['SPLUNK_HOME'],'etc','apps','YourAppNameHere','appserver','controllers')) #build local path and add it to the python path so we can load modules, hack!
0 Karma

twesthead
Path Finder

This works, indeed. Thank you for your answer.

0 Karma

dominiquevocat
SplunkTrust
SplunkTrust

Glad it works for you. When its checked by Splunk you can look at what i used it for in https://splunkbase.splunk.com/app/3573/ 🙂

0 Karma

alacercogitatus
SplunkTrust
SplunkTrust

I have a controller in one of my apps. The only real difference I can see is that you did not call the class the same as the filename. Change it, and let me know.

 class my_script(controllers.BaseController):

This should present it self and work. Additionally, you may want to try putting this in web.conf

 [endpoint:my_script]
0 Karma

twesthead
Path Finder

Hi, thank you for your answer.
My controllers already work fine with :
- [endpoint:my_script] in web.conf
- my_script.py as a file name in controllers
- class random_name(controllers.BaseController): in my_script.py

In this Splunk Wiki page, someone shows an example where the class name doesn't match the file name. https://wiki.splunk.com/Community:40GUIDevelopment

What I am trying to achieve is moving some code from controllers to subdirectories.

0 Karma

dominiquevocat
SplunkTrust
SplunkTrust

did you ever manager to get it to run? I need a ready made class and i can not load it inside my controller 😕

0 Karma

twesthead
Path Finder

I still haven't found a solution to my problem which was: keeping the code inside the app structure.
If you don't have this requirement, have you tried putting your ready made class inside a module and inside the following folder?

$SPLUNK_HOME/lib/python2.7/site-packages

0 Karma

dominiquevocat
SplunkTrust
SplunkTrust

actually i managed... will post it as a answer but it is hackish, you can exted the python path... 😕

0 Karma
Get Updates on the Splunk Community!

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...

.conf24 | Personalize your .conf experience with Learning Paths!

Personalize your .conf24 Experience Learning paths allow you to level up your skill sets and dive deeper ...

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...