Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Traceback error
#1
I'm trying to import a class into another Python file. The previous example I have for this is something that Larz60+ built for me but he didn't set it up this way.

In any event, this is what I'm faced with in the example I'm trying to learn.

This is the class I've made called "Students.py:

class student:

    def__init__(self, name, major, GPA, is_on_probation):
        self.name = name
        self.major = major
        self.GPA = GPA
        self.is_on_probation = is_on_probation
This is where we import it:

from Students import Students

student1 = Student("Jim", "Business", 3.1, False)

print(student1)
This is the error:

Error:
C:\Python365\python.exe F:/USERS/Tonya/Python/Draft/App.py Traceback (most recent call last): File "F:/USERS/Tonya/Python/Draft/App.py", line 1, in <module> from Students import Students File "F:\USERS\Tonya\Python\Draft\Students.py", line 3 def__init__(self, name, major, GPA, is_on_probation): ^ SyntaxError: invalid syntax Process finished with exit code 1
As always - any help you can provide is most appreciated!
Reply
#2
The Problem is that you are missing spaces between def __init__ and (self, ...)
so just replace the line
def__init__(self, name, major, GPA, is_on_probation):
with
def __init__ (self, name, major, GPA, is_on_probation):
Maybe you have a little type error there, but I'm a bit surprised that the import is working, because the student class is called "student", but you imported "Students" and used it with "Student"
Reply
#3
Ok - cool - I've made the changes you've suggested. Now I'm getting another error.

My code for students: I've tried changing 'students' with 'student' and either way I get this error. I am using PyCharm and it has a light bulb that suggests 'renaming the element'. What does that mean?:

class students:

    def __init__ (self, name, major, GPA, is_on_probation):
        self.name = name
        self.major = major
        self.GPA = GPA
        self.is_on_probation = is_on_probation
My code for App. Here, I've tried naming everything to student (except for the file name). Here I get a light bulb suggesting I 'convert to import students'. Which caused another error so I switched it back. On another note, when I change 'from Students import Students' to 'from Students import student' the whole line became grey. Is that the way it should be?

from Students import Students

student1 = Students("Jim", "Business", 3.1, False)

print(student1)
Error:
C:\Python365\python.exe F:/USERS/Tonya/Python/Draft/App.py Traceback (most recent call last): File "F:/USERS/Tonya/Python/Draft/App.py", line 1, in <module> from Students import Students ImportError: cannot import name 'Students' Process finished with exit code 1
I really appreciate your help! Thank you!
Reply
#4
You need to get the capitalization the same as well. In Python, students is different than Students.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Let's break it down step by step ;)
You have a file named Students.py that is the module you are trying to import. Inside of this module you have a class named students (lets rename it to student again, because it fits perfectly). Since you tryied to create an instance of the class by typing Students("Jim", "Business", 3.1, False) the compiler thinks that you have a different class called Students so the students class you loaded from the Students module is not used :) The most important step is to use the class name while importing. If you like you can then even give it a different name, but until this step the names should match. If you have decided to use a one of these possibilities you have to stick with that name, so when you call the constructor you have to use the right name.
(just keep track of when you use capital and when you used lower letters)

Possibility 1:
Students.py
class student: #this is the class name that we have to use to import it into a different file
 
    def __init__ (self, name, major, GPA, is_on_probation):
        self.name = name
        self.major = major
        self.GPA = GPA
        self.is_on_probation = is_on_probation
the main script
from Students import student #here we use the class name from Students.py to import it into this file
 
student1 = student("Jim", "Business", 3.1, False) #the exact same name has to be used here as the constructor
 
print(student1)
Here you see that we use Students (the file name) as where to import from and then tell the computer what to import. Here we want the class student to be imported. When this import is done student has to be used while calling its constructor.

Possibility 2:
Students.py
class student: #still the class name which has to be used when you like to import it
 
    def __init__ (self, name, major, GPA, is_on_probation):
        self.name = name
        self.major = major
        self.GPA = GPA
        self.is_on_probation = is_on_probation
the main script
from Students import student as Student #now we still use the class name from Students.py, but we giving it an alias name. Here Student
 
student1 = Student("Jim", "Business", 3.1, False) #We use the alias now to call the constructor
 
print(student1)
Here whe still import from the Students.py file and still want to import the class student that is defined, but we are renaming it (or let's say we are giving it an alias name). In this case Student. Now we call the constructor, but we have to use the alias Student
Reply
#6
Both of you - thank you for your help!

icabod801 - thank you for pointing that out. I should have caught that!

ThiefOfTime - thank you. Both of these possibilities seemed to work in that they processed with the exit code of 0. It wasn't what I expected though. Here it is:

Error:
C:\Python365\python.exe F:/USERS/Tonya/Python/Draft/App.py <Students.student object at 0x00000000029286D8> Process finished with exit code 0
Thank you!
Reply
#7
Well, you created an object, if you try to print it this kind of output will occure.
if you want a neatly looking output when you convert it to string (or when you want to print it), you need to change the Students.py to this:
class student: #this is the class name that we have to use to import it into a different file
  
    def __init__(self, name, major, GPA, is_on_probation):
        self.name = name
        self.major = major
        self.GPA = GPA
        self.is_on_probation = is_on_probation
    def __repr__(self):
        return "{} {} {} {}".format(self.name, self.major, self.GPA, self.is_on_probation)
This will let you have an defined output, when you try to print it :)
Reply
#8
Now that is REALLY COOL! Thank you! That went further than the YouTube video I've been watching.

Thanks again!
Reply
#9
there are some methods each class shares, like __init__ , __repr__ , __str__ , etc. They are very useful and even I need to use them more *shame on me* xD
no problem, mate. classes are a huge field, it is worth diving deep into.

cheers ;)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Traceback error tjnichols 3 2,611 Sep-25-2018, 07:24 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020