added function to activate a virtualenv if there is any in the directory of the python files

This commit is contained in:
Stefan Friese 2025-02-26 10:04:38 +01:00
parent 6bf6e6ad1e
commit bf71ea8fba
1 changed files with 23 additions and 0 deletions

View File

@ -38,3 +38,26 @@ let g:ale_fixers = {
\ '*': ['remove_trailing_lines', 'trim_whitespace'], \ '*': ['remove_trailing_lines', 'trim_whitespace'],
\ 'python': ['black', 'add_blank_lines_for_python_control_statements'], \ 'python': ['black', 'add_blank_lines_for_python_control_statements'],
\} \}
function! LoadVirtualEnv()
if filereadable('./.venv/bin/activate')
let g:virtualenv_path = getcwd() . '/.venv/bin/python'
let g:python3_host_prog = g:virtualenv_path
echo "Loaded virtual environment: " . g:virtualenv_path
elseif filereadable('./venv/bin/activate')
let g:virtualenv_path = getcwd() . '/venv/bin/python'
let g:python3_host_prog = g:virtualenv_path
echo "Loaded virtual environment: " . g:virtualenv_path
else
" If no virtualenv is found, fall back to the default Python
" Check if variables exist before unsetting them
if exists('g:virtualenv_path')
unlet g:virtualenv_path
endif
if exists('g:python3_host_prog')
unlet g:python3_host_prog
endif
endif
endfunction
autocmd BufEnter * call LoadVirtualEnv()