How to import a local library in Livebook?

I’m trying to develop a project using Livebook.
So I wrote and tested the code for the first module.
I don’t want to publicize the code to Hex yet, because it’s way too early for this.
I created a new project using mix.
Now I would like to use the code of my first module in Livebook for further development.
When specifying the module name in Livebook’s Mix.install section I get the error message “No package with name…”. I kind of expected this. But then how can I “import” the module for use inLivebook?

1 Like

you can use the path option

Mix.install([
  {:mylib,  path: "/Users/myuser/somefolder/mylib"}
])
6 Likes

You may prefer relative paths using __DIR__. That would resolve to the directory your notebook is in.

For this type of work I usually create a directory, run git init, save my notebook here, commit, then run mix new <library_name> so everything is co-located.

An alternative would be to mix new, initialize git, and commit (I always commit immediately after the generator runs before I make any customizations), then save a notebook under the notebooks directory. Then you could do something like __DIR__/../ to go up to the parent directory and load the library that way. This approach is probably better if you’re wanting to include a notebook with your library.

The first approach makes sense if your primary focus is the notebook itself with tiny libraries to support it. You never need to publish to hex in either scenario if you don’t want to. If you package the whole thing in a git repository, other users can replicate your work whereas a standalone notebook wouldn’t work without all your dependencies.

You likely do not want to use absolute paths because my /Users/jeremy/ directory is not the same as your /Users/pit007 would be for an example. If you’re the only one using the notebook, portability doesn’t matter but I create very few notebooks for just myself.

2 Likes

I like to run an elixir application and then connect to that application instead of directly trying to pull in libraries.

Run your project with something like iex --sname test --cookie mycookie -S mix, which will start an iex console with your library loaded. Note the sname and cookie you use to create the session.

Then inside the livebook, find
Screen Shot 2022-11-18 at 3.29.41 PM
and click it. A menu will appear, and click the Configure button. Once the popup appears, click Attached node. You can then fill in your cookie and name and click connect. Now your livebook will have access to the same session at your iex console.

When you recompile code in iex, the livebook will have all the updated code available.

@w0rd-driven’s solution works. Connecting to a running node is my preferred method.

5 Likes

I’m noticing that after recompiling inside IEx, I still have to click the “Reconnect and setup” cell at the top to get the updates, even though there’s nothing in that cell. The Livebook doesn’t automatically update. Is this your experience?

Livebook also doesn’t seem to take into account the .iex.exs file where I have pre-entered aliases that I want to use in an IEx session or Livebook notebook. I still have to re-alias things inside the notebook.

No, I just didn’t know I had to recompile.
After all Phoenix notices changes automatically via inotify and recompiles.

Thanks for your comment.

When starting Phoenix with iex -S, it no longer seems to pick up the signal to recompile. If you hop into the console and type recompile it will work. I haven’t tried to see if there is a way to fix this. Using the recompile command, you will not have to restart and reset the connection.

Wonderful

Thank you very much for your help.
Cheers
Peter

Okay, I did some playing around, and this appears inaccurate. I just tried again and added a function to the LiveView. That seemed not to require a call to recompile. I’m going to experiment a little and see what I find. I’ll check back in if I figure anything out.

I had a similar question about recompilation a few months back. José and Jonatan were kind enough to answer my question in the github discussions here: How to recompile installed mix dependency (with `:path` option) without reconnecting runtime? · livebook-dev/livebook · Discussion #1625 · GitHub

Tl;dr: recompilation is not (yet) supported by Mix.install/2, so reconnecting is the only option, for path-dependencies.

3 Likes

Interestingly enough, I am now just doing recompile inside the session started by iex --sname <name> --cookie <cookie> -S mix, and it is working. This exact thing wasn’t working earlier when I posted my previous reply, for whatever reason, but now, I can just do recompile and then re-evaluate individual cells as expected. I’m not for sure what changed or if Livebook just got into some weird state.

I haven’t rechecked the .iex.exs file not being read.