Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activating venv on Windows 10 #1628

Open
dstrozzi opened this issue Jul 27, 2019 · 8 comments
Open

Activating venv on Windows 10 #1628

dstrozzi opened this issue Jul 27, 2019 · 8 comments

Comments

@dstrozzi
Copy link

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
Copy link
Collaborator

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:

  • pyvenv-virtual-env
    (should be c:/Users/strozzi2/AppData/Local/Continuum/anaconda3 for you)
  • pyvenv-virtual-env-name
    (should be anaconda3)
  • python-shell-virtualenv-path
    (should be c:/Users/strozzi2/AppData/Local/Continuum/anaconda3/)
  • python-shell-virtualenv-root
    (should be c:/Users/strozzi2/AppData/Local/Continuum/anaconda3/)
  • exec-path
    (the first element should be c:/Users/strozzi2/AppData/Local/Continuum/anaconda3/bin)
  • process-environment
    (should contain VIRTUAl_ENV=c:/Users/strozzi2/AppData/Local/Continuum/anaconda3/ and PYTHONHOME)

@dstrozzi
Copy link
Author

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Author

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

@galaunay
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

galaunay commented Sep 5, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants