How do I load an Elixir library into iex without adding it to a project's mix.exs deps?

不羁的心 提交于 2019-12-20 12:14:46

问题


I want to tryout the Poison json module without creating a mix project.

How do I install it and make it available in iex via import?

I have been able to add it to a project, then use it after going into the project directory and using iex -S mix:

tbrowne@LILJEN:~/code/elixirTry/pj$ cat mix.exs
defmodule Pj.Mixfile do
  use Mix.Project

  def project do
    [app: :pj,
     version: "0.0.1",
     elixir: "~> 1.2",
     build_embedded: Mix.env == :prod,
     start_permanent: Mix.env == :prod,
     deps: deps]
  end

  # Configuration for the OTP application
  #
  # Type "mix help compile.app" for more information
  def application do
    [applications: [:logger]]
  end

  # Dependencies can be Hex packages:
  #
  #   {:mydep, "~> 0.3.0"}
  #
  # Or git/path repositories:
  #
  #   {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
  #
  # Type "mix help deps" for more examples and options
  defp deps do
    [{:poison, "~> 2.0"}]
  end
end
tbrowne@LILJEN:~/code/elixirTry/pj$ cat lib/pj.ex
defmodule Person do
  @derive [Poison.Encoder]
  defstruct [:name, :age]
end

defmodule Pj do
  xx = Poison.encode!(%Person{name: "Devin Torres", age: 27})
end

tbrowne@LILJEN:~/code/elixirTry/pj$ iex -S mix
Erlang/OTP 18 [erts-7.2] [source-e6dd627] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.2.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> import Poison
nil
iex(2)>

However if I just go into a normal iex in a generic directory then I can't seem to access the Poison library:

iex(4)> import IO
nil
iex(5)> puts("hello")
hello
:ok
iex(6)> import Poison
** (CompileError) iex:6: module Poison is not loaded and could not be found

Also, how do I install a library globally from github?


回答1:


I can recommend you this blog post.


1st Step: What do you want?

There's more than a couple of libraries I want to use without a Mix project, like

  • Combine
  • CSV
  • Poison

Get their sources from Github, git checkout to the last release and compile them.

2nd Step: Where do you want them?

One compilation was over, create ~/.mix/beam/ and move the .beam files into this directory.

3rd Step: Now let's customize your Interactive EliXir shell!

Thankfully, iex is just a shell script. If you happen to have a custom $PATH variable that points to ~/.local/bin, then copy iex to this directory and rename it to something like deviex. Then in your custom deviex, move to the last line and change it to…

exec elixir --no-halt --erl "-user Elixir.IEx.CLI" -pa "$HOME/.mix/beam" +iex "$@"

And now it will load the .beam files located at ~/.mix/beam at startup.

The reason why we use a different script for IEx is to avoid name conflicts with installed libs in the projects you'll work on with regular iex.




回答2:


Not a direct answer, but another way to maybe achieve what you want:

You could have a playground project that you generate once (e.g. mix new playground) and that you can then relatively easily add new dependencies to.

If you do iex -S mix within this project, you'll get all its dependencies.

If you want to quickly experiment with e.g. Poison at some later point in time, you can just go back into this project.




回答3:


I don't know if there is an official way to do this.

One way would be to clone the library project locally, compile it, and then add it to the library path like this by creating a ~/.iex.exs script:

IO.puts "Adding poison to path from ~/.iex.exs"
true = Code.prepend_path("#{path_to_project}"/poison/_build/dev/lib/poison/ebin")


来源:https://stackoverflow.com/questions/36065774/how-do-i-load-an-elixir-library-into-iex-without-adding-it-to-a-projects-mix-ex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!