Mistake in importing the class

30 views Asked by At

I was working with importing classes (please see my code below) and I ran into the following mistake:

Below is the code of the file admin_instance.py and I see the squiggly line under admin in admin_instance.py

import admin

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

and this one is the code of 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()

The code is running in VS Code, but it says that Import 'admin' could not be resolved Pylance (reportMissingImports) [Ln 1, Col 8].

Both of my files are in the same directory, so I'm not sure what the mistake is here. Please help me fix that issue! Please see the screenshot below.

enter image description here

1

There are 1 answers

4
Chris Fu On BEST ANSWER

The project folder you have opened is not the parent folder of "admin.py" and "admin_instance.py" (or the path of "admin.py" would be relative instead of "c: > ..."). So those files are being treated as isolated Python code files.

Select VScode menu "File" -> "Open Folder" and open "C:...\Python work\importing", and you will see the difference.