[英]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 befrom calculation.base_class import BaseClass
.它应该是
from calculation.base_class import BaseClass
。 And you will need to havePYTHONPATH=./src
.你需要有
PYTHONPATH=./src
。 If you havefrom base_class import BaseClass
python won't know where thebase_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 addcalculation
directory tosys.path
or change the import as I recommended above.您将不得不将
calculation
目录添加到sys.path
或按照我上面的建议更改导入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.