我想尝试 Poison json 模块而不创建混合项目。
如何安装它并通过导入使其在 iex 中可用?
我已经能够将它添加到项目中,然后在进入项目目录并使用 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 库:
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 全局安装库?
不是直接答案,而是另一种实现你想要的方法:
您可以拥有一个生成一次的游乐场项目(例如mix new playground
),然后您可以相对轻松地添加新的依赖项。
如果你这样做iex -S mix
在此项目中,您将获得其所有依赖项。
如果您想快速尝试,例如Poison
在稍后的某个时间点,您可以返回到该项目。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)