Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Import Elixir Dependencies Into Current File/Module

I'm trying to use the Hex packaged CSV.

I'm added the dependency to mix.exs like so:

  def application do
    [applications: [:logger, :csv]]
  end

  defp deps do
    [
      {:csv, "~> 1.4.2"}
    ]

Then, I run mix deps.get & mix deps.compile in terminal.

The dependency folder shows up in deps folder.

Everything seems to be okay until I try to use the dependency. I'm getting this error:

"module CSV is not loaded and could not be found"

I've tried a simply use without importing like so:

File.stream!("data.csv") |> CSV.decode

I've tried importing like so:

import(CSV)
File.stream!("data.csv") |> CSV.decode

I've tried prefixing the deps directory:

import(deps.CSV)
File.stream!("data.csv") |> CSV.decode

And the full path to the deps subdirectory:

import(deps.csv.lib.csv.CSV)
File.stream!("data.csv") |> CSV.decode

No matter what it doesn't seem to find the dependency module.

What am I missing?

like image 555
Emily Avatar asked Sep 14 '16 03:09

Emily


1 Answers

I'm guessing that your IDE is misconfigured. Here is an command line example of how to get up and running.

mix new csv_tester
cd csv_tester
# edit mix.exs to add {:csv, "~> 1.4"} to your deps, like you have above
mix deps.get
iex -S mix
iex(1)> [["name", "age"], ["Bob Saget", 64]] |> CSV.encode() |> Enum.join("")
"name,age\r\nBob Saget,64\r\n"

Modules are globally accessible, so you do not need to use the import statement unless you only want a specific function in a given module, or you do not want to have to fully qualify the function call with the Module name. See import docs.

Anything that you can execute in iex you should be able to call from any other Module in your project

If you still need help debugging, you can run this in iex to list all modules that should be available at runtime and then filter for anything containing CSV in the module name:

iex(1)> :code.get_path() |> 
  Enum.map(&to_charlist/1) |> 
  Enum.map(&:erl_prim_loader.list_dir/1) |> 
  Enum.map(&elem(&1, 1)) |> 
  Enum.concat() |> 
  Enum.map(&to_string/1) |> 
  Enum.filter(fn module -> module =~ "CSV" end)

Which should output something like this

["Elixir.CSV.Encode.beam", "Elixir.CSV.Encoder.beam", "Elixir.CSV.beam",
 "Elixir.CSV.Lexer.EncodingError.beam", "Elixir.CSV.LineAggregator.beam",
 "Elixir.CSV.Parser.beam", "Elixir.CSV.Lexer.beam",
 "Elixir.CSV.Encode.BitString.beam",
 "Elixir.CSV.LineAggregator.CorruptStreamError.beam", "Elixir.CSV.Decoder.beam",
 "Elixir.CSV.Decoder.RowLengthError.beam", "Elixir.CSV.Encode.beam",
 "Elixir.CSV.Parser.SyntaxError.beam", "Elixir.CSV.Encode.Any.beam",
 "Elixir.CSV.Defaults.beam"]
like image 148
sdc Avatar answered Oct 13 '22 17:10

sdc