You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code中导入同目录模块时Pylance提示无法解析的问题求助

VS Code中导入同目录模块时Pylance提示无法解析的问题求助

各位大佬好!我最近在练习Python的类导入时碰到了一个奇怪的问题:代码能正常运行,但VS Code里的Pylance一直给我标红报错,提示 Import 'admin' could not be resolved Pylance (reportMissingImports),两个文件明明在同一个目录里,实在搞不懂哪里出问题了,来求助大家!

先给大家说明下我的情况:
我有两个文件,admin_instance.py和admin.py,都放在同一个文件夹下面。

这是admin_instance.py的代码:

import admin

my_admin = admin.Admin('john', 'doe', 30, 'USA')
my_admin.describe_user()
my_admin.admin_privileges.show_privileges()

这是admin.py的代码:

class User:
    def __init__(self, first_name, last_name, age, location):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age
        self.location = location

    def describe_user(self):
        print(f"The full name of the user is {self.first_name.title()} {self.last_name.title()}." 
              f" He is {self.age} years old and he is from {self.location}.")

    def greet_user(self):
        print(f"Hello, {self.first_name.title()} {self.last_name.title()}!")

class Privileges:
    def __init__(self, privileges = ['can add post', 'can delete post', 'can ban user']):
        self.privileges = privileges

    def show_privileges(self):
        print("Administrator's set of privileges are: ", sep = "", end = " ")
        for x in self.privileges:
            print(x, end = " ")

class Admin(User):
    def __init__(self, first_name, last_name, age, location):
        super().__init__(first_name, last_name, age, location)
        self.admin_privileges = Privileges()

运行代码是没问题的,但Pylance就是在import admin这行下面画了红色波浪线,提示找不到这个导入(截图展示:VS Code编辑器中admin_instance.py的第一行import admin下方有红色波浪线,右侧悬浮提示框显示Import 'admin' could not be resolved Pylance (reportMissingImports) [Ln 1, Col 8])。

我确认过两个文件确实在同一个目录里,为什么Pylance会识别不到呢?有没有什么办法能解决这个提示问题呀?

备注:内容来源于stack exchange,提问作者RFZ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.17 12:48:02