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"}
])
3 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.

3 Likes