【问题标题】:How do I get intellisense, auto-completion to work for PyGObject library installed in virtual venv on VSCode?如何在 VSCode 上的虚拟 venv 中获得安装在 PyGObject 库中的智能感知、自动完成功能?
【发布时间】:2021-08-10 20:18:56
【问题描述】:
OS = Ubuntu 18.04 64 bit LTS
VSCode version = 1.59.0

在以下代码中的“

import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

window = Gtk.Window(title="Hello World")
window.??? <<<<<<<< HERE
window.show()
window.connect("destroy", Gtk.main_quit)

if __name__ == "__main__":
    Gtk.main()

我在 VSCode 终端的虚拟环境中使用 pip3 安装了以下软件包:

pip3 install Pycairo
pip3 install PyGObject
pip3 install SQLAlchemy==1.4.22
pip3 install psutil
pip3 install pexpect
pip3 install websockets
pip3 install PyGobject PGObject-stubs

安装 PyGobject PGObject-stubs 后,我可以在 window. 之后看到以下内容:

window.__new__
window.__annotations__
window.__class__
window.__delattr__

和其他 "__xxx__" 选项,但没有常规选项,如连接、显示、添加、调整大小等。

这是我在 .vscode 目录下的 settings.json 代码:

{
    "python.defaultInterpreterPath": "/usr/bin/python3.9",
    "python.linting.enabled": false,
    "python.linting.pylintPath": "pylint",
    "editor.formatOnSave": true,
    "python.formatting.provider": "yapf", // or "black" here
    "python.linting.pylintEnabled": true,
    "[python]": {
        "editor.defaultFormatter": "ms-python.python"
    }
}

我必须怎么做才能让 VSCode 识别 PyGOject 库的自动完成功能?

【问题讨论】:

  • 你在虚拟环境中选择了解释器吗?调出命令面板 (Ctrl-Shift-P) 并开始输入“Python: Select Interpreter”,然后确保您使用的是虚拟环境的 Python 解释器。

标签: visual-studio-code ubuntu-18.04 pygobject


【解决方案1】:

较新的 PyGObject 版本是动态生成的,pylance 无法理解。

目前最好的选择是使用生成的文档:

https://lazka.github.io/pgi-docs/

在包中包含生成存根已完成一些工作,但尚未完成:

https://github.com/pygobject/pgi-docgen/pull/176

【讨论】:

    猜你喜欢
    • 2021-01-24
    • 2015-08-27
    • 2016-08-22
    • 2015-08-27
    • 2018-06-01
    • 2022-07-28
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    相关资源
    最近更新 更多