How to write code in Julia from Python

Hi

I am struggling with my first script with Julia.

In Python I am using this piece of code to have the PATH of ‘splunk -sdk-python’.

import sys, os

pa = os.path.join(os.path.abspath(os.path.join('', os.pardir)), "splunk-sdk-python")
sys.path.insert(0, pa)
print(pa)

How can I do that with Julia?

I don’t really understand what you would like to achieve with this code in Julia, but

pa = abspath(joinpath("..","splank-sdk-python"))

gives me the same result as your python code for pa.
( “…/splank-sdk-python” expanded relative to the current working directory)

Well, my problem in Python, is that I need to know the PATH of the SDK in order to use it.
I am using the SDK to have access to the data which is inside a database. Once I have the data I want to use Julia.

How do I import “sys” and “os” (which are from Python) into Julia?
With py"""

I’m confused, maybe I’m missing something basic here but it is my (possibly incorrect) understanding that your python code

pa = os.path.join(os.path.abspath(os.path.join('', os.pardir)), "splunk-sdk-python")

just returns the expanded version of “…/splunk-sdk-python” irregardless of whether this is the path of “splunk-sdk-python”. What am I missing?

anyway if you want to call some python code and import some modules you can do that with PyCall.jl, have a look at the README.

Hi again,

pa = os.path.join(os.path.abspath(os.path.join(‘’, os.pardir)), “splunk-sdk-python”)
sys.path.insert(0, pa)

How do I do this into Julia?

sys.path.insert(0, pa)

great, good luck!

How do I do this into Julia?

sys.path.insert(0, pa)

With PyCall?

I assume you could add something to the python path using PyCall, but I am afraid I again don’t understand what you want to do.
It would be greatly helpful if you could describe as clearly as possible what is it exactly that you are trying to do so people would be able to recommend the best way to do that in Julia.
PyCall is what you need to run python code from Julia. If your goal is to call some python function from this splunk sdk you should indeed use PyCall.

Ok

“splunk-sdk-python” is in this PATH: ~/splunk-sdk-python
My script is in this PATH: ~/roc/my_script.py

When I execute it, it says that it cannot find the “splunklib”. So with that piece of code I am giving the PATH where the SDK is located.

All the python code is:

import sys, os

pa = os.path.join(os.path.abspath(os.path.join('', os.pardir)), "splunk-sdk-python")
sys.path.insert(0, pa)


import splunklib.client as client

HOST = "localhost"
PORT = 8089
USERNAME = "admin"
PASSWORD = "passwdSPLUNK"

# Create a Service instance and log in 
service = client.connect(
    host=HOST,
    port=PORT,
    username=USERNAME,
    password=PASSWORD)

# Print installed apps to the console to verify login
for app in service.apps:
    print (app.name)

I guess I have to use a push! (LOAD_PATH, string(@__DIR__) or something like that.

using PyCall
pyimport("sys")."path".insert(0, pa)

(using ."path" instead of .path is to prevent it from being converted to a Julia array.)

2 Likes