skip to Main Content

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


  1. The line you show to activate the virtual environment does not work in Vim:

    :. venv/bin/activate
    

    Vim interprets the . to stand for the current line and venv is not a valid Ex command. You’ll get

    E492: Not an editor command: . venv/bin/activate
    

    You would need to run it in a shell using the :! command, like your call to pip above:

    :!. venv/bin/activate
    

    This will run successfully but is flawed as well. As explained in :help :! and :help 'shell', Vim will invoke a subshell and source 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.

    Login or Signup to reply.
  2. The purpose of this command:

    $ source .venv/bin/activate
    

    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:

    :!pip install pandas
    

    you effectively:

    1. Create a new sub-shell that inherits the parent’s virtual environment.
    2. Install pandas in that virtual environment.
    3. Quit that sub-shell.

    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:

    :!source .venv/bin/activate
    

    in Vim serves no purpose whatsoever, as…

    • it does nothing package-specific,
    • whatever changes it does — if any — to the sub-shell’s environment dies with it,
    • it can’t be expected to do anything to Vim’s environment or to its parent anyway.

    Now let’s go back to the initial, under-specified, problem:

    1. You install Pandas in the current virtual environment.
    2. Something complains that Pandas isn’t installed.

    I went trough the following steps in order to get a sense of what you are dealing with:

    1. Create a dummy project and cd into it:

      $ mkdir testpython
      $ cd $_
      
    2. Create a virtual environment:

      $ python3 -m venv .venv
      
    3. Activate it:

      $ source .venv/bin/activate
      
    4. Open a dummy Python file in Vim:

      $ vim foo.py
      
    5. Put some dummy code in foo.py:

      import pandas as pd
      s = pd.Series([1, 2, 3, 4, 5])
      print(s)
      
    6. Test it without Pandas installed:

      Traceback (most recent call last):
        File "/Users/romlafou/Documents/foopython/foo.py", line 1, in <module>
          import pandas as pd
      ModuleNotFoundError: No module named 'pandas'
      
      shell returned 1
      

      Well yeah, Pandas is not installed.

    7. Install Pandas from Vim:

      :!pip install pandas
      
    8. Run the buffer through the interpreter:

      :w !python3
      0    1
      1    2
      2    3
      3    4
      4    5
      dtype: int64
      

      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:

    • a "virtual environment" is really a bunch of environment variables,
    • when you start Vim, it inherits the virtual environment of its parent process
    • installing a package in the current virtual environment is not expected to change the virtual environment, whether it is installed from within Vim or not,
    • the package is immediately available to any 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:

    :py3file %
    

    which indeed throws an ugly error:

    Traceback (most recent call last):
      File "", line 1, in <module>
      File "foo.py", line 1, in <module>
        import pandas as pd
    ModuleNotFoundError: No module named 'pandas'
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search