Import a module from a file into IEX

What is the proper way to load a module from a file in to IEX?

In the python world, doing something like this pretty standard:

from . import xyz
xyz.do_something()

I did a bit of google and this is what I found
$ iex

Code.load_file(“some_file.exs”)
moduleFromSomeFile.doSomething(1)

I was wondering if this is the “right” way to import a module in to iex and just inspecting/playing around with functions.

Thank you

If the compiled module is on the binary path for the BEAM then you can just do import Xyx to import everything from it. Or call it straight with Xyz.blah(42). mix sets up all those paths for you automatically if it is part of a project so with a mix project you can just do iex -S mix to get everything linked properly.

3 Likes

If I’m just dinkin around in a directory, outside of a mix project, and I have a module named “SomeModule.exs” in the working directory and I’m in iex, I do
c "SomeModule.exs", and I’ll have the module available to play with.

10 Likes

Hey there!
You could simply use- “import abc” (Given that you are in the directory where the module is defined)
and then happily use the functions defined in abc ,and hence without having to do abc.fun1() simply do fun1().

3 Likes
  • import_file/1
  • c/1 / c/2 / r/1

Above are what you need. Enjoy!

Wow, not a single one of of these worked for me. Many show the module compile, but then the functions I expect are undefined. Could somebody show me a complete example, of how to run Jason.encode!(%{ "a" => 1 }), in the Elixir REPL, from inside a mix project?

$ iex -S mix
iex> Jason.encode!(%{"a" => 1})
"{\"a\":1}"
4 Likes

Generally in Elixir you will not be importing modules from outside your project, so you will not need the techniques listed in this post. Instead you will generally declare dependencies (by adding to the list of deps in your mix.exs file). Then (as @hauleth showed) you can run iex -S mix to start an iex session inside the project, which will have all the modules from all your dependencies loaded (also if you’re using phoenix you may be interested in running iex -S mix phx.start to have an iex shell in the same beam instance as your phoenix application).

Here’s a page from the official tutorial with some more info: https://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html

1 Like

what if you need to debug in a docker prod container where you don’t have the mix* files and you need to load HTTPoison to understand a catchy issue that doesn’t happen in your test env?

Yeah, that doesn’t sound like a general case :+1:

I have a fixture under test/support/fixtures that isn’t working. I was hoping to load that module into iex and debug it interactively but i get “module not loaded and could not be found”. Is there a way to config for this scenario? Thanks.

Update: I got it to work in iex via Code.load_file("test/fixtures/filename.ex"). Iex says this is deprecated in favor of Code.require_file but I couldn’t get that one to work.

In a Phoenix project, iex is usually running in MIX_ENV=dev which means files in test/support aren’t compiled with the default generated mix.exs:

Also, it may be a transcription error - but your Code.load_file example is reading from test/fixtures while your previous post mentions test/support/fixtures :thinking:

2 Likes

Clarifying what @al2o3cr means into an actionable:

You probably actually want to run MIX_ENV=test iex -S mix to get that module.

2 Likes

Either that, or add test/support to elixirc_paths for dev; I’ve worked on projects that did that because the seeds.exs file used ex_machina factories.

add test/support to elixirc_paths for dev

Yikes, I don’t like that because it subverts the developers expectations and gives me an “icky feeling”, though I can’t immediately identify a downside.