From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Justin Timmons Newsgroups: gmane.emacs.bugs Subject: bug#24464: 25.1; python.el virtualenv Date: Sun, 11 Dec 2016 19:39:56 -0500 Message-ID: <87h96akmjn.fsf@gmail.com> References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1481503901 23094 195.159.176.226 (12 Dec 2016 00:51:41 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 12 Dec 2016 00:51:41 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) Cc: 24464@debbugs.gnu.org To: Pim Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Mon Dec 12 01:51:37 2016 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cGEq6-0004KS-2a for geb-bug-gnu-emacs@m.gmane.org; Mon, 12 Dec 2016 01:51:34 +0100 Original-Received: from localhost ([::1]:57754 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cGEg5-0006Mb-3D for geb-bug-gnu-emacs@m.gmane.org; Sun, 11 Dec 2016 19:41:13 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:37105) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cGEfz-0006MS-3w for bug-gnu-emacs@gnu.org; Sun, 11 Dec 2016 19:41:08 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cGEfu-0007uA-7i for bug-gnu-emacs@gnu.org; Sun, 11 Dec 2016 19:41:07 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:51395) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cGEfu-0007u6-2w for bug-gnu-emacs@gnu.org; Sun, 11 Dec 2016 19:41:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1cGEft-0005op-Sw for bug-gnu-emacs@gnu.org; Sun, 11 Dec 2016 19:41:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Justin Timmons Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 12 Dec 2016 00:41:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 24464 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 24464-submit@debbugs.gnu.org id=B24464.148150321022301 (code B ref 24464); Mon, 12 Dec 2016 00:41:01 +0000 Original-Received: (at 24464) by debbugs.gnu.org; 12 Dec 2016 00:40:10 +0000 Original-Received: from localhost ([127.0.0.1]:38561 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cGEf4-0005nd-1f for submit@debbugs.gnu.org; Sun, 11 Dec 2016 19:40:10 -0500 Original-Received: from mail-qk0-f180.google.com ([209.85.220.180]:36698) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cGEf2-0005nO-CP for 24464@debbugs.gnu.org; Sun, 11 Dec 2016 19:40:08 -0500 Original-Received: by mail-qk0-f180.google.com with SMTP id n21so69057951qka.3 for <24464@debbugs.gnu.org>; Sun, 11 Dec 2016 16:40:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=FABFY1qLtAkeaXoaArutyPj89tLAk2m4YpXEIQQPzvQ=; b=dumPJsZMuQ+x1N1nKPh2XpU4WeJpPQfG6BMdJZe8Nul2qY8d+nt5ltsdY8sQCoSOZ1 Un83VU137hJEjgBKwiP+zqBWD8uPiE99xkpgKi13WHDf/UhXUdsYvYalddCFU+w5nQKE sTCzE3WlDGELTiCuVamD9aU0aDLbOthVialpGtWLw7aKYBybbv770ME7jc8eK5fsK1iz CBYVWG2yUAgKE2lr7xiPBbq0NsLSf2x39OTEyfiG3nLHeqMZQTLzKAIy3ip7VXYQ4Ng7 FvdJO2I3wuubbUvhDfIK96iroZy1uCVt+ouL7jjlvZEyhnrJN2sgdCDs+0YEAOMV5Wcq HnTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=FABFY1qLtAkeaXoaArutyPj89tLAk2m4YpXEIQQPzvQ=; b=JqNm9xkPSDmdCPSpDjPKO7Izj7SsunzNGVplh7rTWvub4nTk2ngpDRUwy1ouG70+6b XXtVCMpDKv5L1cqjtu0kj/pN7vYczZkEwlviWsMydjlQ6AoU8Yo1cTU0N8E1xyc5l+r0 I3m+RMv+asGyuTPFB1EU+B/UxgknrR9J/g4bcVsBaxWUf+1gzGNPOk05uRvIzs63bYHv dgXxuFMb7YzWcQUJ+FlQvrjduTBFFkG+e0LhHltYfZ8q77yrS+iG9KEw1RqvpvJHY8bj 2MXQnmSLqErLUBaiGKiQdLNol+6w3ACZSA/HzL52I9/cK9SSNxP26XLg3c+7KuHL4y1K JDnA== X-Gm-Message-State: AKaTC01NDYYnV/rR3qX9f1il0Mjj4xeVvRuPoD1uwfbKd7jcXIRJpoTpF/c76PTZ6StYOA== X-Received: by 10.55.70.76 with SMTP id t73mr76810557qka.195.1481503202885; Sun, 11 Dec 2016 16:40:02 -0800 (PST) Original-Received: from restitution (c-73-186-209-171.hsd1.ma.comcast.net. [73.186.209.171]) by smtp.gmail.com with ESMTPSA id e24sm25317558qkj.12.2016.12.11.16.40.01 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 11 Dec 2016 16:40:01 -0800 (PST) In-Reply-To: (Pim's message of "Mon, 19 Sep 2016 00:52:36 +0200") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:126894 Archived-At: --=-=-= Content-Type: text/plain 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') --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=venv.diff Content-Description: A patch to fix Windows venv usage in python shells 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) --=-=-=--