Skip to content

Commit d5588a4

Browse files
authored
ISSUE-223: Allow py/initialize! settings to be read from a python.edn file (#226)
* Allow "py/initialize!" settings to be read from a `python.edn` file * add documentation * add missing paren, make flycheck happy
1 parent bfd5646 commit d5588a4

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Time for a ChangeLog!
2+
## 2.021
3+
* Add support for a `python.edn` file, see `libpython-clj2.python/initialize!` documentation
4+
25
## 2.020
36
* Better support for running with `-Dlibpython_clj.manual_gil=true` - `with-manual-gil`.
47
Addresses [issue 221](https://github.com/clj-python/libpython-clj/issues/221).

src/libpython_clj2/python.clj

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ user> (py/py. np linspace 2 3 :num 10)
3434
[libpython-clj2.python.windows :as win]
3535
[tech.v3.datatype.ffi :as dtype-ffi]
3636
[tech.v3.datatype.errors :as errors]
37-
[clojure.tools.logging :as log])
37+
[clojure.tools.logging :as log]
38+
clojure.edn)
3839
(:import [java.util Map List]
3940
[clojure.lang IFn]))
4041

@@ -47,6 +48,26 @@ user> (py/py. np linspace 2 3 :num 10)
4748
"Initialize the python library. If library path is not provided, then the system
4849
attempts to execute a simple python program and have python return system info.
4950
51+
Note: all of the options passed to `initialize!` may now be provided in
52+
a root-level `python.edn` file. Example:
53+
54+
```
55+
;; python.edn
56+
{:python-executable \"/usr/bin/python3.7\"
57+
:python-library-path \"/usr/lib/libpython3.7m.so\"
58+
:python-home \"/usr/lib/python3.7\"
59+
:python-verbose true}
60+
```
61+
or, using a local virtual environment:
62+
```
63+
;; python.edn
64+
{:python-executable \"env/bin/python\"}
65+
```
66+
67+
The file MUST be named `python.edn` and be in the root of the classpath.
68+
With a `python.edn` file in place, the `initialize!` function may be called
69+
with no arguments and the options will be read from the file. If arguments are
70+
passed to `initialize!` then they will override the values in the file.
5071
5172
Returns either `:ok` in which case the initialization completed successfully or
5273
`:already-initialized` in which case we detected that python has already been
@@ -72,8 +93,12 @@ user> (py/py. np linspace 2 3 :num 10)
7293
no-io-redirect?]
7394
:as options}]
7495
(if-not (and (py-ffi/library-loaded?)
75-
(= 1 (py-ffi/Py_IsInitialized)))
76-
(let [info (py-info/detect-startup-info options)
96+
(= 1 (py-ffi/Py_IsInitialized)))
97+
(let [python-edn-opts (-> (try (slurp "python.edn")
98+
(catch java.io.FileNotFoundException _ "{}"))
99+
clojure.edn/read-string)
100+
options (merge python-edn-opts options)
101+
info (py-info/detect-startup-info options)
77102
_ (log/infof "Startup info %s" info)
78103
_ (when-let [lib-path (:java-library-path-addendum
79104
options (:java-library-path-addendum info))]

0 commit comments

Comments
 (0)