# Embedding Julia and Packages

**URL:** <https://discourse.julialang.org/t/embedding-julia-and-packages/4305>\
**Category:** General Usage\
**Tags:** embedding\
**Created:** [June 16, 2017, 1:13pm UTC](https://discourse.julialang.org/t/embedding-julia-and-packages/4305 "2017-06-16T13:13:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [June 16, 2017, 1:13pm UTC](https://discourse.julialang.org/t/embedding-julia-and-packages/4305/1 "2017-06-16T13:13:11Z")

</div>

Hi,  
I’m guessing the answer but anyway would like a confirmation. Reading the [Embedding](https://docs.julialang.org/en/release-0.5/manual/embedding/) docs I see no reference to the possibility to call Julia modules from other languages, right?

There is this `jl_function_t *func = jl_get_function(jl_base_module, "sqrt");` which suggest that other than `jl_base_module` could be used but couldn’t find any further documentation on the `jl_get_function` (also found #22190 but couldn’t understand the outcome of it too).

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [June 16, 2017, 1:21pm UTC](https://discourse.julialang.org/t/embedding-julia-and-packages/4305/2 "2017-06-16T13:21:11Z")

</div>

`jl_eval_string("Base")`

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [June 16, 2017, 1:29pm UTC](https://discourse.julialang.org/t/embedding-julia-and-packages/4305/3 "2017-06-16T13:29:52Z")

</div>

Sorry, I used a bad naming. I meant to say if we could call functions from Julia _Packages_.  
I am still investigating the possibility to establish a two-way communication between Julia and Red but for it to work data would have to be transmitted back and forth. But if can’t call functions from my Packages (or others) everything falls down.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 16, 2017, 1:56pm UTC](https://discourse.julialang.org/t/embedding-julia-and-packages/4305/4 "2017-06-16T13:56:47Z")

</div>

It’s a bit unclear what do you mean by “calling” a module since modules are not callable. You can run arbitrary julia code with `jl_eval_string`, which includes importing and obtaining a reference to a module. Then you can get any global variables in the module with `jl_get_global` and call them or do whatever you want on them.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [June 16, 2017, 2:09pm UTC](https://discourse.julialang.org/t/embedding-julia-and-packages/4305/5 "2017-06-16T14:09:20Z")

</div>

> It’s a bit unclear what do you mean by “calling” a module

I rephrased it in the second post to _call functions from Julia Packages_ which includes the need to be able to send data to the Julia side and eventually get some other back to caller in the external side.  
Something called `eval_string` seams a bit limiting but I never ventured in this Julia domain.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 16, 2017, 2:40pm UTC](https://discourse.julialang.org/t/embedding-julia-and-packages/4305/6 "2017-06-16T14:40:53Z")

</div>

You shouldn’t use `eval_string` for all calls. However, it is the right function to define your own julia helper. Similar to PyPy, since a lot of the features are defined in the language itself (not C), we define only a small number of C-API to evaluate julia code and to do some basic operations on objects and you generally need to create a few of your own julia function helpers to do what you want. It is the most natural way since a lot of the things you want to do are defined in julia and you can also take advantage of the JIT that way.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [June 16, 2017, 2:48pm UTC](https://discourse.julialang.org/t/embedding-julia-and-packages/4305/7 "2017-06-16T14:48:48Z")

</div>

Thanks. Let’s say that I understand the general picture … now the details

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [June 16, 2017, 8:04pm UTC](https://discourse.julialang.org/t/embedding-julia-and-packages/4305/8 "2017-06-16T20:04:50Z")

</div>

Currently the best (only?) full non-C embedding example is probably pyjulia. It uses a global Dict (via PyCall) to store a reference to every wrapped object in order to prevent garbage collection.

For other general API usage examples from C(++), look at CxxWrap (uses a global array for persistent references), and for embedding see node-julia, and maybe julia.js (not sure how far along it is by now, but it looked promising).

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [June 16, 2017, 11:23pm UTC](https://discourse.julialang.org/t/embedding-julia-and-packages/4305/9 "2017-06-16T23:23:01Z")

</div>

Thanks. `mexjulia.jl` also has an embedding example. And I see that it uses `jl_function_t *fn = jl_get_function(jl_main_module, fnName);` (not `jl_base_module` like in the Embedding docs).
