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>.

ccWildcard
Explorer

is ended up solving a major problem for me using Ansible to setup Splunk and running python scripts inside the Ansible playbook. Thank you so much!

0 Karma

laiyongmao
Path Finder

Ayn ,Thank you very much!

0 Karma
Get Updates on the Splunk Community!

Data Management Digest – December 2025

Welcome to the December edition of Data Management Digest! As we continue our journey of data innovation, the ...

Index This | What is broken 80% of the time by February?

December 2025 Edition   Hayyy Splunk Education Enthusiasts and the Eternally Curious!    We’re back with this ...

Unlock Faster Time-to-Value on Edge and Ingest Processor with New SPL2 Pipeline ...

Hello Splunk Community,   We're thrilled to share an exciting update that will help you manage your data more ...