jorgenschaefer / elpy

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

elpy-goto-definition not working for me (No definition found) #1878

Closed jcerjak closed 3 years ago

jcerjak commented 3 years ago

Summary

I was happily using elpy, but after a reinstall of my OS (from Ubuntu 18.04 LTS to 20.04 LTS), elpy-goto-definition does not work for me anymore. I'm using the same version of Emacs and same Emacs config as before. Also, elpy does automatically pick up the currently active virtualenv anymore (will open a separate ticket for that, if it's not related to this issue).

Steps to reproduce

Emacs.............: 26.3 Elpy..............: 1.35.0 Virtualenv........: None Interactive Python: python3 3.8.5 (/home/jcerjak/projects/myproject/venv/bin/python3) RPC virtualenv....: rpc-venv (/home/jcerjak/.emacs.d/elpy/rpc-venv) Python...........: python 3.8.5 (/home/jcerjak/.emacs.d/elpy/rpc-venv/bin/python) Jedi.............: 0.18.0 Rope.............: 0.18.0 Autopep8.........: 1.5.4 Yapf.............: 0.30.0 Black............: 20.8b1 Syntax checker....: flake8 (/home/jcerjak/projects/myproject/venv/bin/flake8)

Warnings

You have not activated a virtual env. It is not mandatory but often a good idea to work inside a virtual env. You can use M-x pyvenv-activate or M-x pyvenv-workon to activate one.

The directory ~/.local/bin/ is not in your PATH. As there is no active virtualenv, installing Python packages locally will place executables in that directory, so Emacs won't find them. If you are missing some commands, do add this directory to your PATH -- and then do `elpy-rpc-restart'.

* Manually set virtualenv with `pyvenv-activate`
* Check `M-x elpy-config` again, looks ok now:

Elpy Configuration

Emacs.............: 26.3 Elpy..............: 1.35.0 Virtualenv........: venv (/home/jcerjak/projects/myproject/venv/) Interactive Python: python3 3.8.5 (/home/jcerjak/projects/myproject/venv/bin/python3) RPC virtualenv....: rpc-venv (/home/jcerjak/.emacs.d/elpy/rpc-venv) Python...........: python 3.8.5 (/home/jcerjak/.emacs.d/elpy/rpc-venv/bin/python) Jedi.............: 0.18.0 Rope.............: 0.18.0 Autopep8.........: 1.5.4 Yapf.............: 0.30.0 Black............: 20.8b1 Syntax checker....: flake8 (/home/jcerjak/projects/myproject/venv/bin/flake8)

