[英]How do I load an Elixir library into iex without adding it to a project's mix.exs deps?
I want to tryout the Poison json module without creating a mix project.我想在不创建混合项目的情况下试用 Poison json 模块。
How do I install it and make it available in iex via import?如何安装它并通过导入使其在 iex 中可用?
I have been able to add it to a project, then use it after going into the project directory and using iex -S mix:我已经能够将它添加到项目中,然后在进入项目目录并使用 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,那么我似乎无法访问 Poison 库:
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?另外,如何从 github 全局安装库?
Not a direct answer, but another way to maybe achieve what you want:不是直接的答案,而是另一种可能实现您想要的方式:
You could have a playground project that you generate once (eg mix new playground
) and that you can then relatively easily add new dependencies to.您可以有一个您生成一次的游乐场项目(例如
mix new playground
),然后您可以相对轻松地添加新的依赖项。
If you do iex -S mix
within this project, you'll get all its dependencies.如果您在该项目中执行
iex -S mix
,您将获得其所有依赖项。
If you want to quickly experiment with eg Poison
at some later point in time, you can just go back into this project.如果您想在稍后的某个时间点快速尝试例如
Poison
,您可以回到这个项目。
There's more than a couple of libraries I want to use without a Mix project, like我想在没有 Mix 项目的情况下使用多个库,例如
Get their sources from Github, git checkout to the last release and compile them.从 Github 获取他们的源代码,git checkout 到最后一个版本并编译它们。
Once compilation is over, create ~/.mix/beam/ and move the .beam files into this directory.编译完成后,创建 ~/.mix/beam/ 并将 .beam 文件移动到此目录中。
Thankfully, iex
is just a shell script.值得庆幸的是,
iex
只是一个 shell 脚本。 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
.如果您碰巧有一个指向 ~/.local/bin 的自定义
$PATH
变量,则将 iex 复制到此目录并将其重命名为deviex
之类的名称。 Then in your custom deviex
, move to the last line and change it to…然后在您的自定义
deviex
中,移动到最后一行并将其更改为...
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.现在它将在启动时加载位于 ~/.mix/beam 的 .beam 文件。
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
.我们为 IEx 使用不同脚本的原因是为了避免与您将使用常规
iex
处理的项目中已安装的库发生名称冲突。
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:一种方法是在本地克隆库项目,编译它,然后通过创建
~/.iex.exs
脚本将其添加到库路径中:
IO.puts "Adding poison to path from ~/.iex.exs"
true = Code.prepend_path("#{path_to_project}"/poison/_build/dev/lib/poison/ebin")
You can use Mix.install
, introduced in Elixir 1.12.您可以使用 Elixir 1.12 中引入的
Mix.install
。 It works from any directory, no Mix project required.它适用于任何目录,不需要 Mix 项目。
iex(1)> Mix.install [:poison]
:ok
iex(2)> import Poison
Poison
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.