syl20bnr / spacemacs

A community-driven Emacs distribution - The best editor is neither Emacs nor Vim, it's Emacs *and* Vim!
http://spacemacs.org
GNU General Public License v3.0
23.67k stars 4.89k forks source link

Autocompletion not working for python #14608

Closed dtransposed closed 1 year ago

dtransposed commented 3 years ago

Description

Autocompletion is not working for a code written in a script, however it works for Inferior Python.

Reproduction guide

Observed behaviour Completions do not work. company-diag returns the log:

Zrzut ekranu 2021-04-6 o 16 12 26

However, when performing same import and autocompletion in inferior mode (run-python), autocompletion works as expected. company-diag returns the log:

Zrzut ekranu 2021-04-6 o 16 12 00

System Info

OS: darwin20.1.0 Emacs: 27.1 Spacemacs: 0.300.0 Spacemacs branch:

commit df8249a0522c2bda83cef476d41d55de3cfbec0f (HEAD -> develop, origin/develop, origin/HEAD)
Author: JAremko <w3techplayground@gmail.com>
Date:   Mon Apr 5 06:15:21 2021 +0300

Distribution: spacemacs Editing style: vim Completion: helm Layers:

Zrzut ekranu 2021-04-6 o 16 16 41

I have tried solutions listed in following issues: https://github.com/syl20bnr/spacemacs/issues/11785 https://github.com/syl20bnr/spacemacs/issues/10638

Namely:

  1. Putting this at your dotfile user-config:
    (eval-after-load "company"
    '(add-to-list 'company-backends 'company-anaconda))
  2. Adding

dotspacemacs-additional-packages '(jedi)

Nothing works for me so far.

lebensterben commented 3 years ago

not related, but this should be the example for other bug reports. it's well described.

dtransposed commented 3 years ago

I have noticed super interesting thing. Every time I run: pip install --upgrade jedi json-rpc service_factory in the peen environment, the autocompletion starts suddenly working! Then I restart emacs and autocompletion breaks! I run the pip install... again and things are working once again. This is weird.

@lebensterben Thank you!

lebensterben commented 3 years ago

Do you have virtual environment?

dtransposed commented 3 years ago

Yes. I am using pyenv. For the purposes of this investigation I have created new environment testenv

  1. I open emacs.
  2. Before I open any file I activate this environment through pyenv-mode-set.
  3. When I edit the file I can see that the name of virtual environment is present on the power bar.
  4. pip install --upgrade jedi json-rpc service_factory is being run inside this virtual environment.

Edit: anaconda-show-doc also behaves funny, exactly like the autocompletion engine.

alexey0308 commented 3 years ago

@dtransposed you use anaconda backend, which requires inferior python buffer created for auto-completion. Hence, first C-c C-p, then everything should work.

EDIT: Unfortunately, I can not find it in the anaconda documentation and it looks like it should work right away, but at the times I was using it, I had such problem, until the inferior-python-buffer is started.

dtransposed commented 3 years ago

@alexey0308 Thank you! It works like charm! Could you explain why this is the case? Do other backends have different behaviour?

lebensterben commented 3 years ago

https://github.com/syl20bnr/spacemacs/blob/3371a8220c8e48503dc6dd4e2d6ac532ba9946ad/layers/%2Blang/python/packages.el#L97-L109

Line 102 probably need to be changed to :modes python-mode inferior-python-mode for anaconda backend.

alexey0308 commented 3 years ago

LSP starts its server automatically in the background, but you need to restart it if you activate the environment, so it catches up with the new configuration. I don't use anaconda anymore and I don't remember, where actually I read, that one needs to have a python process for anaconda to work. Maybe check the source code or docs or take it as a folklore knowledge :)

lebensterben commented 3 years ago

But it's actually handled here: https://github.com/syl20bnr/spacemacs/blob/3371a8220c8e48503dc6dd4e2d6ac532ba9946ad/layers/%2Blang/python/funcs.el#L64-L71

lebensterben commented 3 years ago

(If we drop support of all non-lsp backends, life would be much easier for all of us)

alexey0308 commented 3 years ago

https://github.com/syl20bnr/spacemacs/blob/3371a8220c8e48503dc6dd4e2d6ac532ba9946ad/layers/%2Blang/python/packages.el#L97-L109

Line 102 probably need to be changed to :modes python-mode inferior-python-mode for anaconda backend.

@lebensterben this is for inferior buffer only: it is always on anaconda backend, regardless of lsp or not for the python-mode. For python-mode, you already found anaconda configuration in your second message.

alexey0308 commented 3 years ago

(If we drop support of all non-lsp backends, life would be much easier for all of us)

Anaconda can work with tramp AFAIK, I don't know if lsp as well?

lebensterben commented 3 years ago

Anaconda can work with tramp AFAIK, I don't know if lsp as well?

A valid point. Setting up LSP over remote connection is doable, and I've actually seen someone mentioned this a few days ago. But it requires some setups.

See https://emacs-lsp.github.io/lsp-mode/page/remote/

But I really wish we can drop non-lsp support, for its many benefits:

alexey0308 commented 3 years ago

I understand, it's more work to support it, especially when there is no dedicated maintainer for every layer. But it's rather a political decision: you see people using anaconda for some reason, or e.g. I use ess because I like to have completion based on the inferior buffer state as well, and lsp infers only based on the code base, right? Trade-off between maintaining and users ¯_(ツ)_/¯

smile13241324 commented 3 years ago

One point against LSP is that it normally takes much more system load than traditional emacs modes. Still the ecosystem definitely goes into the direction of LSP. I dimly remember we already have layers supporting only LSP right now.

lebensterben commented 2 years ago

Please retry with one of the LSP backend.

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please let us know if this issue is still valid!