Function: org-babel-python-initiate-session-by-key

org-babel-python-initiate-session-by-key is a byte-compiled function defined in ob-python.el.gz.

Signature

(org-babel-python-initiate-session-by-key &optional SESSION)

Documentation

Initiate a python session.

If there is not a current inferior-process-buffer matching SESSION then create it. If inferior process already exists (e.g. if it was manually started with run-python), make sure it's configured to work with ob-python. If session has already been configured as such, do nothing. Return the initialized session.

Source Code

;; Defined in /usr/src/emacs/lisp/org/ob-python.el.gz
(defun org-babel-python-initiate-session-by-key (&optional session)
  "Initiate a python session.
If there is not a current inferior-process-buffer matching
SESSION then create it.  If inferior process already
exists (e.g. if it was manually started with `run-python'), make
sure it's configured to work with ob-python.  If session has
already been configured as such, do nothing.  Return the
initialized session."
  (save-window-excursion
    (let* ((session (if session (intern session) :default))
           (py-buffer (org-babel-session-buffer:python session))
           (python-shell-buffer-name
	    (org-babel-python-without-earmuffs py-buffer))
           (existing-session-p (comint-check-proc py-buffer))
           (cmd (org-babel-python--command t)))
      (if cmd
          (let* ((cmd-split (split-string-and-unquote cmd))
                 (python-shell-interpreter (car cmd-split))
                 (python-shell-interpreter-args
                  (combine-and-quote-strings
                   (append (cdr cmd-split)
                           (when (member system-type
                                         '(cygwin windows-nt ms-dos))
                             (list "-i"))))))
            (run-python))
        (run-python))
      (with-current-buffer py-buffer
        (if existing-session-p
            ;; Session was created outside Org.  Assume first prompt
            ;; already happened; run session setup code directly
            (unless org-babel-python--initialized
              ;; Ensure first prompt. Based on python-tests.el
              ;; (`python-tests-shell-wait-for-prompt')
              (while (not (org-babel-python--python-util-comint-end-of-output-p))
                (sit-for 0.1))
              (org-babel-python--setup-session))
          ;; Adding to `python-shell-first-prompt-hook' immediately
          ;; after `run-python' should be safe from race conditions,
          ;; because subprocess output only arrives when Emacs is
          ;; waiting (see elisp manual, "Output from Processes")
          (add-hook
           'python-shell-first-prompt-hook
           #'org-babel-python--setup-session
           nil 'local)))
      ;; Wait until Python initializes
      ;; This is more reliable compared to
      ;; `org-babel-comint-wait-for-output' as python may emit
      ;; multiple prompts during initialization.
      (with-current-buffer py-buffer
        (while (not org-babel-python--initialized)
          (sleep-for 0.010)))
      (setq org-babel-python-buffers
	    (cons (cons session py-buffer)
		  (assq-delete-all session org-babel-python-buffers)))
      session)))