Reputation: 3759
I've been writing the following class definition in Sublime Text for python
class BPHmanager(object):
"""Class for BPHmanager"""
def makeNewProject(self, projname):
if os.path.exists(projname):
print "Directory of that name already exists in current directory."
sys.exit(2) # exit the program.
else:
os.mkdir(projname)
os.chdir(projname)
os.mkdir(".bph")
os.mkdir(".bph/deletes")
defaults = raw_input("Add default datatypes to project? Yy/Nn > ")
if defaults == 'Y' or defaults == 'y':
self.Datatypes = {"reads": [], "assemblies": [], "alignments":[], "annotations":[]}
self.MetadataDefs["reads"] = []
self.MetadataDefs["assemblies"] = []
self.MetadataDefs["alignments"] = []
self.MetadataDefs["annotations"] = []
def saveSettings(self):
settingsfile = open(".bph/bph.settingsfile", 'w')
settingsdict = {self.Organisms, self.Datatypes, self.MetadataDefs}
settingsfile.write(json.dumps(settingsdict, separators=(',',':')))
settingsfile.close()
print "Saved changes to .bph/bph.settingsfile"
def __init__(self, argv):
# and so on
For some reason, if I import this file to test it or copy and paste into the interpreter, I always get many indentation errors, starting with:
def saveSettings(self):
File "<stdin>", line 1
def saveSettings(self):
^
IndentationError: unexpected indent
But The line for the saveSettings
function begins at the same indentation level as the previous function, so I can't figure out why I'm getting this error.
Upvotes: 0
Views: 8843
Reputation: 589
It's most likely that you have an inconsistent mix between tabs and spaces in the file. Nowadays tabs are often thought of as 4 spaces, but there is actually a tab character that gets inserted when you hit the tab key.
However, because using 4 spaces for indentation instead of tabs has started to become something of a standard, many text editors will default to using 4 spaces as the method of auto-indentation, which is what results in a mixture of tab characters (ones that you hit the tab key for), and 4 spaces (ones that the editor put in for you). Python really doesn't like that, and will error. Although spaces are becoming a standard, you need to pick one and be consistent.
In sublime (I'm assuming Sublime 2), you can convert your leading tabs/spaces, and also set it to automatically expand your tabs to 4 spaces when you hit the tab key. This is outlined in the Sublime docs here:
https://www.sublimetext.com/docs/2/indentation.html
Upvotes: 2