0

So I have 1 package student with one class Student and I have a main.py outside that package and I'm trying to create an object of Student example

class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id

Separate File main.py:

 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()

Any help will be appreciated.

2
  • I hope your indents aren't actually that bad. Commented Oct 13, 2013 at 5:06
  • 2
    you need to import the student in your main.py Commented Oct 13, 2013 at 5:08

2 Answers 2

4

While the class defined outside of your code, the class needs to be imported. Assume main.py and student.py are in the same folder:

student.py

class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id


main.py

def main():
    from student import Student
    print("is workign")
    temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()
Sign up to request clarification or add additional context in comments.

2 Comments

What if the student class in in a sub sub-directory? because I have a different package so the student.py is in another folder called object
from subdic1.subdic2.student import Student
0

in the main funtion just write - from student import Student. It is - from 'Filename' import 'Classname'

So

def main(): print("is workign") from student import Student temp = Student("50") ## I want to create the object of class Student and send an attribute

if name == 'main': main()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.