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!

Why You Can't Miss .conf25: Unleashing the Power of Agentic AI with Splunk & Cisco

The Defining Technology Movement of Our Lifetime The advent of agentic AI is arguably the defining technology ...

Deep Dive into Federated Analytics: Unlocking the Full Power of Your Security Data

In today’s complex digital landscape, security teams face increasing pressure to protect sprawling data across ...

Your summer travels continue with new course releases

Summer in the Northern hemisphere is in full swing, and is often a time to travel and explore. If your summer ...