<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Import python libraries into script - things don't work inside Splunk that work on Command line... in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/Import-python-libraries-into-script-things-don-t-work-inside/m-p/460976#M8389</link>
    <description>&lt;P&gt;My guess you hit this problem here &lt;A href="https://stackoverflow.com/a/33796439"&gt;https://stackoverflow.com/a/33796439&lt;/A&gt; &lt;/P&gt;

&lt;P&gt;cheers, MuS&lt;/P&gt;</description>
    <pubDate>Mon, 25 May 2020 22:21:27 GMT</pubDate>
    <dc:creator>MuS</dc:creator>
    <dc:date>2020-05-25T22:21:27Z</dc:date>
    <item>
      <title>Import python libraries into script - things don't work inside Splunk that work on Command line...</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Import-python-libraries-into-script-things-don-t-work-inside/m-p/460973#M8386</link>
      <description>&lt;P&gt;&lt;STRONG&gt;TL;DR&lt;/STRONG&gt; - the python scripts runing from the &lt;EM&gt;$SPLUNK_HOME$/bin/splunk cmd python&lt;/EM&gt; throw errors that aren't thrown using &lt;EM&gt;/usr/bin/python&lt;/EM&gt;.&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;So, I've got a load of data which has X/Y coordinates stored in NZTM2000 format, and I want to enrich the data by converting to a Lat/Lng so that I can plot onto a map visualisation, or output to other systems that use this format. I've created an app (replaced with in the examples below), and dropped a Python script file in there as a proof of concept.&lt;/P&gt;
