clj-python / libpython-clj

Python bindings for Clojure
Eclipse Public License 2.0
1.06k stars 69 forks source link

"Failed to load library" when library-path exists #117

Closed eoswald closed 4 years ago

eoswald commented 4 years ago

I'm on Windows 10 and it seems like it's failing to find the provided python library path when the DLL clearly exists.

project.clj:

(defproject libpython-test "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.0"]
                 [clj-python/libpython-clj "1.45"]]
  :main libpython-test.core
  :repl-options {:init-ns libpython-test.core})

core.clj:

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

(defn -main
  [& args]
  (py/initialize! :python-executable "C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe"
                  :library-path "C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll"))

Output:

PS C:\Users\eoswald\tests\libpython-test> ls C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe

    Directory: C:\Users\eoswald\AppData\Local\Programs\Python\Python37-32

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       10/20/2018   2:07 PM          97432 python.exe

PS C:\Users\eoswald\tests\libpython-test> ls C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll

    Directory: C:\Users\eoswald\AppData\Local\Programs\Python\Python37-32

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       10/20/2018   2:05 PM        3632792 python37.dll

PS C:\Users\eoswald\tests\libpython-test> lein run
Sep 01, 2020 12:42:06 PM clojure.tools.logging$eval513$fn__516 invoke
INFO: Executing python initialize with options:{:python-executable "C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe", :program-name nil, :python-home nil, :library-path "C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll"}
Sep 01, 2020 12:42:06 PM clojure.tools.logging$eval513$fn__516 invoke
INFO: Detecting startup-info for Python executable: C:\Users\eoswald\AppData\Local\Programs\Python\Python37-32\python.exe
Sep 01, 2020 12:42:06 PM clojure.tools.logging$eval513$fn__516 invoke
INFO: Startup info detected:
{:lib-version "3.7",
 :java-library-path-addendum
 "C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\lib",
 :exec-prefix
 "C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32",
 :executable
 "C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe",
 :libnames
 ("C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll"
  "python3.7"),
 :prefix
 "C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32",
 :base-prefix
 "C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32",
 :base-exec-prefix
 "C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32",
 :python-home
 "C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32",
 :version [3 7 1],
 :platform "win32"}

