Installation

My own Python script cannot import Splunk Python library modules?

laiyongmao
Path Finder

My own Python script cannot import Splunk Python library modules?

[root@localhost bin]# ./qqip.py 8.8.8.8
Traceback (most recent call last):
File "./qqip.py", line 162, in
import splunk.Intersplunk as si
ImportError: No module named splunk.Intersplunk

[root@localhost bin]# vim qqip.py

161 import re
162 import splunk.Intersplunk as si
163 ipre = re.compile("\d+.\d+.\d+.\d+")
164 results = []
165 if name == 'main':

[root@localhost bin]# find / -name Intersplunk
/opt/splunk/lib/python2.7/site-packages/splunk/Intersplunk.pyo
/opt/splunk/lib/python2.7/site-packages/splunk/Intersplunk.pyc
/opt/splunk/lib/python2.7/site-packages/splunk/Intersplunk.py

???

1 Solution

Ayn
Legend

Splunk comes bundled with its own Python environment. You could either set your PYTHONPATH in order to make use of the packages available in it, or better yet you should run Splunk's python completely: /opt/splunk/bin/splunk cmd python <yourfile.py>.

View solution in original post

joelurtubia
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/Splunk_SA_Scientific_Python_linux_x86_64/bin/linux_x86_64/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

Ayn
Legend

Splunk comes bundled with its own Python environment. You could either set your PYTHONPATH in order to make use of the packages available in it, or better yet you should run Splunk's python completely: /opt/splunk/bin/splunk cmd python <yourfile.py>.

laiyongmao
Path Finder

Ayn ,Thank you very much!

0 Karma
Get Updates on the Splunk Community!

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...