Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can not execute a custom function which has python script inside it: no python provider found #9927

Closed
jdhao opened this issue Apr 19, 2019 · 6 comments
Labels
closed:question issues that are closed as usage questions

Comments

@jdhao
Copy link

jdhao commented Apr 19, 2019

  • nvim --version: 0.4.0
  • Vim (version: 7.4): behaves differently
  • Operating system/version: CentOS 7.4
  • Terminal name/version: mintty terminal, 3.0.0
  • $TERM: xterm-256color

Steps to reproduce

Using the following minimal init.vim:

function! RandomInt(maxLen)
python << EOF
import vim
import random

idx = random.randint(0, int(vim.eval('a:maxLen')))
vim.command("let index = %s"% idx)
EOF
echo index
endfunction
  1. open neovim with the above setting:
nvim -u init.vim
  1. using the following command in command mode:
call RandomInt(10)

Actual behaviour

I got an error. The error message is:

Error detected while processing function RandomInt:
line 7:
E319: No "python" provider found. Run ":checkhealth provider"
line 8:
E121: Undefined variable: index

For reference, the output of :checkhealth provider:

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

## Python 2 provider (optional)
  - INFO: Disabled (g:loaded_python_provider=1).  This might be due to some previous error.
  - WARNING: No Python executable found that can `import neovim`. Using the first available executable for diagnostics.
  - ERROR: Python provider error:
    - ADVICE:
      - provider/pythonx: Could not load Python 2:
          /bin/python2 does not have the "neovim" module. :help provider-python
          /bin/python2.7 does not have the "neovim" module. :help provider-python
          python2.6 not found in search path or not executable.
          /home/haojiedong/tools/anaconda3/bin/python is Python 3.6 and cannot provide Python 2.
  - INFO: Executable: Not found

## Python 3 provider (optional)
  - INFO: `g:python3_host_prog` is not set.  Searching for python3 in the environment.
  - INFO: Executable: /home/haojiedong/tools/anaconda3/bin/python3
  - INFO: Python version: 3.6.5
  - INFO: pynvim version: 0.3.2
  - 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)
  - WARNING: `node` and `npm` (or `yarn`) must be in $PATH.
    - ADVICE:
      - Install Node.js and verify that `node` and `npm` (or `yarn`) commands work.

It shows that Python3 is found.

But if I use the above minimal setting with vim, I can call the function and get a random number.

Expected behaviour

I can execute the function without error.

@mhinz
Copy link
Member

mhinz commented Apr 19, 2019

Well, :checkhealth provider says:

  - INFO: Disabled (g:loaded_python_provider=1).  This might be due to some previous error.

So, have you set that option or was it Nvim? In any case, Nvim considers Python 2 support to be disabled, so the behaviour that I would expect, is that it wouldn't even try to execute your function in the first place.

@jdhao
Copy link
Author

jdhao commented Apr 19, 2019

No, in the init.vim, there is only the function definition. Do you mean I have to add the following setting:

let g:loaded_python_provider=0

and install python2 in my system? Possibly also set the g:python2_host_prog?

@mhinz
Copy link
Member

mhinz commented Apr 19, 2019

I think you're confused by the fact that :python means Python 2. That is disabled (apparently it's not installed) on your system.

For Python 3, use :python3 instead.

Also consider changing the function to function! RandomInt(maxLen) abort, so it stops at the very first error, so you won't even see the E121: Undefined variable: index when :python already fails.

@jdhao
Copy link
Author

jdhao commented Apr 19, 2019

Changing from python << EOF to python3 << EOF works.

But it seems that I can not make python2 work. I am using Anaconda3. I create a python2 virtual environment and install pynvim inside that environment.

Then I use the following settings:

function! RandomInt(maxLen)
python << EOF
import vim
import random

idx = random.randint(0, int(vim.eval('a:maxLen')))
vim.command("let index = %s"% idx)
EOF
echo index
endfunction

let g:loaded_python_provider=0
let g:python_host_prog=expand('~/tools/anaconda3/envs/python2/bin/python')

After that, I start nvim with nvim -u init.vim in the virtual environment. The output of :checkhealth provider is:

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

## Python 2 provider (optional)
  - INFO: Disabled (g:loaded_python_provider=0).  This might be due to some previous error.
  - INFO: Using: g:python_host_prog = "/home/haojiedong/tools/anaconda3/envs/python2/bin/python"
  - INFO: Executable: /home/haojiedong/tools/anaconda3/envs/python2/bin/python
  - INFO: Python version: 2.7.16
  - INFO: pynvim version: 0.3.2
  - OK: Latest pynvim is installed.

## Python 3 provider (optional)
  - INFO: `g:python3_host_prog` is not set.  Searching for python3 in the environment.
  - INFO: Executable: /home/haojiedong/tools/anaconda3/bin/python3
  - INFO: Python version: 3.6.5
  - INFO: pynvim version: 0.3.2
  - 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)
  - WARNING: `node` and `npm` (or `yarn`) must be in $PATH.
    - ADVICE:
      - Install Node.js and verify that `node` and `npm` (or `yarn`) commands work.

It seems that python2 is still not loaded for some unknown reasons. So I can not call the function inside virtual env (still errors).

@mhinz
Copy link
Member

mhinz commented Apr 19, 2019

You should only set g:loaded_python_provider when you want to disable the Python provider on purpose. Now Nvim thinks that it tried to load the Python provider, but failed doing so.

@jdhao
Copy link
Author

jdhao commented Apr 20, 2019

Thanks for your kind explanations. Removing the line about loaded_python_provider works.

@jdhao jdhao closed this as completed Apr 20, 2019
@justinmk justinmk added the usage label Apr 20, 2019
@justinmk justinmk added the closed:question issues that are closed as usage questions label Aug 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed:question issues that are closed as usage questions
Projects
None yet
Development

No branches or pull requests

3 participants