emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [babel] Setting python interpreter version on per-block or per-subtree basis
@ 2014-04-24 17:04 William Henney
  2014-04-25  1:59 ` Sacha Chua
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: William Henney @ 2014-04-24 17:04 UTC (permalink / raw)
  To: emacs-org

[-- Attachment #1: Type: text/plain, Size: 678 bytes --]

Hi

Is there an easy way to specify the python version to use for a particular
block or sub-tree?

My use case is that I have mainly migrated to python 3, but there is still
the occasional library that has not been updated yet, so I need to fall
back to python 2.7 for some tasks.

I can work around the problem by putting the python 2 code in a separate
org file and use

# Local Variables:
# org-babel-python-command: "/path/to/python2"
# End:

but keeping everything in the same file would be preferable.

Thanks

Will

-- 

  Dr William Henney, Centro de Radioastronomía y Astrofísica,
  Universidad Nacional Autónoma de México, Campus Morelia

[-- Attachment #2: Type: text/html, Size: 919 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [babel] Setting python interpreter version on per-block or per-subtree basis
  2014-04-24 17:04 [babel] Setting python interpreter version on per-block or per-subtree basis William Henney
@ 2014-04-25  1:59 ` Sacha Chua
  2014-04-25  9:00 ` Ian Barton
  2014-04-25 15:27 ` Eric Schulte
  2 siblings, 0 replies; 6+ messages in thread
From: Sacha Chua @ 2014-04-25  1:59 UTC (permalink / raw)
  To: emacs-orgmode

William Henney <whenney@gmail.com> writes:

Hello, Will!

> Is there an easy way to specify the python version to use for a particular
> block or sub-tree?

Is it something you can define an inherited property or a tag for, and
then add some advice around org-babel-execute:python to check that
property and use let to bind org-babel-python-comand?

Maybe something like this, for example:

#+begin_src emacs-lisp
(defadvice org-babel-execute:python (around will activate)
  (if (member "python2" (org-get-tags-at))
    (let ((org-babel-python-command "/path/to/python2"))
      ad-do-it)
    ad-do-it))
#+end_src
	
* Test :python2:
#+begin_src python
return 1 + 3
#+end_src

Sacha

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [babel] Setting python interpreter version on per-block or per-subtree basis
  2014-04-24 17:04 [babel] Setting python interpreter version on per-block or per-subtree basis William Henney
  2014-04-25  1:59 ` Sacha Chua
@ 2014-04-25  9:00 ` Ian Barton
  2014-04-25 15:27 ` Eric Schulte
  2 siblings, 0 replies; 6+ messages in thread
From: Ian Barton @ 2014-04-25  9:00 UTC (permalink / raw)
  To: William Henney, emacs-org


> Is there an easy way to specify the python version to use for a
> particular block or sub-tree?
>
> My use case is that I have mainly migrated to python 3, but there is
> still the occasional library that has not been updated yet, so I need to
> fall back to python 2.7 for some tasks.
>
> I can work around the problem by putting the python 2 code in a separate
> org file and use
>
> # Local Variables:
> # org-babel-python-command: "/path/to/python2"
> # End:
>

I think you can use shebang for this. It definitely works for tangling 
files:

#+begin_src python :shebang #!/usr/bin/python2 :tangle 
./raspberrypi/weather.py :exports none :noweb yes

#+end_src

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [babel] Setting python interpreter version on per-block or per-subtree basis
  2014-04-24 17:04 [babel] Setting python interpreter version on per-block or per-subtree basis William Henney
  2014-04-25  1:59 ` Sacha Chua
  2014-04-25  9:00 ` Ian Barton
@ 2014-04-25 15:27 ` Eric Schulte
  2014-04-25 16:29   ` William Henney
  2 siblings, 1 reply; 6+ messages in thread
From: Eric Schulte @ 2014-04-25 15:27 UTC (permalink / raw)
  To: William Henney; +Cc: emacs-org

[-- Attachment #1: Type: text/plain, Size: 298 bytes --]

The attached patch should allow the specification of the python command
through a new :python header argument.  E.g.,

#+begin_src python :python /path/to/python2
  return 1 + 2
#+end_src

If someone who actually uses python could confirm that it works as
expected then I'll be happy to apply it.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-allow-specification-of-python-command-w-header-arg.patch --]
[-- Type: text/x-diff, Size: 1022 bytes --]

From d57887adc70c524199b3307b74f17ea5751450f0 Mon Sep 17 00:00:00 2001
From: Eric Schulte <schulte.eric@gmail.com>
Date: Fri, 25 Apr 2014 09:24:04 -0600
Subject: [PATCH] allow specification of python command w/header arg

  Using the :python header arg.

* lisp/ob-python.el (org-babel-execute:python): Locally set
  `org-babel-python-command' using a header argument.
