emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Tom Gillespie <tgbugs@gmail.com>
To: Jack Kamm <jackkamm@gmail.com>
Cc: Joost Kremers <joostkremers@fastmail.fm>,
	Maxim Nikulin <manikulin@gmail.com>,
	emacs-orgmode <emacs-orgmode@gnu.org>
Subject: Re: ob-python: import local package into a session
Date: Tue, 24 Nov 2020 13:40:42 -0500	[thread overview]
Message-ID: <CA+G3_PNMe6O_PM5Zj=uOyLzNSXYY+kNDjsuBcmgqaxD2ZJwJTg@mail.gmail.com> (raw)
In-Reply-To: <87d0026deg.fsf@gmail.com>

I have also been dissatisfied with the current options for making
local python libraries accessible in certain org files. The amount of
setup that is required outside the org file itself was too large,
especially if you want someone else who is not intimately familiar
with python to be able to use it. My old solution was to modify the
PYTHONPATH environment variable for the whole Emacs process. However,
after a bit of digging inspired by this thread I now have a solution
that is entirely local: advise ~org-babel-execute:python~ to set a new
PYTHONPATH via the ~process-environment~ dynamic variable and set that
from a buffer local variable for the local additions to PYTHONPATH
along with getenv PYTHONPATH. A working example below. Best!
Tom

#+name: orgstrap
#+begin_src elisp :results none :noweb noexport
(defvar-local local-python-path nil)

(defun advise--obe-python-path (command &rest args)
  (let ((process-environment
         (or (and local-python-path
                  (cons
                   (format
                    "PYTHONPATH=%s"
                    (concat local-python-path (getenv "PYTHONPATH")))
                   process-environment))
             process-environment)))
    (apply command args)))

(advice-add #'org-babel-execute:python :around
            #'advise--obe-python-path)

(setq-local local-python-path (concat default-directory "code:"))
#+end_src

On Tue, Nov 24, 2020 at 9:26 AM Jack Kamm <jackkamm@gmail.com> wrote:
>
> Joost Kremers <joostkremers@fastmail.fm> writes:
>
> > I haven't really considered the option to install the utility functions as a
> > package in the virtual environment, because I expect to change and develop those
> > functions together with the rest of the project. If it were a separate package,
> > I'd need to reinstall it every time I make changes to it, which will probably
> > happen often.
>
> If you install the package using either "python setup.py develop", or
> "pip install -e", then Python will install your code via symlinks
> instead of copying, so then you don't have to worry about reinstalling
> every time you make an edit.
>
> To switch between venv's in emacs, I use pyvenv:
> https://github.com/jorgenschaefer/pyvenv
>


  reply	other threads:[~2020-11-24 18:42 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-23 15:46 ob-python: import local package into a session Joost Kremers
2020-11-23 16:03 ` John Kitchin
2020-11-23 16:20 ` Jack Kamm
2020-11-23 16:21   ` Jack Kamm
2020-11-23 22:47   ` Joost Kremers
2020-11-24 12:23 ` Maxim Nikulin
2020-11-24 13:35   ` Joost Kremers
2020-11-24 14:24     ` Jack Kamm
2020-11-24 18:40       ` Tom Gillespie [this message]
2020-11-25 12:58       ` Joost Kremers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CA+G3_PNMe6O_PM5Zj=uOyLzNSXYY+kNDjsuBcmgqaxD2ZJwJTg@mail.gmail.com' \
    --to=tgbugs@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=jackkamm@gmail.com \
    --cc=joostkremers@fastmail.fm \
    --cc=manikulin@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public 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).