unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Justin Timmons <justinmtimmons@gmail.com>
To: Pim <pim.remkes@upcmail.nl>
Cc: 24464@debbugs.gnu.org
Subject: bug#24464: 25.1; python.el virtualenv
Date: Sun, 11 Dec 2016 19:39:56 -0500	[thread overview]
Message-ID: <87h96akmjn.fsf@gmail.com> (raw)
In-Reply-To: <a23f9de6-dab0-61ae-321b-8aa75af0c097@upcmail.nl> (Pim's message of "Mon, 19 Sep 2016 00:52:36 +0200")

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


It looks like this was caused by an incorrect assumption on the
directory structure of the virtualenv directory. According to the
virtualenv docs only POSIX systems follow the structure
"/path/to/venv/bin/", while windows systems use "/path/to/venv/Scripts"
for the location of the binary files, most importantly including the
python interpreter (see:
https://virtualenv.pypa.io/en/stable/userguide/#windows-notes).

This meant that the wrong path was added to `exec-path` and so we were
starting the python shell installed on the system, rather than the one
bundled inside of the virtualenv.

This should be addressed in the attached patch file - it just adds a
special case for Windows and prepends that path to the system PATH.

I also updated the docs for the `python-shell-calculate-exec-path`
function to make it more clear that using the python interpreter bundled
in the virtualenv will automatically set the PYTHONPATH to point at the
libraries inside of the virtualenv - something that was not immediately
obvious to me.



Steps to verify that patch is working correctly:
  1. (On Windows)
  2. Create virtualenv (eg. 'virtualenv ~/venv')
  3. Install a library NOT currently installed on the your system inside
  the virtualenv. ex:
       ~/venv/Scripts/activate.bat
       pip install sphinx
       deactivate.bat
  4. Start emacs
  5. Set `python-shell-virtualenv-root` to the root directory of your
  virtualenv (eg. "/home/jtimmons/venv")
  6. M-x run-python
  7. Make sure that you can import the library installed in the
  virtualenv in step 3 (eg. 'import sphinx')


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: A patch to fix Windows venv usage in python shells --]
[-- Type: text/x-patch, Size: 1666 bytes --]

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 3701812..86e99f4 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -2099,20 +2099,25 @@ remote host, the returned value is intended for
 (defun python-shell-calculate-exec-path ()
   "Calculate `exec-path'.
 Prepends `python-shell-exec-path' and adds the binary directory
-for virtualenv if `python-shell-virtualenv-root' is set.  If
-`default-directory' points to a remote host, the returned value
-appends `python-shell-remote-exec-path' instead of `exec-path'."
+for virtualenv if `python-shell-virtualenv-root' is set - this
+will use the python interpreter from inside the virtualenv when
+starting the shell.  If `default-directory' points to a remote host,
+the returned value appends `python-shell-remote-exec-path' instead
+of `exec-path'."
   (let ((new-path (copy-sequence
                    (if (file-remote-p default-directory)
                        python-shell-remote-exec-path
-                     exec-path))))
+                     exec-path)))
+
+        ;; Windows and POSIX systems use different venv directory structures
+        (virtualenv-bin-dir (if (eq system-type 'windows-nt) "Scripts" "bin")))
     (python-shell--add-to-path-with-priority
      new-path python-shell-exec-path)
     (if (not python-shell-virtualenv-root)
         new-path
       (python-shell--add-to-path-with-priority
        new-path
-       (list (expand-file-name "bin" python-shell-virtualenv-root)))
+       (list (expand-file-name virtualenv-bin-dir python-shell-virtualenv-root)))
       new-path)))
 
 (defun python-shell-tramp-refresh-remote-path (vec paths)

  reply	other threads:[~2016-12-12  0:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-18 22:52 bug#24464: 25.1; python.el virtualenv Pim
2016-12-12  0:39 ` Justin Timmons [this message]
2017-10-17  1:24   ` Noam Postavsky
2017-11-05 17:20   ` Noam Postavsky

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.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=87h96akmjn.fsf@gmail.com \
    --to=justinmtimmons@gmail.com \
    --cc=24464@debbugs.gnu.org \
    --cc=pim.remkes@upcmail.nl \
    /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.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).