2

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.

Bram Dekker
  • 631
  • 1
  • 7
  • 19

2 Answers2

2

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.

Emerson Harkin
  • 889
  • 5
  • 13
  • @emersonDid not work. It keeps giving the same error message. – Rita de Cassia Souza Paz Oct 28 '20 at 14:55
  • @RitadeCassiaSouzaPaz I can only suggest that you check the output of `os.getcwd()` very carefully and make sure that everything is spelled correctly. If you think you know the path to the directory containing `func1.jl`, you can use `os.listdir('path/to/dir')` to make sure the file is really there. Good luck! – Emerson Harkin Oct 28 '20 at 15:07
0

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')