Function: python-shell-with-environment
python-shell-with-environment is a macro defined in python.el.gz.
Signature
(python-shell-with-environment &rest BODY)
Documentation
Modify shell environment during execution of BODY.
Temporarily sets process-environment and exec-path(var)/exec-path(fun) during
execution of body. If default-directory points to a remote
machine then modifies tramp-remote-process-environment and
python-shell-remote-exec-path instead.
Source Code
;; Defined in /usr/src/emacs/lisp/progmodes/python.el.gz
(defmacro python-shell-with-environment (&rest body)
"Modify shell environment during execution of BODY.
Temporarily sets `process-environment' and `exec-path' during
execution of body. If `default-directory' points to a remote
machine then modifies `tramp-remote-process-environment' and
`python-shell-remote-exec-path' instead."
(declare (indent 0) (debug (body)))
(let ((vec (make-symbol "vec")))
`(progn
(let* ((,vec
(when (file-remote-p default-directory)
(ignore-errors
(tramp-dissect-file-name default-directory 'noexpand))))
(process-environment
(if ,vec
process-environment
(python-shell-calculate-process-environment)))
(exec-path
(if ,vec
exec-path
(python-shell-calculate-exec-path)))
(tramp-remote-process-environment
(if ,vec
(python-shell-calculate-process-environment)
tramp-remote-process-environment)))
(when (tramp-get-connection-process ,vec)
;; For already existing connections, the new exec path must
;; be re-set, otherwise it won't take effect. One example
;; of such case is when remote dir-locals are read and
;; *then* subprocesses are triggered within the same
;; connection.
(python-shell-tramp-refresh-remote-path
,vec (python-shell-calculate-exec-path))
;; The `tramp-remote-process-environment' variable is only
;; effective when the started process is an interactive
;; shell, otherwise (like in the case of processes started
;; with `process-file') the environment is not changed.
;; This makes environment modifications effective
;; unconditionally.
(python-shell-tramp-refresh-process-environment
,vec tramp-remote-process-environment))
,(macroexp-progn body)))))