简体   繁体   English

如何在 VSCode 中“转到定义”到 super class 的方法

[英]How to "go to definition" to a method of super class in VSCode

I am familiar with Pycharm and new to VSCode.我熟悉 Pycharm 并且是 VSCode 的新手。 I would like to "go to definition" like I did in Pycharm but I can't figure out how to.我想像在 Pycharm 中那样“转到定义”,但我不知道该怎么做。 Btw I set up the environment in WSL2 Ubuntu and code from local with WSL VSCode extension.顺便说一句,我在 WSL2 Ubuntu 中设置了环境,并使用 WSL VSCode 扩展从本地编写代码。

Directory tree view is like :
src
 |-calculation
    |-base_class.py
    |-particular_class.py
 |-db_related_module
    |-db_class.py

and each module has:每个模块都有:

base_class.py基类.py

class BaseClass():

    def __init__(self, DbClass):
        self.db_cls = DbClass

particular_class.py特定类.py

from base_class import BaseClass
from db_related_module.db_class import DbClass


class ParticularCalClass(BaseClass):

    def __init__(self):
        super().__init__(
            DbClass
        )

    def some_calculation(self):
        do_db_stuff = self.db_cls
        ret = do_db_stuff.execute_sql()
        return ret


def main():
    calc_cls = ParticularCalClass()
    print(calc_cls.some_calculation())

main()

db_class.py数据库类.py

class DbClass():

    def __init__(self):
        pass

    def execute_sql():
        return "execute some sql"

Here I would like to jump from particular_class.py这里我想从 particular_class.py 跳转

ret = do_db_stuff.execute_sql()

to db_class.py到 db_class.py

def execute_sql():

as it is the definition.因为它是定义。

Normally that would be using F12 , or right click and go to definition.通常这将使用F12或右键单击和 go 来定义。

After testing this is the same problem on my machine.经过测试,这在我的机器上是同样的问题。 So I filed a report on GitHub and here is their response:所以我在GitHub上提交了一份报告,这是他们的回复:

The import in line from base_class import BaseClass is incorrect in this scenario.在这种情况下, from base_class import BaseClass中的导入是不正确的。 It should be from calculation.base_class import BaseClass .它应该是from calculation.base_class import BaseClass And you will need to have PYTHONPATH=./src .你需要有PYTHONPATH=./src If you have from base_class import BaseClass python won't know where the base_class module is, so you won't see go to definition working.如果你有from base_class import BaseClass python 将不知道base_class模块在哪里,所以你不会看到 go 定义工作。 you will wither have to add calculation directory to sys.path or change the import as I recommended above.您将不得不将calculation目录添加到sys.path或按照我上面的建议更改导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM