hylang / hy-mode

Hy mode for Emacs
GNU General Public License v3.0
189 stars 48 forks source link

Teaching hy-shell to set PYTHONPATH #109

Open igoralmeida opened 1 year ago

igoralmeida commented 1 year ago

Hy all :)

Thanks for the nice piece of software. Looking around hy-shell.el, I could not find a way to pass PYTHONPATH to the inferior process --- without a global setenv I guess, which I'd like to avoid since I have other REPLs running. Can you confirm if that's the case? I may simply be missing the correct configuration or required workflow.

If so, here's how I got it to work (this is on top of df81486 and hy 0.25.0):

diff --git a/hy-shell.el b/hy-shell.el
index 22927f2..104d643 100644
--- a/hy-shell.el
+++ b/hy-shell.el
@@ -51,6 +51,9 @@
 (defvar hy-shell--redirect-timeout 0.5
   "Seconds (float) to allow redirection commands to complete before quitting.")

+(defvar hy-shell-extra-pythonpaths nil
+  "Extra pythonpaths, a la `python-shell-extra-pythonpaths'.")
+
 ;;;; Managed

 (defconst hy-shell--name "Hy"
@@ -136,7 +139,12 @@
             (split-string-and-unquote (hy-shell--format-startup-command)))
            (name
             (if (hy-shell--internal?) hy-shell--name-internal hy-shell--name)))
-      (apply #'make-comint-in-buffer name nil program nil switches)
+
+      (let ((process-environment (cl-copy-list process-environment)))
+        (when hy-shell-extra-pythonpaths
+          (setenv "PYTHONPATH" (let ((python-shell-extra-pythonpaths hy-shell-extra-pythonpaths))
+                                 (python-shell-calculate-pythonpath))))
+        (apply #'make-comint-in-buffer name nil program nil switches))

       (unless (derived-mode-p 'inferior-hy-mode)
         (inferior-hy-mode))

Basically I abuse python-shell-calculate-pythonpath and override process-environment before starting the process. That way I can have a list of paths in hy-shell-extra-pythonpaths, set by dir-locals for example.

Thoughts?