Sep 01, 2020 12:42:06 PM clojure.tools.logging$eval513$fn__516 invoke
INFO: Trying python library names ["C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "python3.7" "python3.7m" "python3.6m"]
Sep 01, 2020 12:42:06 PM clojure.tools.logging$eval513$fn__516 invoke
INFO: Setting java library path: C:\Users\eoswald\AppData\Local\Programs\Python\Python37-32\lib:C:\jdk-13.0.2\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Git\cmd;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;D:\Program Files\PuTTY\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Users\eoswald\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\php;C:\jdk-13.0.2\bin;C:\bin;C:\Users\eoswald\AppData\Local\Microsoft\WindowsApps;C:\Users\eoswald\AppData\Local\Programs\Python\Python37-32;C:\Hugo\bin;C:\Users\eoswald\AppData\Local\Microsoft\WindowsApps;C:\Users\eoswald\AppData\Local\atom\bin;C:\Users\eoswald\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\Users\eoswald\AppData\Local\Programs\Microsoft VS Code\bin;;.
Sep 01, 2020 12:42:06 PM clojure.tools.logging$eval513$fn__516 invoke
INFO: Reference thread starting
Exception in thread "main" Syntax error compiling at (C:\Users\eoswald\AppData\Local\Temp\form-init7661073140349624748.clj:1:116).
Failed to load library
        at clojure.lang.Compiler.load(Compiler.java:7647)
        at clojure.lang.Compiler.loadFile(Compiler.java:7573)
        at clojure.main$load_script.invokeStatic(main.clj:452)
        at clojure.main$init_opt.invokeStatic(main.clj:454)
        at clojure.main$init_opt.invoke(main.clj:454)
        at clojure.main$initialize.invokeStatic(main.clj:485)
        at clojure.main$null_opt.invokeStatic(main.clj:519)
        at clojure.main$null_opt.invoke(main.clj:516)
        at clojure.main$main.invokeStatic(main.clj:598)
        at clojure.main$main.doInvoke(main.clj:561)
        at clojure.lang.RestFn.applyTo(RestFn.java:137)
        at clojure.lang.Var.applyTo(Var.java:705)
        at clojure.main.main(main.java:37)
Caused by: clojure.lang.ExceptionInfo: Failed to load library {:libname "python3.6m", :paths [[:system ["python3.6m"]] [:java-library-path ["C\\python3.6m.dll" "\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\python3.6m.dll" "C\\python3.6m.dll" "\\jdk-13.0.2\\bin;C\\python3.6m.dll" "\\WINDOWS\\Sun\\Java\\bin;C\\python3.6m.dll" "\\WINDOWS\\system32;C\\python3.6m.dll" "\\WINDOWS;C\\python3.6m.dll" "\\Windows\\system32;C\\python3.6m.dll" "\\Windows;C\\python3.6m.dll" "\\Windows\\System32\\Wbem;C\\python3.6m.dll" "\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C\\python3.6m.dll" "\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C\\python3.6m.dll" "\\Program Files (x86)\\Git\\cmd;C\\python3.6m.dll" "\\WINDOWS\\system32;C\\python3.6m.dll" "\\WINDOWS;C\\python3.6m.dll" "\\WINDOWS\\System32\\Wbem;C\\python3.6m.dll" "\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;D\\python3.6m.dll" "\\Program Files\\PuTTY\\;C\\python3.6m.dll" "\\WINDOWS\\System32\\OpenSSH\\;C\\python3.6m.dll" "\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C\\python3.6m.dll" "\\WINDOWS\\system32;C\\python3.6m.dll" "\\WINDOWS;C\\python3.6m.dll" "\\WINDOWS\\System32\\Wbem;C\\python3.6m.dll" "\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C\\python3.6m.dll" "\\WINDOWS\\System32\\OpenSSH\\;C\\python3.6m.dll" "\\Users\\eoswald\\AppData\\Roaming\\nvm;C\\python3.6m.dll" "\\Program Files\\nodejs;C\\python3.6m.dll" "\\php;C\\python3.6m.dll" "\\jdk-13.0.2\\bin;C\\python3.6m.dll" "\\bin;C\\python3.6m.dll" "\\Users\\eoswald\\AppData\\Local\\Microsoft\\WindowsApps;C\\python3.6m.dll" "\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32;C\\python3.6m.dll" "\\Hugo\\bin;C\\python3.6m.dll" "\\Users\\eoswald\\AppData\\Local\\Microsoft\\WindowsApps;C\\python3.6m.dll" "\\Users\\eoswald\\AppData\\Local\\atom\\bin;C\\python3.6m.dll" "\\Users\\eoswald\\AppData\\Roaming\\nvm;C\\python3.6m.dll" "\\Program Files\\nodejs;C\\python3.6m.dll" "\\Users\\eoswald\\AppData\\Local\\Programs\\Microsoft VS Code\\bin;;.\\python3.6m.dll"]]]}
        at tech.jna.base$do_load_library.invokeStatic(base.clj:158)
        at tech.jna.base$do_load_library.invoke(base.clj:123)
        at tech.jna.base$load_library.invokeStatic(base.clj:178)
        at tech.jna.base$load_library.invoke(base.clj:174)
        at tech.jna$load_library.invokeStatic(jna.clj:87)
        at tech.jna$load_library.invoke(jna.clj:85)
        at libpython_clj.python.interpreter$setup_direct_mapping_BANG_.invokeStatic(interpreter.clj:430)
        at libpython_clj.python.interpreter$setup_direct_mapping_BANG_.invoke(interpreter.clj:428)
        at libpython_clj.python.interpreter$initialize_BANG_.invokeStatic(interpreter.clj:464)
        at libpython_clj.python.interpreter$initialize_BANG_.doInvoke(interpreter.clj:434)
        at clojure.lang.RestFn.invoke(RestFn.java:619)
        at libpython_clj.python$initialize_BANG_.invokeStatic(python.clj:242)
        at libpython_clj.python$initialize_BANG_.doInvoke(python.clj:229)
        at clojure.lang.RestFn.invoke(RestFn.java:457)
        at libpython_test.core$_main.invokeStatic(core.clj:6)
        at libpython_test.core$_main.doInvoke(core.clj:4)
        at clojure.lang.RestFn.invoke(RestFn.java:397)
        at clojure.lang.Var.invoke(Var.java:380)
        at user$eval140.invokeStatic(form-init7661073140349624748.clj:1)
        at user$eval140.invoke(form-init7661073140349624748.clj:1)
        at clojure.lang.Compiler.eval(Compiler.java:7176)
        at clojure.lang.Compiler.eval(Compiler.java:7166)
        at clojure.lang.Compiler.load(Compiler.java:7635)
        ... 12 more
cnuernber commented 4 years ago

Agree this is legitimate; it should have loaded the library. I wonder if there is some linkage issue when it attempts to load the library.

What is the return value of:

(tech.jna/load-library "C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll")
eoswald commented 4 years ago

That throws the same exception:

Exception in thread "main" Syntax error compiling at (C:\Users\eoswald\AppData\Local\Temp\form-init6781053897180991248.clj:1:116).
Failed to load library
        at clojure.lang.Compiler.load(Compiler.java:7647)
        at clojure.lang.Compiler.loadFile(Compiler.java:7573)
        at clojure.main$load_script.invokeStatic(main.clj:452)
        at clojure.main$init_opt.invokeStatic(main.clj:454)
        at clojure.main$init_opt.invoke(main.clj:454)
        at clojure.main$initialize.invokeStatic(main.clj:485)
        at clojure.main$null_opt.invokeStatic(main.clj:519)
        at clojure.main$null_opt.invoke(main.clj:516)
        at clojure.main$main.invokeStatic(main.clj:598)
        at clojure.main$main.doInvoke(main.clj:561)
        at clojure.lang.RestFn.applyTo(RestFn.java:137)
        at clojure.lang.Var.applyTo(Var.java:705)
        at clojure.main.main(main.java:37)
Caused by: clojure.lang.ExceptionInfo: Failed to load library {:libname "C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll", :paths [[:system ["C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll"]] [:java-library-path ["C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\jdk-13.0.2\\bin;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\WINDOWS\\Sun\\Java\\bin;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\WINDOWS\\system32;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\WINDOWS;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Windows\\system32;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Windows;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Windows\\System32\\Wbem;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Program Files (x86)\\Git\\cmd;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\WINDOWS\\system32;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\WINDOWS;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\WINDOWS\\System32\\Wbem;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;D\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Program Files\\PuTTY\\;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\WINDOWS\\System32\\OpenSSH\\;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\WINDOWS\\system32;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\WINDOWS;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\WINDOWS\\System32\\Wbem;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\WINDOWS\\System32\\OpenSSH\\;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Users\\eoswald\\AppData\\Roaming\\nvm;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Program Files\\nodejs;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\php;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\jdk-13.0.2\\bin;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\bin;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Users\\eoswald\\AppData\\Local\\Microsoft\\WindowsApps;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Hugo\\bin;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Users\\eoswald\\AppData\\Local\\Microsoft\\WindowsApps;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Users\\eoswald\\AppData\\Local\\atom\\bin;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Users\\eoswald\\AppData\\Roaming\\nvm;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Program Files\\nodejs;C\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll" "\\Users\\eoswald\\AppData\\Local\\Programs\\Microsoft VS Code\\bin;;.\\C:\\Users\\eoswald\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.dll"]]]}
        at tech.jna.base$do_load_library.invokeStatic(base.clj:158)
        at tech.jna.base$do_load_library.invoke(base.clj:123)
        at tech.jna.base$load_library.invokeStatic(base.clj:178)
        at tech.jna.base$load_library.invoke(base.clj:174)
        at tech.jna$load_library.invokeStatic(jna.clj:87)
        at tech.jna$load_library.invoke(jna.clj:85)
        at libpython_test.core$_main.invokeStatic(core.clj:6)
        at libpython_test.core$_main.doInvoke(core.clj:4)
        at clojure.lang.RestFn.invoke(RestFn.java:397)
        at clojure.lang.Var.invoke(Var.java:380)
        at user$eval140.invokeStatic(form-init6781053897180991248.clj:1)
        at user$eval140.invoke(form-init6781053897180991248.clj:1)
        at clojure.lang.Compiler.eval(Compiler.java:7176)
        at clojure.lang.Compiler.eval(Compiler.java:7166)
        at clojure.lang.Compiler.load(Compiler.java:7635)
        ... 12 more
cnuernber commented 4 years ago

What happens if you load the dll with dependency walker?

Basically the loadlibrary call has to work. And it is pretty low level. I was hoping for more information in the exception like exactly why the library isn't loading.

eoswald commented 4 years ago

I was able to crash dependency walker and found my issue: I accidentally installed a 32-bit version of Python. Installing a 64-bit version worked. I tried re-installing a 32-bit version and was able to reproduce this issue with Python 3.7 and Python 3.8.

cnuernber commented 4 years ago

Awesome, glad I could help :-).