0

I've created some Python virtual environments:

hmanx ~ » pyenv virtualenvs
  3.7.4/envs/nn (created from /home/roach/.pyenv/versions/3.7.4)
  3.8.2/envs/py38 (created from /home/roach/.pyenv/versions/3.8.2)
  nn (created from /home/roach/.pyenv/versions/3.7.4)
  py38 (created from /home/roach/.pyenv/versions/3.8.2)
  vim-jupyter (created from /usr)

And I want to use the virtualenv nn, I have installed some packages, like numpy.

What I expected is command :py3 import numpy could work.

What I've tried:

  1. use vim plugin: Plug 'lambdalisue/vim-pyenv', but after run PyenvActivate nn command :py3 import numpy get error: ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'.
  2. set let &pythonthreehome='/home/roach/.pyenv/versions/3.7.4/envs/nn/bin/', get error: ModuleNotFoundError: No module named 'numpy'
  3. export PYTHONPATH="/home/roach/.pyenv/versions/nn/bin":$PYTHONPATH, then run vim get error same to method 1.

So, how to let vim use python in a virtual environment?

Or could use virtual python version when run

python3 << EOF

EOF

in a vim script.

1

Here are the commands I use to create a python 3.6.0 pyenv on a Mac using homebrew. Please note that I use neovim but the process to create a pyenv will not be much different for vim.

  1. Install readline and xz along with pyenv and pyenv-virtualenv

    brew install pyenv pyenv-virtualenv readline xz
    
  2. Install python 3.6.0

    CFLAGS="-I$(brew --prefix openssl)/include" \
    LDFLAGS="-L$(brew --prefix openssl)/lib" \
    pyenv install -v 3.6.0
    
  3. Create the pyenv in your $HOME directory (~/.venv):

    ~/.pyenv/versions/3.6.0/bin/python3 -m venv ~/.venv
    ~/.venv/bin/python3 -m pip install --upgrade pip
    ~/.venv/bin/python3 -m pip install neovim pynvim
    
  4. Set the g:python3_host_prog variable in your ~/.vimrc:

    let g:python3_host_prog=$HOME.'/.venv/bin/python3'
    
  5. Check to make sure everything is correctly set up in neovim.

    :checkhealth
    
1
  • Thanks for your reply, but for vim there is no python3_host_prog support. – roachsinai Aug 8 '20 at 5:52
0

A method is decalare export PYTHON_CONFIGURE_OPTS='--enable-shared' before pyenv install a virtual env(thanks to @puremourning):

env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.8.2, then activate it before open file use vim.

But still have a question, as my system python version is 3.8.3, virtual version 3.8.2 works. But if I install a new virtual env 3.7.4, failed.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.