How can new clojure libraries be loaded in the repl
Asked Answered
B

4

12

So I have this workflow problem:

I'm happily typing away on my clojure project repl and realise that i need another library that is not in my project.clj, say in this case, i needed the tools.cli library.

I open up project.clj in my editor and add in an entry to the :dependencies

     [org.clojure/tools.cli "0.2.1"]

Then, within the project directory, I type lein deps in the shell to pull in the necessary libraries

After the project dependencies are pulled, technically all the class files are already there ready to be loaded, but if I go back to my repl and type:

> (use 'tools.cli)

I get this:

=>> FileNotFoundException Could not locate tools/cli__init.class
  or tools/cli.clj on classpath:   clojure.lang.RT.load (RT.java:432)

So I would have to restart my repl, wasting a whole heap of time reconfiguring the state of the repl to where I was before I needed the library.

Is there a way to just load in the library dynamically? eg, after I run lein deps I just go back to the repl and type:

> (load-library "tools.cli") 
> (use 'tools.cli)

Thanks in advance

Backswept answered 25/7, 2012 at 1:8 Comment(1)
See #11328124Tessellation
F
8

Pomegranate is for you:

https://github.com/cemerick/pomegranate

It supports download and addition of new dependencies at runtime, e.g.:

(add-dependencies :coordinates '[[incanter "1.2.3"]]
                  :repositories (merge cemerick.pomegranate.aether/maven-central
                                       {"clojars" "http://clojars.org/repo"}))
Fitts answered 25/7, 2012 at 1:54 Comment(0)
L
1

Will something like this work for you?

https://groups.google.com/d/msg/clojure/AJXqbpGMQw4/0-7-3pXRwGkJ

There is also clojure.core/add-classpath, but it's deprecated.

http://clojuredocs.org/clojure_core/clojure.core/add-classpath

Longish answered 25/7, 2012 at 1:37 Comment(0)
B
1

You can try out one library in the repl using lein-try.

~/.lein/profiles.clj:

{:user {:plugins [[lein-try "0.4.3"]]}}

command line:

$ lein try clj-time "0.5.1"
Fetching dependencies... (takes a while the first time)
lein-try loaded [clj-time "0.5.1"]

nREPL server started on port 57036
REPL-y 0.2.0
Clojure 1.5.1
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)

user=>
Brentonbrentt answered 7/12, 2017 at 9:6 Comment(0)
P
0

Since v1.12.0, Clojure provides add-lib, add-libs and sync-deps out of the box (under namespace clojure.repl.deps).

See: https://clojure.org/news/2023/04/14/clojure-1-12-alpha2

Psychopathist answered 9/8 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.