* Open `main.py` buffer, linters seem to be working, since the semicolon (`;`) is highlighed, as expected (error: `multiple statements on one line (semicolon) [E702])`). Auto-completion also works.
* But if I put my cursor on `requests.get` and try to [go to definition](https://elpy.readthedocs.io/en/latest/ide.html#command-elpy-goto-definition), I get a message `No definition found`. Same also if I try for code in the Python standard lib.

# My configuration
## OS

Ubuntu 20.04.1 LTS. I have commented out all custom code in my `~/.bashrc`.

## Result of `(elpy-config)`

Elpy Configuration

Emacs.............: 26.3 Elpy..............: 1.35.0 Virtualenv........: venv (/home/jcerjak/projects/myproject/venv/) Interactive Python: python3 3.8.5 (/home/jcerjak/projects/myproject/venv/bin/python3) RPC virtualenv....: rpc-venv (/home/jcerjak/.emacs.d/elpy/rpc-venv) Python...........: python 3.8.5 (/home/jcerjak/.emacs.d/elpy/rpc-venv/bin/python) Jedi.............: 0.18.0 Rope.............: 0.18.0 Autopep8.........: 1.5.4 Yapf.............: 0.30.0 Black............: 20.8b1 Syntax checker....: flake8 (/home/jcerjak/projects/myproject/venv/bin/flake8)


## Elpy configuration in my init.el

in ~/.emacs

(use-package elpy :ensure t :init (elpy-enable))

jcerjak commented 3 years ago

This issue looks like a duplicate of #1875. Downgrading jedi as suggested on that issue (in a virtualenv specified by RPC virtualenv) solved it for me.

Just a question regarding having to manually set active virtualenv with pyvenv-activate. It seems that auto-completion, goto definition etc. work also if Virtualenv........: None. Is it expected that the value is None when opening emacs from an active virtualenv? And do I even need to manually set it, if everything works, is there some functionality disabled if this is None?

simurgh9 commented 3 years ago

At the risk of not helping, what I did to fix this was (setq elpy-rpc-virtualenv-path 'current) this will set the RPC virtual environment to be the same as your global environment. Now you can install the needed packages in your global environment with pip3 install autopep8 yapf black rope and finally as suggested here revert to pip install jedi==0.17.2. Now note that elpy-rpc-virtualenv-path being set to current means the RPC's virtual environment should be automatically set to your current one even if not global.

Now to set the virtual environment of your project (which is different than that of RPC's) I think you are to use pyvenv-activate.

jcerjak commented 3 years ago

@simurgh9 thanks for the hint, will try it out. Ideally I'd like to avoid installing the linters globally, so my preferred setup would be:

But either manually activating the virtualenv, or using your suggestion is acceptable for now. Closing the issue, if anyone knows how to get the above solution working (with separate envs), please add a comment.

rileyrg commented 3 years ago

At the risk of not helping, what I did to fix this was (setq elpy-rpc-virtualenv-path 'current) this will set the RPC virtual environment to be the same as your global environment. Now you can install the needed packages in your global environment with pip3 install autopep8 yapf black rope and finally as suggested here revert to pip install jedi==0.17.2. Now note that elpy-rpc-virtualenv-path being set to current means the RPC's virtual environment should be automatically set to your current one even if not global.

Now to set the virtual environment of your project (which is different than that of RPC's) I think you are to use pyvenv-activate.

current sets to the currently active environment which should be the activated venv not the global system value isn't it? (I'm here because elpy goto definition isn't working for me either ;)).

dmitrym0 commented 2 years ago

Hey folks,

I'm having the same issue.. the weird thing is was working yesterday:

Emacs.............: 28.2
Elpy..............: 1.35.0
Virtualenv........: rpc-venv (/Users/dmitrymarkushevich/.emacs.d/elpy/rpc-venv/)
Interactive Python: python3 3.10.8 (/Users/dmitrymarkushevich/.emacs.d/elpy/rpc-venv/bin/python3)
RPC virtualenv....: rpc-venv (/Users/dmitrymarkushevich/.emacs.d/elpy/rpc-venv)
 Python...........: python 3.10.8 (/Users/dmitrymarkushevich/.emacs.d/elpy/rpc-venv/bin/python)
 Jedi.............: 0.17.2 (0.18.1 available)
 Autopep8.........: 1.7.0 (2.0.0 available)
 Yapf.............: 0.32.0
 Black............: 22.10.0
Syntax checker....: flake8 (/Users/dmitrymarkushevich/.emacs.d/elpy/rpc-venv/bin/flake8)

And looking for definitions:

Debugger entered--Lisp error: (error "No definition found")
  signal(error ("No definition found"))
  error("No definition found")
  elpy-xref--definitions("47: execute_query")
  #f(compiled-function (backend id) #<bytecode 0x18055c8e2cd29a89>)(elpy "47: execute_query")
  apply(#f(compiled-function (backend id) #<bytecode 0x18055c8e2cd29a89>) elpy "47: execute_query")
  xref-backend-definitions(elpy "47: execute_query")
  #f(compiled-function () #<bytecode -0x178fb55bdf4a8446>)()
  consult-xref--candidates()
  consult-xref(#f(compiled-function () #<bytecode -0x178fb55bdf4a8446>) ((window . #<window 3 on pack_builder.py>) (display-action) (auto-jump)))
  xref--show-defs(#f(compiled-function () #<bytecode -0x178fb55bdf4a8446>) nil)
  xref--find-definitions("47: execute_query" nil)
  xref-find-definitions("47: execute_query")
  evil-goto-definition-xref("execute_query" 1194)
  run-hook-with-args-until-success(evil-goto-definition-xref "execute_query" 1194)
  evil-goto-definition()
  funcall-interactively(evil-goto-definition)
  call-interactively(evil-goto-definition nil nil)
  command-execute(evil-goto-definition)

Any other tips?

sjlawson commented 1 year ago

I had a very similar issue. I'm running Emacs 29.0.50 (9.0) which I compiled from source on MacOS. When I installed Elpy through melpa and checked elpy-config it didn't find any dependencies even though they were all installed -- it didn't even see "elpy" from elpy-config !!!

What I did to make it work: I removed elpy it from the elpa directory, removed the existing eply rpc virtualenv directory, and just downloaded the elpy code from the repo to my .emacs.d folder, and did the manual installation in init.el , elpy ran the auto deps install. Now everything works beautifully.

Lesson learned: If you compile your editor and one of your packages seems broken, you might have to install that from source as well.