Python Forum
IndentationError: unexpected indent
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndentationError: unexpected indent
#1
The following is the class that received the IndentationError: unexpected indent. I got the error when "from reader.reader import Reader". Which indent is not supposed to? Thanks.

 class Reader:
    def __init__(self, filename):
        self.filename = filename
        self.f = open(self.filename, 'rt')

    def close(self):
        self.f.close()

    def read(self):
        return self.f.read()
Error:
Traceback (most recent call last): File "...\Python Project\venv\Main.py", line 1, in <module> from reader.reader import Reader File "...\Python Project\venv\reader.py", line 1 class Reader: IndentationError: unexpected indent
snippsat write May-01-2022, 10:02 PM:
Added code tag in your post,look at BBCode on how to use.
You have 1 space before class Reader,there should be 0 space.
Reply
#2
Please post your code in python tags.
dee likes this post
Reply
#3
Copy and paste likely fixed the error because I have no problems. It could be a mix of tabs and spaces. All indentation should be 4 spaces. My editor sees your indentation as 3 spaces for the first level (def's) and 4 spaces for the second level. That is bad, but usually not a syntax error.
Reply
#4
(May-01-2022, 10:44 PM)deanhystad Wrote: Copy and paste likely fixed the error because I have no problems. It could be a mix of tabs and spaces. All indentation should be 4 spaces. My editor sees your indentation as 3 spaces for the first level (def's) and 4 spaces for the second level. That is bad, but usually not a syntax error.

Thanks. It is working now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  getting unexpected indent errors trying to move cells up jensengt 4 874 Jun-28-2023, 12:05 PM
Last Post: deanhystad
  xml indent SubElements (wrapping) with multiple strings ctrldan 2 1,473 Jun-09-2023, 08:42 PM
Last Post: ctrldan
  Avoid multiple repeat in indent Frankduc 8 2,897 Jan-18-2022, 05:46 PM
Last Post: Frankduc
  error "IndentationError: expected an indented block" axa 4 2,921 Sep-08-2020, 02:09 PM
Last Post: ibreeden
  IndentationError: unexpected indent jk91 1 2,389 Feb-27-2020, 08:56 PM
Last Post: buran
  could not fix unexpected indent Bayan 1 3,198 Nov-08-2019, 01:45 PM
Last Post: ichabod801
  IndentationError jagannath 1 2,473 Nov-04-2019, 07:41 AM
Last Post: buran
  unable to indent json data dataplumber 4 3,012 Oct-22-2019, 01:55 PM
Last Post: dataplumber
  IndentationError: expected an indented block ryder5227 2 2,607 Sep-27-2019, 06:59 PM
Last Post: jefsummers
  IndentationError on installed package evvvonder 3 3,037 Jun-29-2019, 10:30 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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