# How to create embedded modules

**URL:** https://discourse.julialang.org/t/how-to-create-embedded-modules/30300
**Category:** Internals & Design
**Tags:** embedding
**Created:** [October 25, 2019, 12:45pm UTC](https://discourse.julialang.org/t/how-to-create-embedded-modules/30300 "2019-10-25T12:45:48Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Denis\_Zaika](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/denis_zaika/32/8252_2.png) [@Denis\_Zaika](https://discourse.julialang.org/u/Denis_Zaika)
#### Post date: [October 25, 2019, 12:45pm UTC](https://discourse.julialang.org/t/how-to-create-embedded-modules/30300/1 "2019-10-25T12:45:48Z")

</div>

When embedding python, we can create custom module, populate it with objects internal to application,  
and execute embedded script within this module. How to access internals of hosting application from embedded julia script.

---

<div class="post-metadata">

### Author: ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)
#### Post date: [October 25, 2019, 3:32pm UTC](https://discourse.julialang.org/t/how-to-create-embedded-modules/30300/2 "2019-10-25T15:32:01Z")

</div>

Welcome.

You can use modules and then use them, the module can be defined in the same file or another (using include), the module name does not implies a directory like in Python.  
See [Modules · The Julia Language](https://docs.julialang.org/en/v1/manual/modules/)

I think this can solve your doubts, if not you should ask more concrete questions.

---

<div class="post-metadata">

### Author: ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)
#### Post date: [October 25, 2019, 4:46pm UTC](https://discourse.julialang.org/t/how-to-create-embedded-modules/30300/3 "2019-10-25T16:46:54Z")

</div>

Can you be more specific about what you mean by “custom module”? And how are you embedding Julia/Python into your application?

---

<div class="post-metadata">

### Author: ![Denis\_Zaika](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/denis_zaika/32/8252_2.png) [@Denis\_Zaika](https://discourse.julialang.org/u/Denis_Zaika)
#### Post date: [October 26, 2019, 8:17am UTC](https://discourse.julialang.org/t/how-to-create-embedded-modules/30300/4 "2019-10-26T08:17:16Z")

</div>

I mean creation of the modules on the fly… Just look at this excerpt from python docs…

> Until now, the embedded Python interpreter had no access to functionality from the application itself. The Python API allows this by extending the embedded interpreter. That is, the embedded interpreter gets extended with routines provided by the application. While it sounds complex, it is not so bad.

```python
static int numargs=0;

/* Return the number of arguments of the application command line */
static PyObject*
emb_numargs(PyObject *self, PyObject *args)
{
    if(!PyArg_ParseTuple(args, ":numargs"))
        return NULL;
    return PyLong_FromLong(numargs);
}

static PyMethodDef EmbMethods[] = {
    {"numargs", emb_numargs, METH_VARARGS,
     "Return the number of arguments received by the process."},
    {NULL, NULL, 0, NULL}
};

static PyModuleDef EmbModule = {
    PyModuleDef_HEAD_INIT, "emb", NULL, -1, EmbMethods,
    NULL, NULL, NULL, NULL
};

static PyObject*
PyInit_emb(void)
{
    return PyModule_Create(&EmbModule);

```

Looking at the julia.h, i can see _\_jl\_module\_t_ exist. But, how to add new methods. Also i cant’ figure out how to wrap my C function to _\_jl\_method\_t_.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [October 26, 2019, 1:38pm UTC](https://discourse.julialang.org/t/how-to-create-embedded-modules/30300/5 "2019-10-26T13:38:06Z")

</div>

Unless it’s important to you that it’s structured the same way as in your Python example, this looks mostly like a question about how to call a C function in the application from within the embedded Julia code. The general idea is to use `ccall` and you can find an example of this in [https://github.com/JuliaLang/julia/blob/master/test/embedding/embedding.c](https://github.com/JuliaLang/julia/blob/master/test/embedding/embedding.c), lines 104-112.

Usually it’s easier to create high level functions around the `ccall` on the Julia side but it’s certainly possible to do it from C as well. With a sufficiently ambitious helper library it might even look similar to the Python example, but as far as I know nobody has yet had a big enough need to create such a helper library and make it public.

---

<div class="post-metadata">

### Author: ![Denis\_Zaika](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/denis_zaika/32/8252_2.png) [@Denis\_Zaika](https://discourse.julialang.org/u/Denis_Zaika)
#### Post date: [October 26, 2019, 3:17pm UTC](https://discourse.julialang.org/t/how-to-create-embedded-modules/30300/6 "2019-10-26T15:17:50Z")

</div>

Oh, this is exactly what i want ! To make it clear, i just want` to replace python scripting support in my application.
