neoclide / coc-python

Python extension for coc.nvim, fork of vscode-python
574 stars 51 forks source link

Doesn't work with virtualenv #233

Closed ichirou2910 closed 4 years ago

ichirou2910 commented 4 years ago

Hi, I work on multiple Python projects and I don't want to install all neccessary packages globally. The problem is coc-python only detects globally installed packages and suggests/diagnose based on them but not ones installed in virtualenv, which annoys me quite a bit. Is there any way to config coc-python to work with the virtualenv I am working on? Thanks for reading. Sample image: image

rbhanot4739 commented 4 years ago

You can specify the pythonPath per workspace and then it will work as expected. https://github.com/neoclide/coc.nvim/wiki/Using-the-configuration-file#configuration-file-resolve

ichirou2910 commented 4 years ago

Thanks. It worked well ^^ One more question though. Is it possible to automatically create a local coc-settings.json when I create a new virtualenv with pythonPath set to python of that virtualenv?

ichirou2910 commented 4 years ago

Hi, after playing around a bit I found the solution (for anyone using virtualenvwrapper like me) Put these lines at the end of function mkvirtualenv in ~/.local/bin/virtualenvwrapper.sh or /usr/local/bin/virtualenvwrapper

    PROJ_PATH="`dirname \"$0\"`"              # relative
    PROJ_PATH="`( cd \"$PROJ_PATH\" && pwd )`"  # absolutized and normalized

    echo "$PROJ_PATH/$1"

    mkdir -p $PROJ_PATH/$1/.vim

    cat >> $PROJ_PATH/$1/.vim/coc-settings.json <<- EOF
    {
        "python.pythonPath": "~/.virtualenvs/$1/bin/python"
    }
EOF

Remember to source virtualenvwrapper.sh again. After that, executing mkvirtualenv will generate the coc-settings.json file with pythonPath set accordingly