&lt;P&gt;The Python script I'm using to convert requires a couple of additional libraries (installed using "&lt;EM&gt;pip install pyproj&lt;/EM&gt;") which I installed as the Splunk user (who's home directory is also set to $SPLUNK_HOME$).&lt;/P&gt;
&lt;P&gt;The problem is that the script works fine from the command line:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;splunk@standalone-vm:~/etc/apps/&amp;lt;appname&amp;gt;/bin$ /usr/bin/python /opt/splunk/etc/apps/&amp;lt;appname&amp;gt;/bin/nztm_to_lat_lng.py
(-40.981429307189785, 174.95992442484228)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, if I try running it using the Splunk binary route, I get an error....&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;splunk@standalone-vm:~/etc/apps/&amp;lt;appname&amp;gt;/bin$ /opt/splunk/bin/splunk cmd python /opt/splunk/etc/apps/&amp;lt;appname&amp;gt;/bin/nztm_to_lat_lng.py
Traceback (most recent call last):
  File "/opt/splunk/etc/apps/&amp;lt;appname&amp;gt;/bin/nztm_to_lat_lng.py", line 14, in &amp;lt;module&amp;gt;
    from pyproj import Proj, transform
  File "/opt/splunk/.local/lib/python2.7/site-packages/pyproj/__init__.py", line 69, in &amp;lt;module&amp;gt;
    from pyproj._list import (  # noqa: F401
ImportError: /opt/splunk/.local/lib/python2.7/site-packages/pyproj/_list.so: undefined symbol: PyUnicodeUCS4_FromStringAndSize
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So I thought it might be something to do with the 2.7 version, but it seems not:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;splunk@standalone-vm:~/etc/apps/&amp;lt;appname&amp;gt;/bin$ /usr/bin/python --version
Python 2.7.17
splunk@standalone-vm:~/etc/apps/&amp;lt;appname&amp;gt;/bin$ /opt/splunk/bin/splunk cmd python --version
Python 2.7.17
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which seems to rule that out. So, I tried a different route:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;splunk@standalone-vm:~/etc/apps/&amp;lt;appname&amp;gt;/bin$ /opt/splunk/bin/splunk cmd /usr/bin/python /opt/splunk/etc/apps/&amp;lt;appname&amp;gt;/bin/nztm_to_lat_lng.py
(-40.981429307189785, 174.95992442484228)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So if I get Splunk to call the /usr/bin/python route, everything works as expected. So this points to a problem with the shipped version of python shipped with Splunk, but that seems.... Unlikely.&lt;/P&gt;
&lt;P&gt;So, help, please?&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;Appendix: Here's a copy of the python file I'm trying to run (Note: you'll need the pyproj libary too...)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;#!/usr/bin/env python

"""nztm_to_lat_lng.py: Takes an NZTM2000 X/Y coordinate system and outputs a lat/lng"""
__author__     = "Phil Tanner"
__version__    = "0.0.1"
__status__     = "Prototype"

# If we're running from Splunk command, patch our local path so we can load modules; hack!
import os
splunk_home = os.getenv('SPLUNK_HOME')
if splunk_home is not None:
  import sys
  # Local app path
  sys.path.append(os.path.join(os.environ['SPLUNK_HOME'],'etc','apps','&amp;lt;appname&amp;gt;','bin'))
  # Splunk user path - note this is fixed to the 2.7 to pull in the pip installed pyproj files, without which it fails horribly
  sys.path.append(os.path.join(os.environ['SPLUNK_HOME'],'.local','lib','python2.7','site-packages'))

from pyproj import Proj, transform
inProj = Proj("+proj=tmerc +lat_0=0 +lon_0=173 +k=0.9996 +x_0=1600000 +y_0=10000000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs")
outProj = Proj('epsg:4326')

# Note: hardcoded sample, literally chosen at random!
x1,y1 = 1764883.4618,5461454.6101
# Convert between NZTM &amp;amp; Lat/Lng
x2,y2 = transform(inProj,outProj,x1,y1)

print (x2,y2)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And for reference, here's the Linux version I'm running the server on (which is the standard MS Azure Splunk instance):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;splunk@standalone-vm:~/etc/apps/&amp;lt;appname&amp;gt;/bin$ uname -a
Linux standalone-vm 4.15.0-96-generic #97-Ubuntu SMP Wed Apr 1 03:25:46 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Jun 2020 18:08:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Import-python-libraries-into-script-things-don-t-work-inside/m-p/460973#M8386</guid>
      <dc:creator>phil__tanner</dc:creator>
      <dc:date>2020-06-07T18:08:42Z</dc:date>
    </item>
    <item>
      <title>Re: Import python libraries into script - things don't work inside Splunk that work on Command line...</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Import-python-libraries-into-script-things-don-t-work-inside/m-p/460974#M8387</link>
      <description>&lt;P&gt;Hi phil__tanner,&lt;/P&gt;

&lt;P&gt;from what I experienced so far it's the best, easiest to put the module into the &lt;CODE&gt;$SPLUNK_HOME/etc/apps/&amp;lt;appname&amp;gt;/bin&lt;/CODE&gt; directory and import it from there.&lt;/P&gt;

&lt;P&gt;Hope this helps ...&lt;/P&gt;

&lt;P&gt;cheers, MuS&lt;/P&gt;

&lt;P&gt;PS: Greetings up the coast &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; &lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 05:50:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Import-python-libraries-into-script-things-don-t-work-inside/m-p/460974#M8387</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2020-05-21T05:50:34Z</dc:date>
    </item>
    <item>
      <title>Re: Import python libraries into script - things don't work inside Splunk that work on Command line...</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Import-python-libraries-into-script-things-don-t-work-inside/m-p/460975#M8388</link>
      <description>&lt;P&gt;Hey MuS. &lt;/P&gt;

&lt;P&gt;Tried replying last night, got lots of 500 errors. &lt;/P&gt;

&lt;P&gt;I should've mentioned, I tried copying my repos gained from  &lt;CODE&gt;~splunk/.local/lib/python2.7/site-packages/&lt;/CODE&gt; into the  &lt;CODE&gt;&amp;lt;appname&amp;gt;/bin&lt;/CODE&gt; directory too. Gave me the same error as bringing them in via the path (unsurprisingly) - i.e.:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Traceback (most recent call last):
  File "/opt/splunk/etc/apps/&amp;lt;appname&amp;gt;/bin/nztm_to_lat_lng.py", line 8, in &amp;lt;module&amp;gt;
    from pyproj import Proj, transform
  File "/opt/splunk/etc/apps/&amp;lt;appname&amp;gt;/bin/pyproj/__init__.py", line 69, in &amp;lt;module&amp;gt;
    from pyproj._list import (  # noqa: F401
ImportError: /opt/splunk/etc/apps/&amp;lt;appname&amp;gt;/bin/pyproj/_list.so: undefined symbol: PyUnicodeUCS4_FromStringAndSize
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I had thought it might be an incompatibility (32/64 bit?) between the python shipped with Splunk &amp;amp; the python on the server which pip is compiling packages against. But the output of these commands seems to indicate a 64bit running in each case:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;splunk@standalone-vm:~$ /usr/bin/python -c 'import sys;print("%x" % sys.maxsize, sys.maxsize &amp;gt; 2**32)'
('7fffffffffffffff', True)
splunk@standalone-vm:~$ /opt/splunk/bin/splunk cmd python -c 'import sys;print("%x" % sys.maxsize, sys.maxsize &amp;gt; 2**32)'
('7fffffffffffffff', True)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;(Test results based on advice in this post: &lt;A href="https://stackoverflow.com/questions/1405913/how-do-i-determine-if-my-python-shell-is-executing-in-32bit-or-64bit-mode-on-os"&gt;https://stackoverflow.com/questions/1405913/how-do-i-determine-if-my-python-shell-is-executing-in-32bit-or-64bit-mode-on-os&lt;/A&gt; )&lt;/P&gt;

&lt;P&gt;Thanks, Phil&lt;/P&gt;

&lt;P&gt;PS: Greetings from the flat lands into the hills &amp;amp; valleys the Bay.&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 20:56:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Import-python-libraries-into-script-things-don-t-work-inside/m-p/460975#M8388</guid>
      <dc:creator>phil__tanner</dc:creator>
      <dc:date>2020-05-21T20:56:05Z</dc:date>
    </item>
    <item>
      <title>Re: Import python libraries into script - things don't work inside Splunk that work on Command line...</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Import-python-libraries-into-script-things-don-t-work-inside/m-p/460976#M8389</link>
      <description>&lt;P&gt;My guess you hit this problem here &lt;A href="https://stackoverflow.com/a/33796439"&gt;https://stackoverflow.com/a/33796439&lt;/A&gt; &lt;/P&gt;

&lt;P&gt;cheers, MuS&lt;/P&gt;</description>
      <pubDate>Mon, 25 May 2020 22:21:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Import-python-libraries-into-script-things-don-t-work-inside/m-p/460976#M8389</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2020-05-25T22:21:27Z</dc:date>
    </item>
    <item>
      <title>Re: Import python libraries into script - things don't work inside Splunk that work on Command line...</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Import-python-libraries-into-script-things-don-t-work-inside/m-p/460977#M8390</link>
      <description>&lt;P&gt;Yeah, I ended up at that same place. &lt;/P&gt;

&lt;P&gt;Does this mean that I should be replacing the python binary in &lt;CODE&gt;$SPLUNK_HOME$/bin/python&lt;/CODE&gt; with a differently compiled version? Because the pip install I ran as the &lt;CODE&gt;splunk&lt;/CODE&gt; user clearly built against the OS system of python, not the Splunk version, which is where the conflict first originated, and Splunk doesn't seem to ship with a &lt;CODE&gt;pip&lt;/CODE&gt; version I could compile against....&lt;/P&gt;

&lt;P&gt;Any which way, it looks like this isn't going to be a "upgrade proof" solution? &lt;/P&gt;</description>
      <pubDate>Tue, 26 May 2020 00:21:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Import-python-libraries-into-script-things-don-t-work-inside/m-p/460977#M8390</guid>
      <dc:creator>phil__tanner</dc:creator>
      <dc:date>2020-05-26T00:21:50Z</dc:date>
    </item>
    <item>
      <title>Re: Import python libraries into script - things don't work inside Splunk that work on Command line...</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Import-python-libraries-into-script-things-don-t-work-inside/m-p/460978#M8391</link>
      <description>&lt;P&gt;I would not recommend to replace the shipped python that will result in a lot of tears &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; &lt;BR /&gt;
Like you said, anything you build will not be upgrade proof&lt;/P&gt;

&lt;P&gt;cheers, MuS&lt;/P&gt;</description>
      <pubDate>Tue, 26 May 2020 01:04:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Import-python-libraries-into-script-things-don-t-work-inside/m-p/460978#M8391</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2020-05-26T01:04:21Z</dc:date>
    </item>
    <item>
      <title>Re: Import python libraries into script - things don't work inside Splunk that work on Command line.</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Import-python-libraries-into-script-things-don-t-work-inside/m-p/503807#M8392</link>
      <description>&lt;P&gt;You could try installing the package locally to the script to see if that makes a difference?&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;pip install -t . pyproj&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 10 Jun 2020 21:54:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Import-python-libraries-into-script-things-don-t-work-inside/m-p/503807#M8392</guid>
      <dc:creator>livehybrid</dc:creator>
      <dc:date>2020-06-10T21:54:47Z</dc:date>
    </item>
  </channel>
</rss>

