DEV Community

Cover image for Learn Julia (14): calling python modules
jemaloQiu
jemaloQiu

Posted on

Learn Julia (14): calling python modules

Julia is a very young language compared to Python or Matlab. Thus its ecosystem is not yet as large as that of Python or Matlab. It shall be a very exciting idea for Julia to be able to import existing Python modules and functions, isn't it?

Today I am glad to have successfully fulfilled some graph database related work in Julia by importing some Python modules. So here I will present step by step what I have done just now.

install pkg PyCall

using pkg
Pkg.add("PyCall")

Enter fullscreen mode Exit fullscreen mode

use PyCall with a correct Python env

I am using Anaconda for Python development. And there are three different Python versions on my machine which are managed by Conda envs. So I need to specify the correct Python path in julia ENV parameter and then re-build PyCall.

ENV["PYTHON"] = "D:\\Anaconda3\\envs\\py36\\python.exe"
Pkg.build("PyCall")
using PyCall
Enter fullscreen mode Exit fullscreen mode

Alt Text

import some modules

pyneo = pyimport("py2neo")
pyjson = pyimport("json")
Enter fullscreen mode Exit fullscreen mode

Data preparation

I faked a data file "data.json" with several french expressions. This file contains three french expression objects and each of them has "id","fr","zh" and "category" properties. The content of this file is as follows:

[
    {
        "id": 1,
        "fr": "Tu me dégoutes!",
        "zh": "我讨厌你! ",
        "category": "plainte"
    }, {
        "id": 2,
        "fr": "Je veux plus jamais te voir !",
        "zh": "我不愿再见到你! ",
        "category": "plainte"
    }, {
        "id": 3,
        "fr": "T'es dingue !",
        "zh": "你疯了! ",
        "category": "plainte"
    }
]
Enter fullscreen mode Exit fullscreen mode
datafile = open("./data.json", "r");
ss = read("data.json", String);
print(ss)
Enter fullscreen mode Exit fullscreen mode

Alt Text

Now try to parse the file content as a json object using imported json module.

d = pyjson.loads(ss)
println("d: $d")
Enter fullscreen mode Exit fullscreen mode

It worked thus I can use Pyhton's json module for parsing json-format string (I guess there shall still be issues when the string format is not clean, to be tested in future).
Alt Text

call Python neo4j database and test it

I use Py2neo module to connect to my local Neo4j database. And test it with a simple Cypher command. It works. However, I encounter an error while trying to create a Node in the graph. The type of a Node object has been converted into Dict in Julia.

graph = pyneo.Graph("http://localhost:7474",username="neo4j",password="123456")

res = graph.run("match (n) return n limit 10")
print(collect(res))

n = pyneo.Node("testNode", name="test") 
println(typeof(n))

graph.create(n)  # create nodes in the graph db
Enter fullscreen mode Exit fullscreen mode

Output:
Alt Text

Create nodes using Cypher

Since the Cypher execution works properly, thus I have tried executing cmd create (n:expression) set n.id=$id set n.fr=\"$fr\" set n.zh=\"$zh\" return n for each item of the dict generated from json file. It works and I can see the expressions in the Graph.

for i in d
    id = i["id"]
    fr = i["fr"]
    zh = i["zh"]
    res = graph.run("create (n:expression) set n.id=$id set n.fr=\"$fr\" set n.zh=\"$zh\" return n ")
    println(id, collect(res))
end
Enter fullscreen mode Exit fullscreen mode

Console output:
Alt Text

Neo4j snapshot:
Alt Text

Discussion (2)

Collapse
chrisgreening profile image
Chris Greening

I'm really excited to start working with Julia lol, I've had the interpreter on my computer for like a year now but haven't had the time to really dive into it. Maybe this will be the inspiration I needed to finally sit down and learn

Collapse
jemaloqiu profile image
jemaloQiu Author

Welcome! It's a promising language, may rise among the top 3 languages in the future. Feeling glad to be helpful. Good luck and enjoy the learning.