我正在处理导入类(请参阅下面的代码),我遇到了以下错误:
下面是文件的代码
admin_instance.py
,我在admin_instance.py
中的admin下看到了波浪线
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()
代码在 VS Code 中运行,但它说
Import 'admin' could not be resolved Pylance (reportMissingImports) [Ln 1, Col 8]
。
我的两个文件都在同一目录中,所以我不确定这里的错误是什么。请帮我解决这个问题!请看下面的截图。
您打开的项目文件夹不是“admin.py”和“admin_instance.py”的父文件夹(或者“admin.py”的路径将是相对路径而不是“c:> ...”)。因此这些文件被视为独立的 Python 代码文件。
选择VScode菜单“文件”->“打开文件夹”,打开“C:...\Python work\importing”,你会看到不同之处。