vim-beancount icon indicating copy to clipboard operation
vim-beancount copied to clipboard

Autocomplete does not work if beancount is installed via venv

Open kklipsch opened this issue 2 years ago • 11 comments

The import of the beancount module uses the system python not the virtual one so fails.

kklipsch avatar Mar 17 '23 20:03 kklipsch

So it looks like to support venv, you'll need to also add https://github.com/jmcantrell/vim-virtualenv to you vim plugin list. I can add a note in the instructions.

xentac avatar Apr 03 '23 23:04 xentac

Is there any update on this? It would be good if this plugin allows specifying the venv for beancount, especially given that since Ubuntu 23.04, user-level pip no longer works, and it is required to use either venv or apt to install Python packages.

upsuper avatar Apr 21 '23 12:04 upsuper

For those who have the same problem, I temporarily solved this by adding

import sys
sys.path.append('<venv path>/lib/python3.11/site-packages')

to the beginning of the Python code in beancount#load_everything inside autoload/beancount.vim.

upsuper avatar Apr 22 '23 04:04 upsuper

Did you try also installing the https://github.com/jmcantrell/vim-virtualenv nvim plugin? When I did that, I was able to use an activated venv with my system nvim.

xentac avatar Apr 23 '23 18:04 xentac

With the path manually added, no other extension is needed, and no venv needs to be activated.

upsuper avatar Apr 23 '23 21:04 upsuper

At that point, wouldn't it work to just add the same lines to your init.vim?

xentac avatar Apr 24 '23 15:04 xentac

I haven't heard about it... It seems to be a NeoVim thing but I'm using GVim, so I'm not sure about it. And I wouldn't want to switch the whole vim to one specific venv just because one plugin need it.

upsuper avatar Apr 24 '23 21:04 upsuper

vimrc and init.vim are basically equivalent. I wasn't sure which one you were using, but most settings are the same.

You could use something similar to the autocmd listed here to only apply the settings if you open your beancount file: https://stackoverflow.com/questions/18932012/how-to-load-different-vimrc-file-for-different-working-directory

You could even apply it to *.beancount.

xentac avatar Apr 24 '23 21:04 xentac

I do know autocmd, but how can I config the Python path in autocmd? And how can I scope it to just the code run by this plugin?

upsuper avatar Apr 24 '23 21:04 upsuper

There's only one python interpreter per vim instance. When you modify sys.path, you are modifying it for your entire vim session.

You could put something like

autocmd BufRead,BufNewFile *.beancount python3 import sys; sys.path.append('<venv path>/lib/python3.11/site-packages')

in your .vimrc and it would have the same effect.

xentac avatar Apr 25 '23 06:04 xentac

Oh, okay, thanks. Good to know.

For record, I added the following to my .vimrc:

autocmd BufRead,BufNewFile *.beancount python3 import sys; sys.path.append('<venv path>/lib/python3.11/site-packages'); import beancount; sys.path.pop()

which seems to work without polluting the import path for the interpreter.

upsuper avatar Apr 25 '23 12:04 upsuper