<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to import Python module from subdirectory in appserver/controllers? in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265441#M3350</link>
    <description>&lt;P&gt;did you ever manager to get it to run? I need a ready made class and i can not load it inside my controller &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 16 Apr 2017 15:44:07 GMT</pubDate>
    <dc:creator>dominiquevocat</dc:creator>
    <dc:date>2017-04-16T15:44:07Z</dc:date>
    <item>
      <title>How to import Python module from subdirectory in appserver/controllers?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265438#M3347</link>
      <description>&lt;P&gt;Dear Splunk,&lt;/P&gt;

&lt;P&gt;I have quite a few custom endpoints in my web.conf file.&lt;BR /&gt;
This leads to many Python scripts in my appserver/controllers directory and many helper scripts for functions used by several endpoints.&lt;BR /&gt;
I would like to tidy up this directory by putting some of the scripts in local packages or subdirectories.&lt;BR /&gt;
As an example, here's my file structure:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;appserver
---&amp;gt;controllers
-------&amp;gt;my_script.py
-------&amp;gt;mylib
----------&amp;gt;mymod.py
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;My code:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;#mymod.py
def scream():
    return 'YEAH!'
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;HR /&gt;

&lt;PRE&gt;&lt;CODE&gt;#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()
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now, if I add a empty &lt;CODE&gt;__init__.py&lt;/CODE&gt; file to &lt;CODE&gt;/mylib&lt;/CODE&gt;, &lt;CODE&gt;mylib&lt;/CODE&gt; behaves like a package, and if I test the script locally with splunk's python interpreter in CLI: &lt;CODE&gt;splunk cmd python my_script.py&lt;/CODE&gt; , I am able to import to import my module and use the scream() function from my_script.py.&lt;/P&gt;

&lt;P&gt;However, the import doesn't work one I try to run the script through the web service: &lt;CODE&gt;&lt;A href="http://localhost:8000/en-US/custom/my_app/my_script/scream" target="test_blank"&gt;http://localhost:8000/en-US/custom/my_app/my_script/scream&lt;/A&gt;&lt;/CODE&gt; throws a 404 Error.&lt;/P&gt;

&lt;P&gt;What's the Splunk-approved way of organizing code in my &lt;CODE&gt;controllers&lt;/CODE&gt; directory ?&lt;/P&gt;</description>
      <pubDate>Tue, 06 Dec 2016 12:01:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265438#M3347</guid>
      <dc:creator>twesthead</dc:creator>
      <dc:date>2016-12-06T12:01:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to import Python module from subdirectory in appserver/controllers?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265439#M3348</link>
      <description>&lt;P&gt;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.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; class my_script(controllers.BaseController):
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This should present it self and work. Additionally, you may want to try putting this in &lt;CODE&gt;web.conf&lt;/CODE&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; [endpoint:my_script]
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Dec 2016 15:03:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265439#M3348</guid>
      <dc:creator>alacercogitatus</dc:creator>
      <dc:date>2016-12-06T15:03:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to import Python module from subdirectory in appserver/controllers?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265440#M3349</link>
      <description>&lt;P&gt;Hi, thank you for your answer.&lt;BR /&gt;
My controllers already work fine with :&lt;BR /&gt;
- &lt;CODE&gt;[endpoint:my_script]&lt;/CODE&gt; in &lt;CODE&gt;web.conf&lt;/CODE&gt;&lt;BR /&gt;
- &lt;CODE&gt;my_script.py&lt;/CODE&gt; as a file name in &lt;CODE&gt;controllers&lt;/CODE&gt;&lt;BR /&gt;
- &lt;CODE&gt;class random_name(controllers.BaseController):&lt;/CODE&gt; in &lt;CODE&gt;my_script.py&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;In this Splunk Wiki page, someone shows an example where the class name doesn't match the file name. &lt;A href="https://wiki.splunk.com/Community:40GUIDevelopment"&gt;https://wiki.splunk.com/Community:40GUIDevelopment&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;What I am trying to achieve is moving some code from &lt;CODE&gt;controllers&lt;/CODE&gt; to subdirectories.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Dec 2016 15:31:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265440#M3349</guid>
      <dc:creator>twesthead</dc:creator>
      <dc:date>2016-12-06T15:31:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to import Python module from subdirectory in appserver/controllers?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265441#M3350</link>
      <description>&lt;P&gt;did you ever manager to get it to run? I need a ready made class and i can not load it inside my controller &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 16 Apr 2017 15:44:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265441#M3350</guid>
      <dc:creator>dominiquevocat</dc:creator>
      <dc:date>2017-04-16T15:44:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to import Python module from subdirectory in appserver/controllers?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265442#M3351</link>
      <description>&lt;P&gt;I still haven't found a solution to my problem which was: keeping the code inside the app structure.&lt;BR /&gt;
If you don't have this requirement, have you tried putting your ready made class inside a module and inside the following folder?&lt;/P&gt;

&lt;P&gt;$SPLUNK_HOME/lib/python2.7/site-packages &lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2017 07:46:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265442#M3351</guid>
      <dc:creator>twesthead</dc:creator>
      <dc:date>2017-04-18T07:46:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to import Python module from subdirectory in appserver/controllers?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265443#M3352</link>
      <description>&lt;P&gt;actually i managed... will post it as a answer but it is hackish, you can exted the python path... &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2017 08:34:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265443#M3352</guid>
      <dc:creator>dominiquevocat</dc:creator>
      <dc:date>2017-04-18T08:34:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to import Python module from subdirectory in appserver/controllers?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265444#M3353</link>
      <description>&lt;P&gt;You need to extend the python path &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;        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!
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 Apr 2017 08:35:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265444#M3353</guid>
      <dc:creator>dominiquevocat</dc:creator>
      <dc:date>2017-04-18T08:35:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to import Python module from subdirectory in appserver/controllers?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265445#M3354</link>
      <description>&lt;P&gt;This works, indeed. Thank you for your answer.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2017 09:02:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265445#M3354</guid>
      <dc:creator>twesthead</dc:creator>
      <dc:date>2017-04-18T09:02:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to import Python module from subdirectory in appserver/controllers?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265446#M3355</link>
      <description>&lt;P&gt;Glad it works for you. When its checked by Splunk you can look at what i used it for in &lt;A href="https://splunkbase.splunk.com/app/3573/"&gt;https://splunkbase.splunk.com/app/3573/&lt;/A&gt; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2017 15:11:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-import-Python-module-from-subdirectory-in-appserver/m-p/265446#M3355</guid>
      <dc:creator>dominiquevocat</dc:creator>
      <dc:date>2017-05-02T15:11:27Z</dc:date>
    </item>
  </channel>
</rss>

