From mboxrd@z Thu Jan 1 00:00:00 1970 From: William Henney Subject: Re: Mixing Python2 and Python3 blocks in one file Date: Thu, 19 May 2016 14:21:14 -0500 Message-ID: References: <2016-05-08T10-21-00@devnull.Karl-Voit.at> <2016-05-17T16-45-38@devnull.Karl-Voit.at> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=001a11475b22af5e37053336e0fe Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:37355) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3TVV-0006xu-CV for emacs-orgmode@gnu.org; Thu, 19 May 2016 15:21:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b3TVT-0005vt-1i for emacs-orgmode@gnu.org; Thu, 19 May 2016 15:21:16 -0400 Received: from mail-io0-x22c.google.com ([2607:f8b0:4001:c06::22c]:35815) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3TVS-0005vn-Qb for emacs-orgmode@gnu.org; Thu, 19 May 2016 15:21:14 -0400 Received: by mail-io0-x22c.google.com with SMTP id p64so28814086ioi.2 for ; Thu, 19 May 2016 12:21:14 -0700 (PDT) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Ken Mankoff Cc: Karl Voit , emacs-org --001a11475b22af5e37053336e0fe Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Thu, May 19, 2016 at 8:18 AM, Ken Mankoff wrote: > > Thanks for the example. This makes conda + Org work better than it has fo= r > me in the past. Unfortunately, one major issues is editing and running co= de > outside of Org via (org-edit-special). Perhaps elpy will support conda > environments soon. > > I think Elpy works fine with conda environments - you just have to call pyvenv-activate with the desired path. You have inspired me to try and automate this for org source blocks (see org snippets below). It is a bit clunky, but I got it to work. Let me know what you think, and any stylistic corrections are welcome (my lisp skills are sadly lacking). * Automating Elpy virtual env configuration in babel source editing buffer + This function will automatically activate the virtual environment that corresponds to the current python executable path + It should be a harmless no-op if we are not in a python buffer #+BEGIN_SRC emacs-lisp (defun wjh/elpy-pyvenv-activate-from-babel-info (info) "Activate relevant conda virtual env in Babel source editing buffer. The :python source block parameter is inspected to determine the venv." (let* ((python-command (or (cdr (assoc :python (nth 2 info))) org-babel-python-command)) (venv (replace-regexp-in-string "/bin/python$" "" python-command))) (when (string-match "/envs/" venv) (pyvenv-activate (expand-file-name venv))))) #+END_SRC + Then we need to make sure it is run whenever we edit a babel source block by doing something like =3D(wjh/elpy-pyvenv-activate-from-babel-info org-src--babel-info)=3D, and also to do =3D(pyvenv-deactivate)=3D when we l= eave + Unfortunately, there are no actual hooks that I can find for =3Dorg-edit-src-code=3D or =3Dorg-edit-src-exit=3D + For the activation part we can co-opt the =3Dorg-babel-edit-prep:python= =3D function: #+BEGIN_SRC emacs-lisp (defun org-babel-edit-prep:python (info) (wjh/elpy-pyvenv-activate-from-babel-info info)) #+END_SRC + Although this will break if =3Dob-python.el=3D decides to define =3Dorg-babel-edit-prep:python=3D one day + For the deactivation part, I can't see any other way than to use advice #+BEGIN_SRC emacs-lisp (advice-add 'org-edit-src-exit :after #'pyvenv-deactivate) #+END_SRC + One thing we have to remember is to install the elpy python package and dependencies in each virtual environment. For instance: #+BEGIN_SRC sh source activate myenv pip install elpy rope_py3k importmagic autopep8 yapf jedi #+END_SRC --=20 Dr William Henney, Instituto de Radioastronom=C3=ADa y Astrof=C3=ADsica, Universidad Nacional Aut=C3=B3noma de M=C3=A9xico, Campus Morelia --001a11475b22af5e37053336e0fe Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On Thu, May 19, 2016 at 8:18 AM, Ken Mankoff <mankoff@gma= il.com> wrote:

Thanks for the example. This makes conda + Org work better than it has for = me in the past. Unfortunately, one major issues is editing and running code= outside of Org via (org-edit-special). Perhaps elpy will support conda env= ironments soon.


I think Elpy w= orks fine with conda environments - you just have to call pyvenv-activate w= ith the desired path.=C2=A0

You have inspired me t= o try and automate this for org source blocks (see org snippets below).=C2= =A0 It is a bit clunky, but I got it to work.=C2=A0 Let me know what you th= ink, and any stylistic corrections are welcome (my lisp skills are sadly la= cking). =C2=A0=C2=A0
=C2=A0


* Automating Elpy virtual env configuration in babel source editing= buffer
+ This function will automatically activate the virtual e= nvironment that corresponds to the current python executable path
+ It should be a harmless no-op if we are not in a python buffer
=C2=A0 #+BEGIN_SRC emacs-lisp
=C2=A0 =C2=A0 (defun wjh/elpy-pyve= nv-activate-from-babel-info (info)
=C2=A0 =C2=A0 =C2=A0 "Act= ivate relevant conda virtual env in Babel source editing buffer.
= =C2=A0 =C2=A0 The :python source block parameter is inspected to determine = the venv."
=C2=A0 =C2=A0 =C2=A0 (let* ((python-command (or (= cdr (assoc :python (nth 2 info)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0org-babel-python-command))
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0(venv (replace-regexp-in-string "/bin/python$&= quot; "" python-command)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = (when (string-match "/envs/" venv)
=C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 (pyvenv-activate (expand-file-name venv)))))
=C2= =A0 #+END_SRC
+ Then we need to make sure it is run whenever we e= dit a babel source block by doing something like =3D(wjh/elpy-pyvenv-activa= te-from-babel-info org-src--babel-info)=3D, and also to do =3D(pyvenv-deact= ivate)=3D when we leave
+ Unfortunately, there are no actual hook= s that I can find for =3Dorg-edit-src-code=3D or =3Dorg-edit-src-exit=3D
+ For the activation part we can co-opt the =3Dorg-babel-edit-prep:= python=3D function:
=C2=A0 #+BEGIN_SRC emacs-lisp
=C2= =A0 =C2=A0 (defun org-babel-edit-prep:python (info)
=C2=A0 =C2=A0= =C2=A0 (wjh/elpy-pyvenv-activate-from-babel-info info))
=C2=A0 #= +END_SRC
=C2=A0 + Although this will break if =3Dob-python.el=3D = decides to define =3Dorg-babel-edit-prep:python=3D one day
+ For = the deactivation part, I can't see any other way than to use advice
=C2=A0 #+BEGIN_SRC emacs-lisp
=C2=A0 =C2=A0 (advice-add &#= 39;org-edit-src-exit :after #'pyvenv-deactivate)
=C2=A0 #+END= _SRC
+ One thing we have to remember is to install the elpy pytho= n package and dependencies in each virtual environment.=C2=A0 For instance:=
=C2=A0 #+BEGIN_SRC sh
=C2=A0 source activate myenv
=C2=A0 pip install elpy rope_py3k importmagic autopep8 yapf jedi
=C2=A0 #+END_SRC

=C2=A0

--
=C2=A0 Dr William Henney, Instituto de Radioastronom=C3=ADa y Astrof=C3= =ADsica,
=C2=A0 Universidad Nacional Aut=C3=B3noma de M=C3=A9xico, Campu= s Morelia
--001a11475b22af5e37053336e0fe--