Python和Nvim:如何同时激活全局和本地虚拟环境

2024-04-30 03:53:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Nvim中使用了^{},遇到了一些问题

首先,在Nvim中,运行:checkhealth会导致以下错误消息:

## Python 3 provider (optional)
 42   - WARNING: No Python executable found that can `import neovim`. Using the first available executable for diagnostics.
 43   - ERROR: Python provider error:
 44     - ADVICE:
 45       - provider/pythonx: Could not load Python 3:
 46           /home/<project path>/.venv/bin/python3 does not have the "neovim" module. :help provider-python
 47           python3.7 not found in search path or not executable.
 48           python3.6 not found in search path or not executable.
 49           python3.5 not found in search path or not executable.
 50           python3.4 not found in search path or not executable.
 51           python3.3 not found in search path or not executable.
 52           /home/<project path>/.venv/bin/python does not have the "neovim" module. :help provider-python
 53   - INFO: Executable: Not found

这很好,而且非常有意义。使用:checkhealth时,添加pynvim包(pip install pynvim)将导致以下结果:

# Python 3 provider (optional)
 42   - INFO: `g:python3_host_prog` is not set.  Searching for python3 in the environment.
 43   - INFO: Multiple python3 executables found.  Set `g:python3_host_prog` to avoid surprises.
 44   - WARNING: Your virtualenv is not set up optimally.
 45     - ADVICE:
 46       - Create a virtualenv specifically for Neovim and use `g:python3_host_prog`.  This will avoid the need to install the pynvim module in each virtualenv.
 47   - INFO: $VIRTUAL_ENV matches executable
 48   - INFO: Executable: /home/<project path>/.venv/bin/python3
 49   - INFO: Other python executable: /usr/bin/python3
 50   - INFO: Other python executable: /bin/python3
 51   - INFO: Python version: 3.9.5
 52   - INFO: pynvim version: 0.4.3
 53   - OK: Latest pynvim is installed.

这也有道理。避免全局安装。然而,我遇到的问题是,这需要我同时启用两个独立的虚拟环境。一个单独用于pynvim,另一个用于所有本地项目依赖项,我不知道应该如何做到这一点

如果启用全局,则coc-pyright找不到任何本地模块,并将抛出错误。如果我启用了全局,那么nvim就没有它需要的pynvim

你该怎么做


Tags: orthepathininfoforsearchbin
2条回答

很抱歉我误解了你的帖子。我真的想了想你昨晚睡觉前我以为你是什么意思。所以,我向你道歉

我想我应该问问你是否找到了解决办法

如果没有,您是否使用任何pip/venv包装器,如“pipenv”或“virtualenvwrapper”(这可能会被弃用)?我询问的唯一原因是我知道“pipenv”将允许您在项目的“Pipfile”中实际指定dev dep。那么,你不能将neovim和pyright作为开发副总裁添加到你正在进行的任何项目中吗

如果这不是一个选项,“venv”有一个命令行参数,允许它访问系统的python包

usage: venv [-h] [ system-site-packages] [ symlinks |  copies] [ clear]
            [ upgrade] [ without-pip] [ prompt PROMPT] [ upgrade-deps]
            ENV_DIR [ENV_DIR ...]

Creates virtual Python environments in one or more target directories.

positional arguments:
  ENV_DIR               A directory to create the environment in.

optional arguments:
  -h,  help            show this help message and exit
   system-site-packages
                        Give the virtual environment access to the system
                        site-packages dir.
   symlinks            Try to use symlinks rather than copies, when symlinks
                        are not the default for the platform.
   copies              Try to use copies rather than symlinks, even when
                        symlinks are the default for the platform.
   clear               Delete the contents of the environment directory if it
                        already exists, before environment creation.
   upgrade             Upgrade the environment directory to use this version
                        of Python, assuming Python has been upgraded in-place.
   without-pip         Skips installing or upgrading pip in the virtual
                        environment (pip is bootstrapped by default)
   prompt PROMPT       Provides an alternative prompt prefix for this
                        environment.
   upgrade-deps        Upgrade core dependencies: pip setuptools to the
                        latest version in PyPI

Once an environment has been created, you may wish to activate it, e.g. by
sourcing an activate script in its bin directory.

system-site-packages可能会有所帮助

也许可以全局安装neovim和pyright,然后在创建项目venv时指定此标志。然后将g:python3_host_prog指向您为项目新建的venv。然后,venv应该可以访问它需要的所有包,项目中的包以及您的全局“neovim”和“pyright”

再一次,我为误会感到抱歉。那一定给人的印象是超级屈尊

如果你找到了适合自己情况的解决方案,请告诉我你做了什么。如果没有,让我知道我的建议是否有效

谢谢,祝你今天愉快

coc.nvim和coc pyright不需要pynvim就可以工作,您不需要安装这个模块

相关问题 更多 >