简体   繁体   English

导入无法解析,但程序运行

[英]Import could not be resolved but program runs

I'm learning to code in python and importing my own modules.我正在学习在 python 中编码并导入我自己的模块。 The modules are in the same directory.这些模块位于同一目录中。 This is my code:这是我的代码:

main.py主文件

from test import hallo,myAge,myDriver,Fahrzeug
from person import goodbye,Persona

    def main():
        
        print("---Start Test---")
    
        hallo()
        print(myDriver)
        print(myAge)
    
        opel = Fahrzeug(26,110,"Zafira")
        print(opel.getGeschwindigkeit())
    
        print("---Person---")
        
        goodbye()
        emil = Persona("Emil",22)
        print(emil.getName())
    
        print("---End Test---")
    
    if __name__ == '__main__':
        main()

test.py测试.py

#Random Variables
myDriver = "Hans"
myAge = 47

#Random Function
def hallo():
    print("Hallo I'm your driver")

#Class
class Fahrzeug:
    geschwindigkeit = 0
    ps = 0
    name = ""

    def __init__(self, gesch, p, nam):
        self.geschwindigkeit = gesch
        self.ps = p
        self.name = nam

    def getGeschwindigkeit(self):
        return self.geschwindigkeit

person.py人.py

def goodbye():
    print("Goodbye")

class Persona:
    name = ""
    alter = 0

    def __init__(self,na,alt):
        self.name = na
        self.alter = alt

    def getName (self):
        return self.name

I get following problem message:我收到以下问题消息:

Import "person" could not be resolved Pylance(reportMissingImports) [2,6]

If I run the program, the program will still work despite the message.如果我运行该程序,尽管有消息,该程序仍然可以运行。 Why do I get the problem message?为什么我会收到问题消息?

My View in Visual Studio Code我在 Visual Studio Code 中的视图

In settings.json file you have to add the paths from which you import what's needed in extraPaths ie the path to the root of your project:settings.json文件中,您必须添加从中导入 extraPaths 所需内容的路径,即项目根目录的路径:

{
    "python.pythonPath": "/home/youruser/.virtualenvs/app-FzQGSFjf/bin/python",
    "python.analysis.extraPaths": ["app", "another/path/etc"]
}

Also, I notice that you have saved your module inside .vscode folder, try to move your module out of the .vscode folder as the .vscode folder is usually reserved for VSCode related settings and not your project source code另外,我注意到您已将模块保存在.vscode文件夹中,请尝试将模块移出.vscode文件夹,因为.vscode文件夹通常保留用于 VSCode 相关设置,而不是您的项目源代码

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

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