First of all, I apologize if this is not the right place for this question.
Let's say I have the following python code:
import vim
x = 1
print(vim.eval("py3eval('x')"))
Now I use this code in a vim function in the file myplugin/autoload/myplugin.vim:
function! myplugin#myfunction()
python3 << EOF
import vim
x = 1
print(vim.eval("py3eval('x')"))
EOF
endfunction
Calling this function from vim or lua (vim.fn["myplugin#mytest"]()) prints 1 as expected.
However, I get an error when I try the following:
create a file mymodule.py in the folder python3 in my plugin root directory with the following contents:
def myfunction():
import vim
x = 1
print(vim.eval("py3eval('x')"))
run vim.cmd('py3 import mymodule')
run vim.cmd('py3 mymodule.myfunction()')
I get the following error:
NameError: name 'x' is not defined
I expected both implementations to work exactly the same (it should not matter whether the code is defined in a python3 << block or in a separate python file). Why is x not defined in the second case?
x=1 here has a local scope, so there is no way for external expressions (py3eval) to look at the local variable. This is as designed and you should therefore use a global (or nonlocal) variable.
First of all, I apologize if this is not the right place for this question.
Let's say I have the following python code:
Now I use this code in a vim function in the file
myplugin/autoload/myplugin.vim
:Calling this function from vim or lua (
vim.fn["myplugin#mytest"]()
) prints1
as expected.However, I get an error when I try the following:
mymodule.py
in the folderpython3
in my plugin root directory with the following contents:vim.cmd('py3 import mymodule')
vim.cmd('py3 mymodule.myfunction()')
I get the following error:
I expected both implementations to work exactly the same (it should not matter whether the code is defined in a
python3 <<
block or in a separate python file). Why isx
not defined in the second case?Edit: changed
pyeval
topy3eval