---
 lisp/ob-python.el | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index baa5764..eb25609 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -82,6 +82,8 @@ This function is called by `org-babel-execute-src-block'."
 	 (return-val (when (and (eq result-type 'value) (not session))
 		       (cdr (assoc :return params))))
 	 (preamble (cdr (assoc :preamble params)))
+	 (org-babel-python-command
+	  (or (cdr (assoc :python params)) org-babel-python-command))
          (full-body
 	  (org-babel-expand-body:generic
 	   (concat body (if return-val (format "\nreturn %s" return-val) ""))
-- 
1.9.2


[-- Attachment #3: Type: text/plain, Size: 677 bytes --]


Best,
Eric

William Henney <whenney@gmail.com> writes:

> Hi
>
> Is there an easy way to specify the python version to use for a particular
> block or sub-tree?
>
> My use case is that I have mainly migrated to python 3, but there is still
> the occasional library that has not been updated yet, so I need to fall
> back to python 2.7 for some tasks.
>
> I can work around the problem by putting the python 2 code in a separate
> org file and use
>
> # Local Variables:
> # org-babel-python-command: "/path/to/python2"
> # End:
>
> but keeping everything in the same file would be preferable.
>
> Thanks
>
> Will

-- 
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [babel] Setting python interpreter version on per-block or per-subtree basis
  2014-04-25 15:27 ` Eric Schulte
@ 2014-04-25 16:29   ` William Henney
  2014-04-25 18:04     ` Eric Schulte
  0 siblings, 1 reply; 6+ messages in thread
From: William Henney @ 2014-04-25 16:29 UTC (permalink / raw)
  To: Eric Schulte; +Cc: emacs-org


