【问题标题】:Running a Julia file in a Python Script在 Python 脚本中运行 Julia 文件
【发布时间】:2020-10-28 14:25:24
【问题描述】:

我想在 Python 脚本中运行 Julia 文件。 Julia 文件是

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

这段代码在 Atom 中运行良好。但我需要它在 Python 脚本中工作。 我的 Python 脚本是:

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

给出以下错误:

FileNotFoundError

我尝试将 Julia 文件放在多个文件夹中,但它总是给出相同的信息。 如果有人可以帮助我,我将不胜感激。

【问题讨论】:

    标签: python julia


    【解决方案1】:

    您的 python 解释器可能没有在您期望的位置查找文件。尝试在 python 中运行以下命令。

    import os
    print(os.getcwd())
    

    这会告诉你 python 从哪里开始寻找文件。如果你把你的 julia 文件放在那里,你的代码应该可以工作。您也可以运行os.chdir(os.path.join('path', 'to', 'directory', 'containing', 'julia', 'file'))j.include(os.path.join('absolute', 'path', 'to', 'func1.jl'))

    如果您使用 Hydrogen 在 Atom 中运行 Python 代码,您可能需要查看 how to change where the Python interpreter starts

    【讨论】:

    • @emerson 没有用。它一直给出同样的错误信息。
    • @RitadeCassiaSouzaPaz 我只能建议您非常仔细地检查os.getcwd() 的输出,并确保所有内容都拼写正确。如果您认为您知道包含func1.jl 的目录的路径,则可以使用os.listdir('path/to/dir') 来确保该文件确实存在。祝你好运!
    【解决方案2】:

    我能够通过将路径放在系统变量中并在 Python 中使用以下代码来解决这个问题:

    进口朱莉娅 从 julia.api 导入 Julia

    julia.install()

    jl = Julia(compiled_modules=False)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-19
      • 2020-09-14
      • 1970-01-01
      • 2012-01-24
      • 2015-09-03
      • 2017-10-27
      相关资源
      最近更新 更多