Search code examples
pythonvisual-studiopython-importimporterror

How do I import my own object in vs studio code?


I'm trying to import the Human object I created for my contact book program and im doing it in VS Studio Code but it gives me an error:

Import "Human" could not be resolved

I tried to make the program in pycharm instead and it imported just fine and I even finished the program. And while looking around for a solution for the VS Studio Code version, I found some stuff like adding

"python.autoComplete.extraPaths": ["./**"],

to my settings.json and I did and a few other things i found on google, but it nothing helped.

VS Studio Code:

from Human import Human
#from Contact_Book_Project.Human import Human # auto generated, Not sure how it is different but i do have these 2 classes in a folder called Contact_Book_Project

book = []

def main():
    while (True):

        choice = int(input("Press 1 to add someone to your Contact Book\n2 to remove someone from the book\n3 to find someone\n4 to list everyone\n5 to exit\n"))

        if (choice == 5):
            break
        elif (choice == 1):
            name = input("Name: ")
            phoneNum = input("Phone Number: ")
            address = input("Address: ")

            person = Human(name, phoneNum, address)

            addPerson(person)

def addPerson(person):
    book.append(person)

if __name__ == "__main__":
    main()
class Human:
    def __init__(self, name, phone_Number, address):
        self.Name = name
        self.Phone_Number = phone_Number
        self.Address = address

    def getName(self):
        return self.Name

    def getPhoneNumber(self):
        return self.Phone_Number

    def getAddress(self):
        return self.Address

PyCharm Code:

from Human import Human

book = []


def main():
    while (True):

        try:
            choice = int(input(
                "Press 1 to add someone to your Contact Book\n2 to remove someone from the book\n3 to find someone\n4 to "
                "list everyone\n5 to exit\n"))

            if (choice == 5):
                break

            elif (choice == 1):
                name = input("Name: ")
                phoneNum = input("Phone Number: ")
                address = input("Address: ")

                person = Human(name, phoneNum, address)

                addPerson(person)

            elif (choice == 2):
                name = input("Enter name of person to remove: ")

                temp = 0
                for i in book:
                    if i.getName() == name:
                        book.pop(temp)
                        print(i.getName() + " removed.")
                        break

                    temp += 1

#            elif (choice == 3):
#                name = input("Enter name of person to check if they are in your contact book and retrieve their "
#                             "information: ")
#
#                if name in book:
#                    temp = book.__getitem__(name)
#                    print(
#                        "Name: " + temp.getName() + "\nPhone Number: " + temp.getPhoneNumber() + "\nAddress: " + temp.getAddress())
#                else:
#                    print(name + " does not exist in your contact book.")

            elif (choice == 4):
                for p in book:
                    print(
                        "Name: " + p.getName() + "\nPhone Number: " + p.getPhoneNumber() + "\nAddress: " + p.getAddress() + "\n")

        except ValueError:
            print("\nInvalid input.\n")

def addPerson(person):
    book.append(person)

if __name__ == '__main__':
    main()

both are using the same Human class. How can I fix this? Why is it throwing an error in VS Studio but work in Pycharm?


Solution

  • Maybe try directing VS Code to the exact location of the Human module:

    import sys
    sys.path.append('file path to Human.py') # Add file path to 'Human.py' here
    from Human import Human