Skip to content

How to include source written in julia ? #202

Closed

Description

@terasakisatoshi

When we would like to use 3rd party library from Python via pyjulia
e.g. Images which is a Julia Package , we only have to use Main.using

from julia import Main
Main.using("Images")
img = Main.eval('load("path/to/img")') # where load is a function defined in Images package

On the other hand, is there any way import or incluide our own source code ?

For example I created a trivial example:

#example.jl
function echomsg(msg)
    return msg 
end

Then I would like to call echomsg defined above from Python.
I tried to from julia import Main; Main.eval('include("example.jl")') on python REPL
but failed with the following message

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/terasakisatoshi/.pyenv/versions/miniconda3-3.19.0/lib/python3.5/site-packages/julia/core.py", line 739, in eval
    ans = self._call(src)
  File "/Users/terasakisatoshi/.pyenv/versions/miniconda3-3.19.0/lib/python3.5/site-packages/julia/core.py", line 672, in _call
    self.check_exception(src)
  File "/Users/terasakisatoshi/.pyenv/versions/miniconda3-3.19.0/lib/python3.5/site-packages/julia/core.py", line 722, in check_exception
    .format(exception, src))
julia.core.JuliaError: Exception 'UndefVarError: include not defined' occurred while calling julia code:
include("example.jl")

How should I do ???

Activity

  1. tkf commented on Sep 16, 2018

    @tkf
    Member

    #200 (comment) should fix the problem. Meanwhile, you can do:

    from julia import Julia
    julia = Julia()
    julia.eval("@eval Main import Base.MainInclude: include")
    from julia import Main
    Main.include("...")
  2. terasakisatoshi commented on Sep 17, 2018

    @terasakisatoshi
    Author

    @tkf

    This work fine. Thank you !

    #example.jl
    function echomsg(msg)
        return join([msg, "Thank you !"], ". ")
    end
    def main():
        from julia import Julia
        julia = Julia()
        julia.eval("@eval Main import Base.MainInclude: include")
        from julia import Main
    
        Main.include("example.jl")
        msg = "This works file"
        print(Main.echomsg(msg))
    
    if __name__ == '__main__':
        main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions