0

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?

TheDasher
  • 31
  • 6

1 Answers1

2

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
Daniel
  • 275
  • 1
  • 9
  • Hey, thanks it partially worked, as in the program now works the same way it does in Pycharm but now there's a yellow squiggly line under the first Human in the line: from Human import Human. Which gives me the same error message as before, but runs fine. Any way to fix that? – TheDasher Feb 14 '21 at 20:41
  • So what i'm trying to say is that it now works in VS Studio the same way as it does in Pycharm but also still shows the same error message. Not sure why. – TheDasher Feb 14 '21 at 20:43
  • 1
    @TheDasher So the code now works? I'm not sure about the squiggly line issue, maybe [this](https://stackoverflow.com/questions/57333371/how-to-make-vs-code-read-python-imports-without-displaying-yellow-squigglies) will help? – Daniel Feb 14 '21 at 21:17