Do you want the whole stacktrace? Or are you fine with just the error message?
Override this function to your heart's content:
(advice-add #'org-babel-python-send-string
:override
#'my/org-babel-python-send-string)
(defun my/org-babel-python-send-string (session body)
"Pass BODY to the Python process in SESSION.
Return output."
(with-current-buffer session
(let* ((string-buffer "")
(comint-output-filter-functions
(cons (lambda (text) (setq string-buffer
(concat string-buffer text)))
comint-output-filter-functions))
(body (format "\
try:
%s
except Exception as e:
# These are the relevant lines to change.
print(e)
finally:
print('%s')"
(org-babel-python--shift-right body 4)
org-babel-python-eoe-indicator)))
(let ((python-shell-buffer-name
(org-babel-python-without-earmuffs session)))
(python-shell-send-string body))
;; same as `python-shell-comint-end-of-output-p' in emacs-25.1+
(while (not (and (python-shell-comint-end-of-output-p string-buffer)
(string-match
org-babel-python-eoe-indicator
string-buffer)))
(accept-process-output (get-buffer-process (current-buffer))))
(org-babel-chomp (substring string-buffer 0 (match-beginning 0))))))
You'll also need to change:
#+PROPERTY: header-args :session my_py_session
to
#+PROPERTY: header-args :session my_py_session :results output
Insert the appropriate python code if you want the whole stacktrace.