Developing for Splunk Enterprise
Highlighted

How can I add a Python modules to the Splunk Django environment?

Explorer

I need to add a Python module that is not included in the Splunk python. I want to use this module in Django framework, but the answers in this previous question do not satisfy my requirement:
https://answers.splunk.com/answers/8/can-i-add-python-modules-to-the-splunk-environment.html

any help

Labels (2)
Highlighted

Re: How can I add Python modules to the Splunk environment?

Path Finder

You have a few options:

  • simply add the library locally to the bin folder of your app that will hold your scripted input (or $SPLUNK_HOME/bin/scripts if a script). Local imports will always work
  • If it needs to be global, add to $SPLUNK_HOME/lib/python2.7/site-packages, although that might be harder/less friendly to keep up to date between upgrades/different environments.

My recommendation would be the first option. All you need to do is place your library in the same folder, and then you can simply call import from your script. import always searches the local folder in addition to any system paths.

0 Karma
Highlighted

Re: How can I add Python modules to the Splunk environment?

Explorer

i want to add numpy module to python in splunk ,
i follow the frist option and get the /usr/lib/python2.7/dist-package/numpy
to the bin folder of my project but it still not work .

0 Karma
Highlighted

Re: How can I add Python modules to the Splunk environment?

Path Finder

Ah...numpy is a little more intricate. New answer forthcoming

0 Karma
Highlighted

Re: How can I add Python modules to the Splunk environment?

Path Finder

To add numpy, scipy, pandas, scikit-learn, or statsmodels, there's an app on Splunkbase you can install - Python for Scientific Computing. You need to use the correct version for your OS, as many of these libraries have OS specific dependencies. Here's the Linux 64 bit link: https://splunkbase.splunk.com/app/2882/. The README in the app contains instructions on how to import the libraries into other scripts once that app is installed.

View solution in original post

0 Karma
Highlighted

Re: How can I add Python modules to the Splunk environment?

Explorer

thanks for your answer .
do you have any idea for matlab module

0 Karma
Highlighted

Re: How can I add Python modules to the Splunk environment?

Explorer

Hello, I was the same problem with Mysql module that I was install on my Centos server

Splunk didn't work with this library, because splunk has they own python library...then you can fix it only added on the begin your script all libraries of python and also you must to add the python Centos library too... as this way

Find python packages

[root@xxxx]#find / -name site-packages
/usr/lib/python2.7/site-packages
/usr/lib64/python2.7/site-packages
/opt/splunk/etc/apps/SplunkSAScientificPythonlinuxx8664/bin/linuxx8664/lib/python2.7/site-packages
/opt/splunk/lib/python2.7/site-packages

Find python binary

[root@xxxx]# whereis python
python: /usr/bin/python2.7 /usr/bin/python /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /opt/splunk/bin/python /opt/splunk/bin/python2.7 /usr/share/man/man1/python.1.gz

include all at begin your script

import sys
sys.path.append('/usr/bin/python2.7')
sys.path.append('/usr/lib/python2.7/site-packages')
sys.path.append('/usr/lib64/python2.7/site-packages')

And that's it , you can run mysql module without any problem and create your alerts with this module.

Mysql Connection

import mysql.connector

I hope that this fix will help you
Joel Urtubia Ugarte

0 Karma
Highlighted

Re: How can I add Python modules to the Splunk environment?

New Member
  1. Go to your local python folder apart from Splunk and copy the scripts related to pip (e.g. pip-install, easy-pip etc.)
  2. Paste all these scripts in Splunk site-packages “C:\Program Files\Splunk\Python-2.7\Lib\site-packages”
  3. Then open command prompt as an administrator and cd to this location “C:\Program Files\Splunk\Python-2.7\Lib\site-packages” and run pip install module name -t ” C:\Program Files\Splunk\Python-2.7\Lib\site-packages”
  4. Then the specified library/module will start working in the Splunk
0 Karma