jorgenschaefer / elpy

Emacs Python Development Environment
GNU General Public License v3.0
1.9k stars 261 forks source link

Activating venv on Windows 10 #1628

Open dstrozzi opened 5 years ago

dstrozzi commented 5 years ago

Summary

I can't get a venv to activate on Windows 10.

Steps to reproduce

M-x run-python Anaconda python 3.7.3 starts, but gives this warning: Warning: This Python interpreter is in a conda environment, but the environment has not been activated. Libraries may fail to load. To activate this environment please see https://conda.io/activation

import numpy # gives error, DLL load failed, typical on Windows when venv not setup

My configuration

OS

GNU emacs 26.2 (straight from GNU) on Windows 10. Oddly, I didn't have this issue on Win7 last year. I did have others, like getting plots in ipython to work, but one step at a time!

Result of (elpy-config)

Virtualenv........: anaconda3 (c:/Users/strozzi2/AppData/Local/Continuum/anaconda3)
RPC Python........: 3.7.3 (c:/Users/strozzi2/AppData/Local/Continuum/anaconda3/pythonw.exe)
Interactive Python: python (c:/Users/strozzi2/AppData/Local/Continuum/anaconda3/python.exe)
Emacs.............: 26.2
Elpy..............: 1.29.1
Jedi..............: 0.13.3
Rope..............: 0.12.0
Autopep8..........: Not found
Yapf..............: Not found
Black.............: Not found
Syntax checker....: Not found (flake8)

Elpy configuration in my init.el

I think the only relevant parts from my .emacs:
(pyvenv-mode 1)
(pyvenv-activate "c:/Users/strozzi2/AppData/Local/Continuum/anaconda3")
(elpy-enable)

Thanks!

galaunay commented 5 years ago

From what happen, It looks like a problem in pyvenv. Could you check if it still happen without Elpy, just to narrow down the potential causes ?

In any case, pyvenv works by setting a bunch of variables in Emacs. You can check the following ones, maybe one of them is not set properly:

dstrozzi commented 5 years ago

Thanks for the response. I turned off elpy, and every variable agrees with what you said, except exec-path has c:/.../anaconda3/Scripts but not bin/. anaconda3/ has no bin dir, but there is a condabin/. I added that to exec-path, same problem. Note that on Windows I'm sure one has to run various .bat files instead of the shell scripts, e.g. activate.bat not activate.

galaunay commented 5 years ago

You are right, bin is apparently replaced by scripts on windows. So everything seems fine.

It may be related to this issue. Would the following version of pyvenv-activate fixe the problem ?

(defun pyvenv-activate (directory)
  "Activate the virtual environment in DIRECTORY."
  (interactive "DActivate venv: ")
  (setq directory (expand-file-name directory))
  (pyvenv-deactivate)
  (setq pyvenv-virtual-env (file-name-as-directory directory)
        pyvenv-virtual-env-name (file-name-nondirectory
                                 (directory-file-name directory))
        python-shell-virtualenv-path directory
        python-shell-virtualenv-root directory)
  ;; Set venv name as parent directory for generic directories
  (when (member pyvenv-virtual-env-name '("venv" ".venv"))
    (setq pyvenv-virtual-env-name
          (file-name-nondirectory
           (directory-file-name
            (file-name-directory
             (directory-file-name directory))))))
  ;; Preserve variables from being overwritten.
  (let ((old-exec-path exec-path)
        (old-eshell-path eshell-path-env)
        (old-process-environment process-environment))
    (unwind-protect
        (pyvenv-run-virtualenvwrapper-hook "pre_activate" pyvenv-virtual-env)
      (setq exec-path old-exec-path
            eshell-path-env old-eshell-path
            process-environment old-process-environment)))
  (run-hooks 'pyvenv-pre-activate-hooks)
  (let ((new-directories (append
                          ;; Unix
                          (when (file-exists-p (format "%s/bin" directory))
                            (list (format "%s/bin" directory)))
                          ;; Windows
                          (when (file-exists-p (format "%s/Scripts" directory))
                            (list (format "%s/Scripts" directory)
                                  ;; Package dlls to be loaded at runtime
                                  (when (file-exists-p (format
                                                        "%s/Library/bin"
                                                        directory))
                                    (format "%s/Library/bin" directory))
                                  ;; Apparently, some virtualenv
                                  ;; versions on windows put the
                                  ;; python.exe in the virtualenv root
                                  ;; for some reason?
                                  directory)))))
    (setq pyvenv-old-exec-path exec-path
          pyvenv-old-eshell-path eshell-path-env
          pyvenv-old-process-environment process-environment
          ;; For some reason, Emacs adds some directories to `exec-path'
          ;; but not to `process-environment'?
          exec-path (append new-directories exec-path)
          ;; set eshell path to same as exec-path
          eshell-path-env (mapconcat 'identity exec-path ":")
          process-environment (append
                               (list
                                (format "VIRTUAL_ENV=%s" directory)
                                (format "PATH=%s"
                                        (mapconcat 'identity
                                                   (append new-directories
                                                           (split-string (getenv "PATH")
                                                                         path-separator))
                                                   path-separator))
                                ;; No "=" means to unset
                                "PYTHONHOME")
                               process-environment)
          ))
  (pyvenv-run-virtualenvwrapper-hook "post_activate")
  (run-hooks 'pyvenv-post-activate-hooks))
dstrozzi commented 5 years ago

Thanks for this! It has the same problem as the original, at least the way I'm using it. Namely, the relevant lines in my .emacs.d are: [I copied all the code text from your prior post] (pyvenv-mode 1) (pyvenv-activate "c:/Users/strozzi2/AppData/Local/Continuum/anaconda3") (setq exec-path (cons "c:/Users/strozzi2/AppData/Local/Continuum/anaconda3/condabin" exec-path))

I restart emacs, M-x run-python, same error as before ("This Python interpreter is in a conda environment, but the environment has not been activated." Then import numpy fails).

Maybe I need to do something else?

dstrozzi commented 5 years ago

Sorry to nag, but does anyone have any bright ideas on this issue? Anythin I can try? Thanks.

galaunay commented 5 years ago

I was on holidays for a while, I am trying to catch up.

The issue in pyvenv seems to indicate that c:/Users/strozzi2/AppData/Local/Continuum/anaconda/Library/bin and c:/Users/strozzi2/AppData/Local/Continuum/anaconda/Scripts need to be in the PATH.

So would this help ?

(setq exec-path (cons "c:/Users/strozzi2/AppData/Local/Continuum/anaconda3/Library/bin" exec-path))
(setq exec-path (cons "c:/Users/strozzi2/AppData/Local/Continuum/anaconda3/Scripts" exec-path))
dstrozzi commented 5 years ago

Thanks for replying! Same issue with your suggestion ("This Python interpreter is in a conda environment, but the environment has not been activated....."). FWIW attached is my init.el, which I cut down to a bare minimum.

init.el.txt

galaunay commented 5 years ago

Sorry, I am a bit out of ideas. Your init file works fine for me, but I don't have a computer with windows 10 and Emacs installed.

As this is not really a problem with Elpy but more with Pyvenv, you may have more luck reporting there.