jorgenschaefer / elpy

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

Problems with elpy and virtual environment #1727

Open yorodm opened 4 years ago

yorodm commented 4 years ago

Summary

elpy-config does not recognize the current virtual environment

Steps to reproduce

  1. Open a py file.
  2. Do M-x pyvenv-workon and select a virtualenv
  3. Do M-x elpy-config

My configuration

OS

I'm running Emacs 26.3 on Windows 10.

Result of (elpy-config)

Elpy Configuration

Emacs.............: 26.3
Elpy..............: 1.32.0
Virtualenv........:  (c:/)
Interactive Python: python (not found)
RPC virtualenv....: rpc-venv (c:/Users/hp/.emacs.d/elpy/rpc-venv)
 Python...........: c:/WINDOWS/py.exe 3.7.2 (c:/WINDOWS/py.exe)
 Jedi.............: 0.15.1
 Rope.............: 0.14.0
 Autopep8.........: 1.4.4
 Yapf.............: 0.28.0
 Black............: 19.10b0
Syntax checker....: Not found (flake8)

Elpy configuration in my init.el

(use-package elpy
    :pin melpa
    :config
    (elpy-enable)
    (define-key python-mode-map [remap xref-find-definitions] #'elpy-goto-definition))
yorodm commented 4 years ago

Sorry, I forgot to add that I have elpy-rpc-virtualenv-path set to 'current in my .custom.el

azzamsa commented 4 years ago

Great.

You can close if this not relevant anyomore :)

yorodm commented 4 years ago

Care to explain why? I believe that having elpy-rpc-virtualenv-path set to 'current should make elpy-config list my virtualenv as something other than c:/ also it should detect a proper interpreter (currently is detecting the python launcher)

galaunay commented 4 years ago

I think @azzamsa assumed you found a solution to your problem because your post began with Sorry, I forgot ... :) (I did the same...).

elpy-rpc-virtualenv-path is used to specify which virtualenv to use for the RPC process (which is the process running in the background and providing code completion, static analysis and other functionnalities). From what I saw in your elpy-config result, the RPC seems to work fine, so elpy-rpc-virtualenv-path should not be relevant in this case.

The problem you have seems to be more linked to virtualenv and its activation (as you said in your first post). Can I ask what is the path of the virtualenv you are trying to activate ? If it is c:/WINDOWS/py.exe, then everything seems fine (Elpy assumes the virtualenv name is the parent directory of the parent directory of the executable, so c:\ in your case).

So besides that, Is Elpy working as expected ?

yorodm commented 4 years ago

Sorry again, I'm not a native English speaker and sometimes my thoughts get a little lost in translation.

My virtualenvs are located in other drive and I have WORKON_HOME set so pyvenv works like a charm. On the side of elpy completion works fine too (BTW, thanks for your great work guys). The thing is even after calling pyvenv-activate elpy still shows c:/windows as the active virtualenv. I've tried setting elpy-rpc-virtualenv-path to 'default and still get the same results.

galaunay commented 4 years ago

It may just be a display bug in elpy-config. Is your virtualenv properly activated when you run a python script ?

Elpy looks at the environment variable VIRTUAL_ENV to detect an active virtualenv. Could you check the result of M-x getenv VIRTUAL_ENV ? Can I ask you as well what is the path of the virtualenv you are trying to activate ?

yorodm commented 4 years ago

I don't know if this helps but calling elpy-config is triggering pyvenv-post-activate-hook even if I've previously activated a virtualenv with pyvenv-activate. Maybe this is expected behavior but this removes my active virtualenv every time.

galaunay commented 4 years ago

elpy-config is indeed supposed to deactivate your virtualenv temporarily (deactivate it, run some code, and re-activate it). Maybe there is something wrong dring the re-activation. Is your virtualenv deactivated when running this: (with-elpy-rpc-virtualenv-activated (message "rpc-python: %s" (executable-find elpy-rpc-python-command))) ?

yorodm commented 4 years ago

Yes it does, it also detects c:/windows/py.exe as the elpy-rpc-python-command even if I set elpy-rpc-virtualenv-path to 'default.

galaunay commented 4 years ago

Could you run the following snippet and post the result (deleting the sensitive data if necessary) so I can have an overview of you configuration:

(progn
  (message "python env: %s" pyvenv-virtual-env)
  (message "python env (env var): %s" (getenv "VIRTUAL_ENV"))
  (message "python exec: %s" python-shell-interpreter)
  (message "python exec (full): %s" (executable-find python-shell-interpreter))
  (message "python rpc exec: %s" elpy-rpc-python-command)
  (message "python rpc exec (full): %s"
           (executable-find elpy-rpc-python-command))
  (message "rpc-virtualenv-path: %s" elpy-rpc-virtualenv-path)
  (message "python rpc venv location : %s" (elpy-rpc-get-or-create-virtualenv))
  (with-elpy-rpc-virtualenv-activated
   (message "python venv exec: %s" (executable-find elpy-rpc-python-command))))
yorodm commented 4 years ago

Sorry for the delay. Evaluating the snippet (with a virtualenv activated via pyvenv) gets me this.

python env: c:/Users/hp/Documents/projects/python/venvs/clean/
python env (env var): c:/Users/hp/Documents/projects/python/venvs/clean
python exec: python
python exec (full): c:/Users/hp/Documents/projects/python/venvs/clean/Scripts/python.exe
python rpc exec: c:/WINDOWS/py.exe
python rpc exec (full): c:/WINDOWS/py.exe
rpc-virtualenv-path: default
python rpc venv location : c:/Users/hp/.emacs.d/elpy/rpc-venv
Python path updated. Restart eglot if needed
python venv exec: c:/WINDOWS/py.exe