General Background:
I’m learning Python and chose to make Vim my IDE – that’s Vi IMproved 9.0, with only 4 plugins: coc/.nvim, gruvbox, vim-commentary, and black (PyCharm was recommended, but there’s no regret in my choice with Vim. It’s awesome!). I’m currently building a cheap flight finder OOP, using Amadeus’ SDK installed in a virtualenv.
Set-up:
Vim: 9.0 (plugins include coc/nvim, gruvbox, vim-commentary, and black; Python: 3.8.10 [GCC 9.4.0]; OS: Ubuntu 20.04.6 LTS on Windows 10 x86_64; Kernel: 5.15.153.1-microsoft-standard-WSL2; and Shell: bash 5.0.17
Specific problem:
Along with the SDK, I installed other packages, like requests, I thought I would need. As I dug in, I realized I needed the Pandas package for my project, as well. So while in Vim, in my venv with 4 buffers open, I typed:
:! pip install pandas
This opened a terminal session and installed Pandas successfully.
Then I sourced the venv with:
:! . venv/bin/activate
When I returned to Vim, the line calling Pandas gave the error "Import "pandas" could not be resolved (Pyright reportMissingImports)" when the cursor was placed over the word Pandas. Nevertheless, it worked just fine when I opened a new Vim session, activated the venv, and called the project. Maybe what I’m trying to do simply can’t be done. But if it can, I’d appreciate your help in relaying the steps. Thanks.
2
Answers
The line you show to activate the virtual environment does not work in Vim:
Vim interprets the
.
to stand for the current line andvenv
is not a valid Ex command. You’ll getYou would need to run it in a shell using the
:!
command, like your call topip
above:This will run successfully but is flawed as well. As explained in
:help :!
and:help 'shell'
, Vim will invoke a subshell andsource activate
there.On completion of the command, the shell will exit and all changes to the environment will be lost.
The question is very unclear about the actual error and how it surfaces.
In case the error is caused by
pylint
, it may be worth noting that it has a hard time running in a virtual environment.See pylint doesn't point to virtualenv python
As clarified in your comments to this answer, OP is using coc.nvim with coc-pyright and thus pyright (really, this should be in the question!) It boils down to correctly set up pyright. Related questions are Pyright is not finding imports from libraries in a virtualenv and How to set python interpreter in neovim for python language server depending on pyenv / virtualenv
Otherwise, it depends on whatever external command and plugin you’re using. Vim itself doesn’t do any code checking. There are plugins which will show the errors external static code checkers found.
If you could consider to restart Vim but want to keep buffers and options, you may be interested in
:help :mksession
and:help :mkview
.The purpose of this command:
is to change a bunch of environment variables for the current process and its eventual children, the most important being
$PATH
.Those changes are carried over when you fork a new process (Vim, the Python REPL, a sub-shell, etc.) but the parent environment is typically copied into the child environment, making the two environments completely separate. Changing the parent environment has no effect on the child environment and vice-versa.
Activating a Python virtual environment is not something you do often and especially not something you do after installing a package, unless the package somehow requires it, which should be very rare and which is not the case with Pandas to begin with.
Now, when you do the following in Vim:
you effectively:
pandas
in that virtual environment.At no point did anything change in the child environment or in the parent environment. All that happened is that a bunch of files were added in a bunch of directories under
.venv/
. There is no need to "re-source" anything, so doing:in Vim serves no purpose whatsoever, as…
Now let’s go back to the initial, under-specified, problem:
I went trough the following steps in order to get a sense of what you are dealing with:
Create a dummy project and
cd
into it:Create a virtual environment:
Activate it:
Open a dummy Python file in Vim:
Put some dummy code in
foo.py
:Test it without Pandas installed:
Well yeah, Pandas is not installed.
Install Pandas from Vim:
Run the buffer through the interpreter:
That’s it. Vim doesn’t complain, Python doesn’t complain, the shell doesn’t complain, the terminal emulator doesn’t complain… Everything is fine.
FWIW, I also started a REPL in Vim’s terminal with
:term python3
and everything worked flawlessly. No complaint whatsoever about Pandas not being installed.To sum it up:
python
sub-process (!python3 %
,:w !python3
,:term python3
).Which begs the question: what is complaining about missing Pandas after you have installed it. To which we still have no answer.
My hypothesis is that you are trying to do something like:
which indeed throws an ugly error:
which is to be expected because the Python interpreter used by Vim under the hood is very unlikely to be the one in your
.venv/bin/
and it is very unlikely to be aware of any project-specific virtual environment.If you absolutely insist on using the
:py*
family of commands, then you should probably head off to:help if_pyth.txt
and try to work something out.But I wouldn’t recommend going that path as the other methods,
:[range]w !python3
being my favorite, all honor your virtual environments out of the box.