从R或Python运行.jl文件 [英] Running .jl file from R or Python

查看:55
本文介绍了从R或Python运行.jl文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Julia的新手.我开发了几行代码,以从无法在Python或R中找到的包中获得所需的结果.现在,我试图使此文件易于访问,并将代码包装在Python或R中.有人做过吗?我尝试了几种方法,但没有发现任何有用的方法.

I am new to Julia. I developed a few lines of code to get the results I needed from packages I was not able to find in Python or R. Now, I am trying to get this file to be easily accessible, and wrap the code in Python or R. Has anyone done this before? I have tried a few methods and have not found anything that has helped.

最简单的方法是几行代码,调用.jl文件,运行该文件(然后将代码从julia添加到.txt文件中),然后在代码出现时提醒您完成.

The most simple way to do this would be just a few lines of code that calls the .jl file, runs it (which the code is then added to a .txt file from julia), and then alerts you when the code is done.

任何帮助将不胜感激. R是更可取的方法,在这一点上,Python也将受到赞赏.

Any help would be greatly appreciated. R is the preferable method and at this point Python would be appreciated as well.

推荐答案

请在下面找到有关Python,R和一个外部进程(当然,它是可以从任何其他进程运行的可执行命令)的说明.我建议将您的代码放在一个包中,然后以其中一种语言加载它,而不是将其作为外部进程执行.

Please find below instructions for Python, R and just an external process (which of course is an executable command that can be run from any other process). I recommend putting your code in a package and loading it in one of those languages rather than executing this as an external process.

  1. 使用Python Anaconda(非内置系统Python)并安装Julia

  1. Use Python Anaconda (not in-built system Python) and install Julia

运行Julia并安装PyCall

Run Julia and install PyCall

using Pkg
ENV["PYTHON"]="/path/to/your/python/executable"
pkg"add PyCall"
pkg"build PyCall"

  • 将您的代码放入Julia包

  • Put your code into a Julia package

    using Pkg
    Pkg.generate("MyPackage")
    

    在文件夹src中,您将找到MyPackage.jl,对其进行编辑以使其看起来像这样:

    In the folder src you will find MyPackage.jl, edit it to look like this:

    module MyPackage
    function main(x,y)
         #do very complex staff or place it in your_other_file.jl
         2x.+y
    end
    include("your_other_file.jl")
    export main, and_whatever_other_functio_you_defined
    end
    

  • 安装pyjulia

  • Install pyjulia

    python -m pip install julia
    

    (在Linux系统上,您可能希望使用python3而不是python命令)

    (On Linux systems you might want to use python3 instead of python command)

    对于此步骤,请注意,虽然外部Python可与Julia一起使用.但是,为方便起见,可能值得 考虑使用与Julia一起安装的Python PyCall. 在这种情况下,请使用以下命令进行安装:

    For this step note that while an external Python can be used with Julia. However, for a convenience it might be worth to consider using a Python that got installed together with Julia as PyCall. In that case for installation use a command such this one:

    %HOMEPATH%\.julia\conda\3\python -m pip install julia
    

    或在Linux

    ~/.julia/conda/3/python -m pip install julia
    

    请注意,如果定义了JULIA_DEPOT_PATH变量,则可以用其值替换%HOMEPATH%\.julia~/.julia/.

    Note that if you have JULIA_DEPOT_PATH variable defined you can replace %HOMEPATH%\.julia or ~/.julia/ with its value.

    运行适当的Python并告诉它配置Python-Julia集成:

    Run the appropiate Python and tell it to configure the Python-Julia integration:

    import julia
    julia.install()
    

  • 现在您可以调用您的Julia代码了:

  • Now you are ready to call your Julia code:

    >>> from julia import Pkg
    >>> Pkg.activate(".\\MyPackage") #use the correct path
        Activating environment at `MyPackage\Project.toml`
    >>> from julia import MyPackage
    >>> MyPackage.main([1,2],5)
        [7,9]
    

  • Gnu R

    1. 配置系统PATH变量以指向您的Julia位置.因此,当您在控制台中键入julia时,应启动Julia

    1. Configure your system PATH variable to point to your Julia location. Hence when you type julia in the console it should start Julia

    运行以下脚本以安装R-Julia集成

    Run the script below to install R-Julia integration

    install.packages("JuliaCall")
    
    library(JuliaCall)
    julia <- julia_setup()
    

    1. 按照上面针对Python的说明进行操作(仅限第3步),并创建名为MyPackage

    运行代码

    library(JuliaCall)
    julia_eval("using Pkg;Pkg.activate(\"C:/temp/rrr/MyPackage\")")
    julia_library("MyPackage")
    
    julia_eval("MyPackage.main(3,5)")
    

    重击(或只是任何一种语言)

    1. 按照适用于Python的说明构建软件包(仅限第3步)

    1. Build the package following instructions for Python (step 3 only)

    配置系统PATH变量

    在包目录中运行命令(注意string(:.)是朱利安技巧,我用来避免bash命令中的撇号转义):

    Being in the package directory run the command (note string(:.) is a Julian trick that I use to avoid apostrophe escaping in bash commands):

    julia -e "using Pkg;Pkg.activate(string(:.));Pkg.instantiate();using MyPackage;MyPackage.main(3,4)"
    

    这将为您的软件包安装所有依赖项.为了跳过安装,请从上述命令中删除Pkg.instantiate().

    This will install all dependencies for your package. In order to skip the installation remove Pkg.instantiate() from the above command.

    这篇关于从R或Python运行.jl文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