[-- Attachment #1.1: Type: text/plain, Size: 1846 bytes --]

Dear Sacha, Ian, and Eric

Thanks very much for your replies.  Sacha's way is a clever idea and works
fine, but I think Eric's patch is the best solution in the long term.
 Please see attached test file - the patch works perfectly.  Although I did
have to study the manual carefully to work out how get it to work using the
#+call: syntax. The key is to use "inside header arguments".  Ian's
solution with shebang works when tangling but not for direct evaluation of
the source block.

Cheers

Will




On Fri, Apr 25, 2014 at 10:27 AM, Eric Schulte <schulte.eric@gmail.com>wrote:

> The attached patch should allow the specification of the python command
> through a new :python header argument.  E.g.,
>
> #+begin_src python :python /path/to/python2
>   return 1 + 2
> #+end_src
>
> If someone who actually uses python could confirm that it works as
> expected then I'll be happy to apply it.
>
>
>
> Best,
> Eric
>
> William Henney <whenney@gmail.com> writes:
>
> > Hi
> >
> > Is there an easy way to specify the python version to use for a
> particular
> > block or sub-tree?
> >
> > My use case is that I have mainly migrated to python 3, but there is
> still
> > the occasional library that has not been updated yet, so I need to fall
> > back to python 2.7 for some tasks.
> >
> > I can work around the problem by putting the python 2 code in a separate
> > org file and use
> >
> > # Local Variables:
> > # org-babel-python-command: "/path/to/python2"
> > # End:
> >
> > but keeping everything in the same file would be preferable.
> >
> > Thanks
> >
> > Will
>
> --
> Eric Schulte
> https://cs.unm.edu/~eschulte
> PGP: 0x614CA05D
>
>


-- 

  Dr William Henney, Centro de Radioastronomía y Astrofísica,
  Universidad Nacional Autónoma de México, Campus Morelia

[-- Attachment #1.2: Type: text/html, Size: 2560 bytes --]

[-- Attachment #2: multi-python.org --]
[-- Type: application/octet-stream, Size: 3196 bytes --]

Test of Eric Schulte's patch to allow specifying the python interpreter via the =:python= header command. 

* Default python version
#+name: check-python-version
#+BEGIN_SRC python
import sys
return sys.version
#+END_SRC

#+RESULTS: check-python-version
: 3.3.5 |Anaconda 1.9.1 (x86_64)| (default, Mar 10 2014, 11:22:25) 
: [GCC 4.0.1 (Apple Inc. build 5493)]

This is the control experiment. 

* Custom python version
#+BEGIN_SRC python :python /Users/will/anaconda/envs/py27/bin/python
import sys
return sys.version
#+END_SRC

#+RESULTS:
: 2.7.5 |Anaconda 1.8.0 (x86_64)| (default, Oct 24 2013, 07:02:20) 
: [GCC 4.0.1 (Apple Inc. build 5493)]

This works.

* Test using call syntax
It is necessary to put the =:python= command in the /inside header arguments/, which go in square brackets before the function arguments. 

#+call: check-python-version[:python /Users/will/anaconda/envs/py27/bin/python]() 

#+RESULTS:
: 2.7.5 |Anaconda 1.8.0 (x86_64)| (default, Oct 24 2013, 07:02:20) 
: [GCC 4.0.1 (Apple Inc. build 5493)]

So that works fine, but beware that the following does not: 

#+call: check-python-version() :python /Users/will/anaconda/envs/py27/bin/python

#+RESULTS:
: 3.3.5 |Anaconda 1.9.1 (x86_64)| (default, Mar 10 2014, 11:22:25) 
: [GCC 4.0.1 (Apple Inc. build 5493)]


* Sacha Chua's alternative solution using a tag and advice

This lisp code must be evaluated first
#+begin_src emacs-lisp
(defadvice org-babel-execute:python (around will activate)
  (if (member "python2" (org-get-tags-at))
    (let ((org-babel-python-command "/Users/will/anaconda/envs/py27/bin/python"))
      ad-do-it)
    ad-do-it))
#+end_src

#+RESULTS:
: org-babel-execute:python

** Sacha direct test                                                :python2:
This works when the code block is directly evaluated
#+name: second-check-python-version
#+BEGIN_SRC python
import sys
return sys.version
#+END_SRC

#+RESULTS:
: 2.7.5 |Anaconda 1.8.0 (x86_64)| (default, Oct 24 2013, 07:02:20) 
: [GCC 4.0.1 (Apple Inc. build 5493)]

** Sacha call test one                                              :python2:
When calling another code block, what matters is that the tag is on the header where the block is defined, so this does not work
#+call: check-python-version()

#+RESULTS:
: 3.3.5 |Anaconda 1.9.1 (x86_64)| (default, Mar 10 2014, 11:22:25) 
: [GCC 4.0.1 (Apple Inc. build 5493)]


** Sacha call test two
But this does
#+call: second-check-python-version()

#+RESULTS:
: 2.7.5 |Anaconda 1.8.0 (x86_64)| (default, Oct 24 2013, 07:02:20) 
: [GCC 4.0.1 (Apple Inc. build 5493)]


* Ian Barton's alternative proposed solution with shebang
Does not work for direct evaluation with =C-c C-c=
#+begin_src python :shebang #!/Users/will/anaconda/envs/py27/bin/python :tangle py2-test.py :exports none :noweb yes :results output
import sys
print(sys.version)
#+end_src

#+RESULTS:
: 3.3.5 |Anaconda 1.9.1 (x86_64)| (default, Mar 10 2014, 11:22:25) 
: [GCC 4.0.1 (Apple Inc. build 5493)]

But does work with tangling to a file first
#+BEGIN_SRC sh :results verbatim
./py2-test.py
#+END_SRC

#+RESULTS:
: 2.7.5 |Anaconda 1.8.0 (x86_64)| (default, Oct 24 2013, 07:02:20) 
: [GCC 4.0.1 (Apple Inc. build 5493)]


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [babel] Setting python interpreter version on per-block or per-subtree basis
  2014-04-25 16:29   ` William Henney
@ 2014-04-25 18:04     ` Eric Schulte
  0 siblings, 0 replies; 6+ messages in thread
From: Eric Schulte @ 2014-04-25 18:04 UTC (permalink / raw)
  To: William Henney; +Cc: emacs-org

I've just applied this patch.  Thanks for the very attached nice test
and demonstration file.

Best,

William Henney <whenney@gmail.com> writes:

> Dear Sacha, Ian, and Eric
>
> Thanks very much for your replies.  Sacha's way is a clever idea and works
> fine, but I think Eric's patch is the best solution in the long term.
>  Please see attached test file - the patch works perfectly.  Although I did
> have to study the manual carefully to work out how get it to work using the
> #+call: syntax. The key is to use "inside header arguments".  Ian's
> solution with shebang works when tangling but not for direct evaluation of
> the source block.
>
> Cheers
>
> Will
>
>
>
>
> On Fri, Apr 25, 2014 at 10:27 AM, Eric Schulte <schulte.eric@gmail.com>wrote:
>
>> The attached patch should allow the specification of the python command
>> through a new :python header argument.  E.g.,
>>
>> #+begin_src python :python /path/to/python2
>>   return 1 + 2
>> #+end_src
>>
>> If someone who actually uses python could confirm that it works as
>> expected then I'll be happy to apply it.
>>
>>
>>
>> Best,
>> Eric
>>
>> William Henney <whenney@gmail.com> writes:
>>
>> > Hi
>> >
>> > Is there an easy way to specify the python version to use for a
>> particular
>> > block or sub-tree?
>> >
>> > My use case is that I have mainly migrated to python 3, but there is
>> still
>> > the occasional library that has not been updated yet, so I need to fall
>> > back to python 2.7 for some tasks.
>> >
>> > I can work around the problem by putting the python 2 code in a separate
>> > org file and use
>> >
>> > # Local Variables:
>> > # org-babel-python-command: "/path/to/python2"
>> > # End:
>> >
>> > but keeping everything in the same file would be preferable.
>> >
>> > Thanks
>> >
>> > Will
>>
>> --
>> Eric Schulte
>> https://cs.unm.edu/~eschulte
>> PGP: 0x614CA05D
>>
>>

-- 
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-04-25 18:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-24 17:04 [babel] Setting python interpreter version on per-block or per-subtree basis William Henney
2014-04-25  1:59 ` Sacha Chua
2014-04-25  9:00 ` Ian Barton
2014-04-25 15:27 ` Eric Schulte
2014-04-25 16:29   ` William Henney
2014-04-25 18:04     ` Eric Schulte

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).