Splunk Dev

Can I add python modules to the Splunk environment?

Simeon
Splunk Employee
Splunk Employee

How can I add a python module that is not included in the Splunk python bundle? Specifically, I would like to use the pymssql module from within Splunk to run a scripted input.

Labels (1)
Tags (3)
1 Solution

brwalker_
Community Manager
Community Manager
According to the documentation you should package dependencies your app needs in its /<appname>/bin directory.

View solution in original post

0 Karma

jrodman
Splunk Employee
Splunk Employee

No, there's no way to tell splunk to run some other python. However, you can make the script itself do this as per amrit's comment.

0 Karma

dianbo
New Member

i have 3 pythons in my linux server, local 2.3, 2.5 and 2.6(in splunk4.0.8). I can not install MySQL-python-1.2.3c1 to 2.6, but it's ok to install it to local 2.3. the following is the detail.

cd MySQL-python-1.2.3c1

python2.6 setup.py build ImportError: No module named setuptools

sh setuptools-0.6c11-py2.6.egg ImportError: No module named command.bdist

[root@localhost bin]# ./splunk cmd /local/dl/setuptools-0.6c11-py2.6.egg Traceback (most recent call last): File "", line 1, in File "/local/dl/setuptools-0.6c11-py2.6.egg/setuptools/init.py", line 2, in File "/local/dl/setuptools-0.6c11-py2.6.egg/setuptools/extension.py", line 2, in File "/local/dl/setuptools-0.6c11-py2.6.egg/setuptools/dist.py", line 5, in File "/local/dl/setuptools-0.6c11-py2.6.egg/setuptools/command/init.py", line 13, in ImportError: No module named command.bdist

[root@localhost bin]# ./splunk cmd /local/dl/MySQL_python-1.2.3c1-py2.6-linux-i686.egg couldn't run "/local/dl/MySQL_python-1.2.3c1-py2.6-linux-i686.egg": Exec format error

@the_wolverine, how to tell splunk to execute lookup python script with system's Python?

Thanks,

Dianbo

0 Karma

amrit
Splunk Employee
Splunk Employee

There's also a more upgrade-friendly way to accomplish this. Some of our users setup whichever script they've configured in Splunk as a pass-through to a script that runs using their system Python (with whichever custom modules they've installed).

The steps are roughly:

  • configure your script in splunk (search script, scripted input, whatever)

  • this script should:

    • unset PYTHONPATH (in os.environ)

    • perhaps unset LD_LIBRARY_PATH, depending on your environment (also in os.environ)

    • create a process to run /usr/bin/python (via subprocess)

    • redirect stdin, stdout, stderr to/from script2

script2 can then load any arbitrary python module installed in your system's python installation.

Dark_Ichigo
Builder

Is there a step by step tutorial to accomplish this on a Linux environment?

bmacias84
Champion

As another option I would checkout this post on how to add egg file or source files while still using Splunk python rather than system. http://answers.splunk.com/answers/220196/import-non-native-python-libraries-into-splunk.html#answer-...

0 Karma

Johnvey
Contributor

Users are free to install any python module they desire. The caveats are, 1) upgrading Splunk may break them, 2) installing newer versions of packages that come with Splunk may produce unknown interaction problems.

To install a python package in splunk:

$ splunk cmd <python_install_command>

So if the package uses the setup.py method:

$ cd path_to_package_setup
$ splunk cmd python setup.py install

Or if it's an egg,

$ splunk cmd ./my-python-installer.egg

The final location of the installed modules would be:

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

wollinet
Path Finder

Is that still supported with 4.1 ? I tried both ways, first one prouced errors, second one didn't produce any output and nothing was installed.

0 Karma

igor
Splunk Employee
Splunk Employee

Just use the standard way of installing modules (make sure that splunk and python are in path):
Untar pymssql-1.0.2.tar.gz
cd pymssql-1.0.2
splunk cmd python setup.py install

Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...