Python Forum
IndentationError when no problem with indentation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndentationError when no problem with indentation
#1
I have a legacy Python script that I'm attempting to get running; this code was functional at the time of development. However, when I attempt to run it, I get the following error message:

Error:
line 36 tick.label.set_fontsize(fontsize) ^ IndentationError: expected an indented block
However, this is confusing, since I haven't modified the code, and line 36 is just a regular line, with no spaces or tabs at the beginning of this line. I located the following relevant thread that discusses this error:

Indentation error in Python
https://stackoverflow.com/questions/1497...-in-python

However, all of the proposed solutions assume that there actually exists an indentation error; in contrast, I don't believe that my code has indentation problems (e.g. if I hit my left arrow key at the beginning of line 36, the cursor moves to the first space of the line above, so there are no hidden spaces or tabs present...), but rather, I am wondering if there is some other underlying issue that would be causing this seemingly irrelevant error message...

If it is helpful, line 36 is the first line of substantial code following the imports and declarations section, and the code is:
labels = pd.read_csv('input.csv')
.

Any insights on what might be going wrong here, will be appreciated. Thanks!!
Reply
#2
We really need to see more of your code. Errors, especially indentation errors, are often caused by thing on the lines previous to the line flagged. If it's on line 36, give us the first 40 lines of code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks for your interest in assisting, ichabod; the code is below: (I'll note that the forum editor doesn't seem to allow me to include a blank line as the first line of the code, so what I reference as line 36 above is shown as line 35 below)

# In [1]

import os, sys, csv, gzip
import numpy as np
import pandas as pd
import scipy.sparse as sp
from scipy import io

import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['pdf.fonttype'] = 42 ## Output Type 3 (Type3) or Type 42 (TrueType)
rcParams['font.sans-serif'] = 'Arial'
#matplotlib inline

from plots import COLORS10, enlarge_tick_fontsize
import seaborn as sns
sns.set_style('whitegrid')

from sklearn.cross_validation import (StratifiedKFold, cross_val_score)
from sklearn.metrics import *
from sklearn.decomposition import TruncatedSVD
from sklearn import ensemble
from sklearn.pipeline import Pipeline
import xgboost as xgb

# /Library/Python/2.7/site-packages/IPython/html.py:14: ShimWarning: The `IPython.html` package has been deprecated. You should import from `notebook` instead. `IPython.html.widgets` has moved to `ipywidgets`.
#  "`IPython.html.widgets` has moved to `ipywidgets`.", ShimWarning)

# ===============================================================================
# STAGE 1: ...
# ===============================================================================

# Load input file

labels = pd.read_csv('input.csv')
print (labels.shape)
labels.head()

# output: (232, 7) + a table
Reply
#4
I don't know what's going on, because I am not getting an indent error when I run that code. It runs up to the plot import, and fails because I don't have plot installed. But an indent error should come up before that. I added an indent error to make sure, and it does.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
you post some code and it does not match your error.
line 36 according to error msg is tick.label.set_fontsize(fontsize)
That is not present in your code and line 36 is something else, i.e. - the code you post is not the one that produce the error. Post the relevant code as well as full traceback
Reply
#6
Quote:I don't know what's going on, because I am not getting an indent error when I run that code. It runs up to the plot import, and fails because I don't have plot installed. But an indent error should come up before that. I added an indent error to make sure, and it does.

Thanks for your update, ichabod; I have to wonder whether this is an issue of (a) the code having been developed with Python 2, and my current use of Python 3, and/or (b) the code having been developed on a Mac, while I'm using a Windows 10 PC... I had to change various syntax instances to get past various other compiler issues, e.g. the original code hadn't placed parentheses around the arguments of print statements, and I had to add these.

@buran: Thanks for your response. As I note in my post above, since the forum editor won't allow me to post a blank line as the first line of the code, as is included in my original script, what is shown as line 35 in the code above, is actually line 36 when my code runs. The full traceback is provided in my OP; apart from listing the working directory path and filename, there is no other info provided in the error message.

Update: When I removed the initial blank line from my script and re-ran it, I see that the error message continues to refer to line 36, and that this refers to the plots.py script, rather than to my script!

Error:
File "C:\...\plots.py", line 36 tick.label.set_fontsize(fontsize) ^ IndentationError: expected an indented block
Thanks for your helpful observation, buran; I corrected the legitimate indentation error at line 36 in the plots.py file, and this IndentationError message has now been resolved.
Reply
#7
Regardless of the line number, as buran pointed out, the code you posted does not include the line
  tick.label.set_fontsize(fontsize)

You need to post the code that includes this line as well as several lines before.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#8
Thanks for your recommendation, sparkz_alot; please see my updated post above where I clarify that the error message had been related to the supporting plots.py script rather than to my main script... Sorry that I didn't notice that distinction in the first place...
Reply
#9
Where did plots.py come from, and why is it broken?  :p
Reply
#10
Thanks for your question, nilamo; plots.py came from a repository associated with the other code above, and I don't know why the indentation error was present, but it was easy to fix once I recognized the correct file in which that error was occurring...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndentationError: unexpected indent Grin 3 4,863 May-15-2018, 06:49 AM
Last Post: buran

Forum Jump:

User Panel Messages

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