本文介绍了如何将Elixir库加载到iex中而不将其添加到项目的mix.exs deps中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想试用Poison json模块而不创建一个混合项目。

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

如何安装并通过导入使其在iex中可用?

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

我已经能够添加它进入一个项目,然后在进入项目目录并使用iex -S mix之后使用它:

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)>

但是,如果我只是进入通用目录中的普通iex,则似乎无法访问Poison库:

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

另外,如何我应该从github全局安装一个库吗?

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

推荐答案

我可以向您推荐。

在没有Mix项目的情况下,我想使用多个库,例如

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


  • 合并

  • CSV

  • 毒药

从Github获取其源代码,git checkout到最新版本并进行编译。

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

一个编译结束了,创建〜/ .mix / beam /并将.beam文件移到该目录中。

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

很高兴,iex只是一个shell脚本。如果您碰巧有一个指向〜/ .local / bin的自定义$ PATH变量,则将iex复制到此目录并将其重命名为deviex之类的东西。
然后在您的自定义deviex中,移至最后一行并将其更改为…

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 "$@"

现在它将在启动时加载位于〜/ .mix / beam的.beam文件。

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

我们使用a的原因IEx的另一种脚本是避免与常规Iex一起使用的项目中的已安装库名称冲突。

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.

这篇关于如何将Elixir库加载到iex中而不将其添加到项目的mix.exs deps中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 07:22