Python Forum
IndentationError when no problem with indentation - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: IndentationError when no problem with indentation (/thread-5501.html)

Pages: 1 2


IndentationError when no problem with indentation - BobLoblaw - Oct-07-2017

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/14979224/indentation-error-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!!


RE: IndentationError when no problem with indentation - ichabod801 - Oct-07-2017

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.


RE: IndentationError when no problem with indentation - BobLoblaw - Oct-07-2017

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



RE: IndentationError when no problem with indentation - ichabod801 - Oct-07-2017

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.


RE: IndentationError when no problem with indentation - buran - Oct-07-2017

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


RE: IndentationError when no problem with indentation - BobLoblaw - Oct-08-2017

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.


RE: IndentationError when no problem with indentation - sparkz_alot - Oct-08-2017

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.


RE: IndentationError when no problem with indentation - BobLoblaw - Oct-08-2017

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...


RE: IndentationError when no problem with indentation - nilamo - Nov-01-2017

Where did plots.py come from, and why is it broken?  :p


RE: IndentationError when no problem with indentation - BobLoblaw - Nov-02-2017

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...