ThePikachuIH
ThePikachuIH

Reputation: 71

How do I make ale highlight using a virtual environment

I just started configuring Vim as an IDE, and the first file I launched into was one that happened to use a Virtual Environment.

In my .vimrc, I added ale as my lint engine, and coc as my autocomplete (intellisense) engine. So I entered, and saw there were errors. I exited vim, and then sourced my virtual environment and entered again, and there were still errors. For some reason, though, they were all at the imports.

I've tried:

I setup ale's linters and fixers as the following

 " ALE vars
 let g:ale_disable_lsp = 1
 let g:ale_linters = {
        \   'python': ['flake8'],
        \   'javascript': ['eslint'],
        \}
 let g:ale_fixers = {
 \   'python': ['black'],
 \}
 let g:ale_fix_on_save = 1
 let g:ale_sign_warning = '-!'
 let g:ale_virtualenv_dir_names = ['env']

but it still throws the errors

How do I make it warn using my Virtual Environment?

Upvotes: 3

Views: 2494

Answers (2)

Al-Baraa El-Hag
Al-Baraa El-Hag

Reputation: 873

I am going to assume that you are using Neovim (or that this solution will also work for Vim) but YMMV.

==> Run the command :checkhealth provider in neovim. You want to look for the part about python 3:

## Python 3 provider (optional)
  - INFO: Using: g:python3_host_prog = "/home/albaraa/Documents/venv_global/bin/python3"
  - INFO: Executable: /home/albaraa/Documents/venv_global/bin/python3
  - INFO: Python version: 3.8.5
  - INFO: pynvim version: 0.4.2
  - OK: Latest pynvim is installed.

============================================================================

If it doesn't say OK, just follow the prompts given:

  • If it can't find your python version, then in your init.vim file put the location of your virtualenv:
let g:python3_host_prog = '/home/albaraa/Documents/venv_global/bin/python3'
  • If it finds it and you still get an error, just follow the prompts. You will need to run this command INSIDE your virtual environment:
pip install neovim pynvim

============================================================================

If it says OK but it is still not working:

  • Make sure that you are running neovim in the python virtual environment. I have an alias for Neovim setup like this so that I can type in nvimp and have it do it automatically:
alias nvimp="source ~/Documents/venv_global/bin/activate && nvim"
  • Check whether you can access your python virtual environment inside neovim. I think some older versions had a problem with this:
" This should show your virtual environment
:which python

" This should show your virtual environment in the path
:echo $PATH

" This should show your virtual environment is in the path of the neovim command shell (has to be the same as above)
:!echo $PATH

Upvotes: 1

ThePikachuIH
ThePikachuIH

Reputation: 71

I seem to have fixed it. EDIT: I DID NOT FIX IT

In my .vimrc file, I just needed to indent this code block.

let g:ale_fixers = {
     \   'python': ['black'],
     \}

It must have made vim and ale confused and threw errors, especially since I put that before my g:virtualenv_dir_names variable.

UPDATE

That was not what was causing the problem

It was all to do with my virtual environments, for some reason. If anyone can explain this to me, I'll be thankful.

I needed to rm -dr my virtual environment I had before, and then remake and reinstall it.

Hope this helps some of you!

Upvotes: 3

Related Questions