Open dstrozzi opened 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:
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
)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.
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))
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?
Sorry to nag, but does anyone have any bright ideas on this issue? Anythin I can try? Thanks.
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))
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.
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)
Elpy configuration in my init.el
Thanks!