neoclide / coc-python

Python extension for coc.nvim, fork of vscode-python
574 stars 51 forks source link

Project's virtual environment not being used #174

Closed TravisDart closed 4 years ago

TravisDart commented 4 years ago

I have a virtual environment for neovim which holds jedi, yapf, flake8, etc. I don't want to install the Python packages for neovim in the global environment or my project's virtual environment, and this creating a virtual environment for neovim is the only way I know to do this.

My neovim config is below. The coc.vim.conf file is the Example vim configuration on the coc.nvim readme.

let g:python_host_prog = $HOME . '/.pyenv/versions/neovim2/bin/python'
let g:python3_host_prog = $HOME . '/.pyenv/versions/neovim3/bin/python'
let python3binpath = $HOME . '/.pyenv/versions/neovim3/bin/'

call plug#begin('~/.config/nvim/plugged')
    Plug 'neoclide/coc.nvim', {'branch': 'release' }
    Plug 'numirias/semshi', {'do': ':UpdateRemotePlugins'}
call plug#end()

source ~/.config/nvim/coc.vim.conf

let g:coc_global_extensions = 'coc-python'

My coc-settings.json:

{
    "python.pythonPath": "~/.pyenv/versions/neovim3/bin/python",
    "python.formatting.provider": "yapf",
    "python.formatting.yapfPath": "~/.pyenv/versions/neovim3/bin/yapf",
    "python.jediEnabled": true,
    "python.jediPath": "~/.pyenv/versions/neovim3/lib/python3.8/site-packages/",
    "python.linting.pylintEnabled": false,
    "python.linting.flake8Enabled": true,
    "python.linting.flake8Path": "~/.pyenv/versions/neovim3/bin/flake8",
    "python.sortImports.path":  "~/.pyenv/versions/neovim3/bin/isort",
    "python.linting.lintOnSave": false,
    "python.autoUpdateLanguageServer": false,
    "python.disableInstallationCheck": true
}

:checkhealth output:

health#coc#check
========================================================================
  - OK: Environment check passed

  - OK: Javascript bundle found
  - OK: Service started

health#nvim#check
========================================================================
## Configuration
  - OK: no issues found

## Performance
  - OK: Build type: Release

## Remote Plugins
  - OK: Up to date

## terminal
  - INFO: key_backspace (kbs) terminfo entry: key_backspace=\177
  - INFO: key_dc (kdch1) terminfo entry: key_dc=\E[3~

health#provider#check
========================================================================
## Clipboard (optional)
  - WARNING: No clipboard tool found. Clipboard registers (`"+` and `"*`) will not work.
    - ADVICE:
      - :help |clipboard|

## Python 2 provider (optional)
  - INFO: pyenv: Path: /usr/share/pyenv/libexec/pyenv
  - INFO: pyenv: $PYENV_ROOT is not set. Infer from `pyenv root`.
  - INFO: pyenv: Root: /home/arch/.pyenv
  - INFO: Using: g:python_host_prog = "/home/arch/.pyenv/versions/neovim2/bin/python"
  - INFO: Executable: /home/arch/.pyenv/versions/neovim2/bin/python
  - INFO: Python version: 2.7.17
  - INFO: pynvim version: 0.4.1
  - OK: Latest pynvim is installed.

## Python 3 provider (optional)
  - INFO: pyenv: Path: /usr/share/pyenv/libexec/pyenv
  - INFO: pyenv: $PYENV_ROOT is not set. Infer from `pyenv root`.
  - INFO: pyenv: Root: /home/arch/.pyenv
  - INFO: Using: g:python3_host_prog = "/home/arch/.pyenv/versions/neovim3/bin/python"
  - INFO: Executable: /home/arch/.pyenv/versions/neovim3/bin/python
  - INFO: Python version: 3.8.2
  - INFO: pynvim version: 0.4.1
  - OK: Latest pynvim is installed.

## Ruby provider (optional)
  - WARNING: `ruby` and `gem` must be in $PATH.
    - ADVICE:
      - Install Ruby and verify that `ruby` and `gem` commands work.

## Node.js provider (optional)
  - INFO: Node.js: v13.12.0
  - INFO: Neovim node.js host: /home/arch/.config/yarn/global//node_modules/neovim/bin/cli.js
  - OK: Latest "neovim" npm/yarn package is installed: 4.8.0

Now, when I create a test project, the wrong virtual environment is identified, and the Django packages don't autocomplete.

pyenv virtualenv 3.8.2 djangotest
pyenv activate djangotest
pip install Django
django-admin startproject djangotest
nvim djangotest/djangotest/urls.py

The satus line says: Python 3.8.2 64-bit ('neovim3': venv)

Shouldn't this identify the djangotest virtual environment?

Also, when I type from django.shortcuts no part of "shortcuts" gets an autocomplete suggestion. However, when I type from yapf., the yapflib submodule is suggested. I think this means that the current virtual environment is not being used for the autocomplete.

What am I doing wrong? How do I get the current virtual environment to be used for autocomplete, while keeping the neovim modules in their own (separate) virtual environment?

TravisDart commented 4 years ago

I tried many different configuration combinations, and found one that works. The solution is to use "python.pythonPath": "~/.pyenv/shims/python"

This is the full coc-settings.json:

{
    "python.pythonPath": "~/.pyenv/shims/python",
    "python.formatting.provider": "yapf",
    "python.formatting.yapfPath": "~/.pyenv/versions/neovim3/bin/yapf",
    "python.jediEnabled": true,
    "python.jediPath": "~/.pyenv/versions/neovim3/lib/python3.8/site-packages/",
    "python.linting.pylintEnabled": false,
    "python.linting.flake8Enabled": true,
    "python.linting.flake8Path": "~/.pyenv/versions/neovim3/bin/flake8",
    "python.sortImports.path":  "~/.pyenv/versions/neovim3/bin/isort",
    "python.linting.lintOnSave": false,
    "python.autoUpdateLanguageServer": false,
    "python.disableInstallationCheck": true
}