Liking cljdoc? Tell your friends :D


JNA libpython bindings to the tech ecosystem.

Clojars Project travis integration

  • Bridge between JVM objects and Python objects easily; use Python in your Java and use some Java in your Python.
  • Python objects are linked to the JVM GC such that when they are no longer reachable from the JVM their references are released. Scope based resource contexts are also available.
  • Finding the python libraries is done dynamically allowing one system to run on multiple versions of python.
  • REPL oriented design means fast, smooth, iterative development.


We aim to integrate Python into Clojure at a deep level. This means that we want to be able to load/use python modules almost as if they were Clojure namespaces. We also want to be able to use Clojure to extend Python objects. I gave a talk at Clojure Conj 2019 that outlines more of what is going on.

This code is a concrete example that generates an embedding for faces:

(ns facial-rec.face-feature
  (:require [libpython-clj.require :refer [require-python]]
            [libpython-clj.python :as py]
            [tech.v2.datatype :as dtype]))

(require-python '(mxnet mxnet.ndarray mxnet.module mxnet.model))
(require-python 'cv2)
(require-python '[numpy :as np])

(defn load-model
  [& {:keys [model-path checkpoint]
      :or {model-path "models/recognition/model"
           checkpoint 0}}]
  (let [[sym arg-params aux-params] (mxnet.model/load_checkpoint model-path checkpoint)
        all-layers (py/$a sym get_internals)
        target-layer (py/get-item all-layers "fc1_output")
        ;;TODO - We haven't overloaded enough of the IFn invoke methods for
		;;this to work without using call-kw
        model (py/call-kw mxnet.module/Module [] {:symbol target-layer :context (mxnet/cpu) :label_names nil})]
    (py/$a model bind :data_shapes [["data" [1 3 112 112]]])
    (py/$a model set_params arg-params aux-params)

(defonce model (load-model))

(defn face->feature
    (if-let [new-img (cv2/imread img-path)]
      (let [new-img (cv2/cvtColor new-img cv2/COLOR_BGR2RGB)
            new-img (np/transpose new-img [2 0 1])
            input-blob (np/expand_dims new-img :axis 0)
            data (mxnet.ndarray/array input-blob)
            batch ( :data [data])]
        (py/$a model forward batch :is_train false)
        (-> (py/$a model get_outputs)
            (py/$a asnumpy)
            (#(dtype/make-container :java-array :float32 %))))
      (throw (Exception. (format "Failed to load img: %s" img-path))))))


user> (require '[libpython-clj.require :refer [require-python]])
...logging info....
user> (require-python '[numpy :as np])
user> (def test-ary (np/array [[1 2][3 4]]))
user> test-ary
[[1 2]
 [3 4]]

We have a document on all the features but beginning usage is pretty simple. Import your modules, use the things from Clojure. We have put effort into making sure things like sequences and ranges transfer between the two languages.


One very complimentary aspect of Python with respect to Clojure is it's integration with cutting edge native libraries. Our support isn't perfect so some understanding of the mechanism is important to diagnose errors and issues.

Current, we launch the python3 executable and print out various different bits of configuration as json. We parse the json and use the output to attempt to find the shared library so for example if we are loading python 3.6 we look for on Linux or libpython3.6m.dylib on the Mac.

This pathway has allowed us support Conda albeit with some work. For examples using Conda, check out the facial rec repository above or look into how we build our test docker containers.

Further Information

New To Clojure

New to Clojure or the JVM? Try remixing the nextjournal entry and playing around there. For more resources on learning and getting more comfortable with Clojure, we have an introductory document.



Copyright © 2019 Chris Nuernberger

This program and the accompanying materials are made available under the terms of the Eclipse Public License 2.0 which is available at

Can you improve this documentation? These fine people already did:
Chris Nuernberger & skullgoblet1089
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close