Running a Julia file in a Python Script

I want to run a Julia file inside a Python Script. The Julia file is

func1.jl

using LowRankApprox

using LinearAlgebra

function f(A)

    F = pqrfact(A)

    M = A * F[:P]

    return M

end

function k(A)

    F = pqrfact(A)

    k = F[:k]

    return k

end

This code is working perfectly in Atom. But I need it to work inside a Python Script. My Python Script is:

import numpy as np

import julia

j = julia.Julia()

j.include("func1.jl")

A = np.array([[1, 3, 1], [1, 1, 1], [2, 1, 1]])

print(j.k(A))

Gives the following error:

FileNotFoundError

I've tried to put the Julia file in several folders but it always gives the same message. If anyone can help me I will be very grateful.

Upvotes: 2

Views: 446

Answers (2)

I was able to solve this problem by placing the path in the system variable and with the following code in Python:

import julia from julia.api import Julia

julia.install()

jl = Julia(compiled_modules=False)

j = julia.Julia() j.include('func1.jl')

Upvotes: 0

Emerson Harkin
Emerson Harkin

Reputation: 917

Your python interpreter is probably not looking for files in the place you expect. Try running the following in python.

import os
print(os.getcwd())

This will tell you where python starts looking for files. If you put your julia file there, your code should work. You can also run os.chdir(os.path.join('path', 'to', 'directory', 'containing', 'julia', 'file')), or j.include(os.path.join('absolute', 'path', 'to', 'func1.jl')).

If you're using Hydrogen to run Python code in Atom, you might want to check out how to change where the Python interpreter starts.

Upvotes: 2

Related Questions