pit007

pit007

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?

Marked As Solved

outlog

outlog

you can use the path option

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

Also Liked

adkron

adkron

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.

w0rd-driven

w0rd-driven

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.

linusdm

linusdm

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.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement