clj-python / libpython-clj

Python bindings for Clojure
Eclipse Public License 2.0
1.08k stars 68 forks source link

Loading of (anaconda) python virtual env can fail when requireing namespaces in the wrong order #89

Closed orolle closed 4 years ago

orolle commented 4 years ago

Given Using anaconda in windows (or any virtual env), I have to specify :python-executable, :library-path and :windows-anaconda-activate-bat when initalize! libpython-clj so that libpython-clj finds the right python environment; else it will fail.

Cause When requiring libpython-clj.require it requires libpython-clj.metadata which calls initalize! without any options. Thus, if initalize! was not called before this call can fail or load the wrong virtual environment.

Workaround Create a namespace which is loaded before libpython-clj.metadata namespace, which then initialize! with the right parameters to load the right python virtual environment before libpython-clj.metadata is loaded.

(ns project-name.python
  (:require [libpython-clj.python :as py]))

(defonce
  libpython-clj
  (py/initialize!
    :python-executable "..."
    :library-path      "..."
    :windows-anaconda-activate-bat "..."))
jjtolton commented 4 years ago

You are doing it correctly!
I agree it would be more ideal if you could import libpython-clj.require and then do py/initialize! -- however, require-python needs to ask the Python runtime for a lot of information in order to correctly datafy the metadata. Check the offical template and you'll see that's exactly what we're doing!

In the future we're planning the ability to add a python.edn to be able to handle this so that the order of requirements are largely commutative. For now, your workaround is the accepted solution.

cnuernber commented 4 years ago

Closing. Open to better solution.