# How to write code in Julia from Python

**URL:** <https://discourse.julialang.org/t/how-to-write-code-in-julia-from-python/34214>\
**Category:** General Usage\
**Created:** [February 5, 2020, 5:28pm UTC](https://discourse.julialang.org/t/how-to-write-code-in-julia-from-python/34214 "2020-02-05T17:28:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)\
**Post date:** [February 5, 2020, 5:28pm UTC](https://discourse.julialang.org/t/how-to-write-code-in-julia-from-python/34214/1 "2020-02-05T17:28:59Z")

</div>

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

```julia
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?

---

<div class="post-metadata">

**Author:** ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)\
**Post date:** [February 5, 2020, 6:30pm UTC](https://discourse.julialang.org/t/how-to-write-code-in-julia-from-python/34214/2 "2020-02-05T18:30:43Z")

</div>

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

```julia
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)

---

<div class="post-metadata">

**Author:** ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)\
**Post date:** [February 5, 2020, 6:37pm UTC](https://discourse.julialang.org/t/how-to-write-code-in-julia-from-python/34214/3 "2020-02-05T18:37:07Z")

</div>

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"""`

---

<div class="post-metadata">

**Author:** ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)\
**Post date:** [February 5, 2020, 6:42pm UTC](https://discourse.julialang.org/t/how-to-write-code-in-julia-from-python/34214/4 "2020-02-05T18:42:45Z")

</div>

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

```julia
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?

---

<div class="post-metadata">

**Author:** ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)\
**Post date:** [February 5, 2020, 6:49pm UTC](https://discourse.julialang.org/t/how-to-write-code-in-julia-from-python/34214/5 "2020-02-05T18:49:53Z")

</div>

anyway if you want to call some python code and import some modules you can do that with [PyCall.jl](https://github.com/JuliaPy/PyCall.jl), have a look at the README.

---

<div class="post-metadata">

**Author:** ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)\
**Post date:** [February 5, 2020, 7:00pm UTC](https://discourse.julialang.org/t/how-to-write-code-in-julia-from-python/34214/6 "2020-02-05T19:00:58Z")

</div>

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?

```julia
sys.path.insert(0, pa)

```

---

<div class="post-metadata">

**Author:** ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)\
**Post date:** [February 5, 2020, 7:05pm UTC](https://discourse.julialang.org/t/how-to-write-code-in-julia-from-python/34214/7 "2020-02-05T19:05:11Z")

</div>

great, good luck!

---

<div class="post-metadata">

**Author:** ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)\
**Post date:** [February 5, 2020, 7:07pm UTC](https://discourse.julialang.org/t/how-to-write-code-in-julia-from-python/34214/8 "2020-02-05T19:07:26Z")

</div>

How do I do this into Julia?

```julia
sys.path.insert(0, pa)

```

With PyCall?

---

<div class="post-metadata">

**Author:** ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)\
**Post date:** [February 5, 2020, 7:13pm UTC](https://discourse.julialang.org/t/how-to-write-code-in-julia-from-python/34214/9 "2020-02-05T19:13:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)\
**Post date:** [February 5, 2020, 7:22pm UTC](https://discourse.julialang.org/t/how-to-write-code-in-julia-from-python/34214/10 "2020-02-05T19:22:41Z")

</div>

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:

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

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 5, 2020, 7:34pm UTC](https://discourse.julialang.org/t/how-to-write-code-in-julia-from-python/34214/11 "2020-02-05T19:34:48Z")

</div>

> [@Aizzaac](#):
>
> How do I do this into Julia? `sys.path.insert(0, pa)` With PyCall?

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

```

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