0

Encountered the following error when I setup clj-python in virtual environment.

In order to fix the error, there are two steps which I did but the errors still exist.

  1. to specify ":python-executable" and ":library-path" with "py/initialize!" (please see the source "core.clj:

  2. If I did symbolic-linking in the virtualenv's lib "~/.venv/ml/lib/libpython3.7m.dylib" directory, "lein repl" cannot be started.
    e.g ln -s /usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/libpython3.7m.dylib

Please feel free to comment.


update on 24 May 2020

"lein repl" can be started when revoke ":python-executable" and ":python-executable" to local dictionaries.

(ns my-stuff.core
  (:require [libpython-clj.python :as py]))

(py/initialize! :python-executable "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/bin/python3.7"
                :library-path "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/libpython3.7m.dylib")

(require '[libpython-clj.require :refer [require-python]])

Error

my-stuff.core=> May 23, 2020 6:39:35 PM clojure.tools.logging$eval505$fn__508 invoke
INFO: Executing python initialize with options:{:python-executable "/Users/nelson/.venv/ml/bin/python", :program-name nil, :python-home nil, :library-path "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/libpython3.7m.dylib"}
May 23, 2020 6:39:35 PM clojure.tools.logging$eval505$fn__508 invoke
INFO: Detecting startup-info for Python executable: /Users/nelson/.venv/ml/bin/python
May 23, 2020 6:39:35 PM clojure.tools.logging$eval505$fn__508 invoke
INFO: Startup info detected:
{:lib-version "3.7",
 :java-library-path-addendum "/Users/nelson/.venv/ml/lib",
 :exec-prefix "/Users/nelson/.venv/ml",
 :executable "/Users/nelson/.venv/ml/bin/python",
 :libnames
 ("/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/libpython3.7m.dylib"
  "python3.7"),
 :prefix "/Users/nelson/.venv/ml",
 :base-prefix
 "/usr/local/opt/python/bin/../Frameworks/Python.framework/Versions/3.7",
 :base-exec-prefix
 "/usr/local/opt/python/bin/../Frameworks/Python.framework/Versions/3.7",
 :python-home "/Users/nelson/.venv/ml",
 :version [3 7 7],
 :platform "darwin"}

May 23, 2020 6:39:35 PM clojure.tools.logging$eval505$fn__508 invoke
INFO: Trying python library names ["/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/libpython3.7m.dylib" "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/libpython3.7m.dylib" "python3.7" "python3.7m" "python3.6m"]
May 23, 2020 6:39:35 PM clojure.tools.logging$eval505$fn__508 invoke
INFO: Setting java library path: /Users/nelson/.venv/ml/lib:/Users/nelson/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
May 23, 2020 6:39:35 PM clojure.tools.logging$eval505$fn__508 invoke
INFO: Reference thread starting
May 23, 2020 6:39:35 PM clojure.tools.logging$eval505$fn__508 invoke
INFO: Library /usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/libpython3.7m.dylib found at [:system "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/libpython3.7m.dylib"]
Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

Current thread 0x0000700008986000 (most recent call first):
Subprocess failed (exit code: 134)

project.clj

(defproject my-stuff "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
            :url "https://www.eclipse.org/legal/epl-2.0/"}
  :dependencies [[org.clojure/clojure "1.10.1"]
                 [clj-python/libpython-clj "1.44"]]
  :main ^:skip-aot my-stuff.core
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all}})

core.clj

(ns my-stuff.core
  (:require [libpython-clj.python :as py]
            [libpython-clj.jna.base]))

(py/initialize! :python-executable (str (System/getenv "HOME") "/.venv/ml/bin/python")
                :library-path (str "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/libpython3.7m.dylib"))
madeinQuant
  • 1,721
  • 1
  • 18
  • 29

1 Answers1

0

I did a small test of of clj-python a while ago and it seems there's a limitation with lein so that new projects are encouraged to use the clj tool and deps.edn.

I have a small example that uses virtualenv and includes Clojure/Python interop with a small library that you would install from pip in your virtualenv here: https://github.com/dfuenzalida/clj-python-interop

Denis Fuenzalida
  • 3,271
  • 1
  • 17
  • 22
  • Encountered an error when execute (initialize-python! "./env/bin/python") "Execution error (ExceptionInfo) at tech.jna.base/do-load-library (base.clj:158).Failed to load library". I follow your procedures, encounter an error when using "len repl' and "clj". The difference is that I did a test with python 3.7.7. Thank you for your help. – madeinQuant May 24 '20 at 02:20
  • Your problem with the `encodings` module looks like [this issue](https://stackoverflow.com/q/38132755/483566). I created a gist with some changes in my project that you might want to try: https://gist.github.com/dfuenzalida/05eb0f6ba306386ace1a04eb22680352 ... The way I tested it was to open the project in Emacs with CIDER and jack-in to a new REPL. I don't think you need any symlinks if you have used virtualenv correctly. – Denis Fuenzalida May 24 '20 at 04:16
  • I got an error "Syntax error (ExceptionInfo) compiling at (libpython_clj/metadata.clj:13:1). Failed to load library". try to use docker instead. e,g. https://github.com/cnuernber/facial-rec – madeinQuant May 25 '20 at 03:53
  • 1
    I found this "https://github.com/clj-python/libpython-clj/issues/96". – madeinQuant May 25 '20 at 08:40