Skip to content

Commit 5dfc96c

Browse files
committed
documented uv support
1 parent 65fa30a commit 5dfc96c

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ a.out
2323
pregen-ffi-test
2424
.calva
2525
.lock
26+
pyproject.toml
27+
uv.lock

topics/environments.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,50 @@ Conda requires that we set the LD_LIBRARY_PATH to the conda install.
1414

1515
* [example conda repl launcher](https://github.com/clj-python/libpython-clj/blob/master/scripts/conda-repl)
1616
* [libpython-clj issue for Conda](https://github.com/clj-python/libpython-clj/issues/18)
17+
18+
19+
20+
## uv
21+
22+
When you are using the (awesome !) python package manager [uv](https://docs.astral.sh/uv/) we provide a nice integration, which allows to auto-mange
23+
declarative python environments.
24+
25+
Assuming that you have 'uv' installed (it exists for Linux, Windows , Mac) you can specify and auto-setup a local python venv incl. python version by adding the following to `python.edn`
26+
(Linux example)
27+
28+
```
29+
:python-version "3.10.16"
30+
:python-deps ["openai==1.58.1"
31+
"langextract"]
32+
:python-executable ".venv/bin/python"
33+
:pre-initialize-fn libpython-clj2.python.uv/sync-python-setup!
34+
```
35+
36+
The versions specification takes the same values as in uv, so would allow ranges a swell, for examples. We suggest to use precise versions, if possible
37+
38+
Having this, on a call to `(py/initialize!)` a python venv will be created/updated to match the python version and the specified packages. This calls behind the scenes `uv sync` so the spec and the venv are "brought in sync".
39+
40+
Re-syncing can be as well called manually (while the Clojure repl runs), invoking directly `(libpython-clj2.python.uv/sync-python-setup!)`
41+
42+
### ux on Windows
43+
44+
On Windows we need to use:
45+
`:python-executable ".venv/Scripts/python"`
46+
47+
as the python executable.
48+
49+
### Caveat
50+
51+
We have noticed that under Windows for some python versions `libpython-clj` does not setup the python library path correctly, resulting in python libraries not found using for example: `(py/import-module "xxx")`
52+
53+
This is visible by inspecting python `sys.path`, which should contain `.venv/` via `(py/run-simple-string "import sys; print(sys.path)")`,
54+
55+
`sys.path` should contain something like
56+
`c:\\Users\\behrica\\Repos\\tryLangExtract\\.venv`
57+
58+
This can be fixed as by running after `(py/initialize!)` the following:
59+
60+
Windows: `(py/run-simple-string "import sys; sys.path.append('.venv/Lib/site-packages')")`
61+
Linux: `(py/run-simple-string "import sys; sys.path.append('/.venv/lib/<python_version>/site-packages')")`
62+
63+
Not sure, if the precise paths can change across python versions. They can be discovered by looking into `.venv` directory and see where precisely the "site-packages" directory is located.

0 commit comments

Comments
 (0)