3

A sample of my python file is

def __init__(self, csvFile, chunksize=10000):
        self.newName = csvFile[:-4]
        import ipdb; ipdb.set_trace()
        self.csvFile=csvFile
        self.chunksize=int(chunksize)
        self.headers_without_timestamp = header_without_timestamp 

        self.total_rows = 0
        self.username=username
        self.password=password
        self.dbname="data"

and it is well written.

I have the little error, but I can't fix it. When I ran the command python3 Final_Fast_Version_Waqar.py ~/home/Data/DCIX_OB.csv 1000 7, I got

  File "Final_Fast_Version_Waqar.py", line 37
    import ipdb; ipdb.set_trace()
                                ^
TabError: inconsistent use of tabs and spaces in indentation

It is a problem related to the vim editor I guess because I set up an Ubuntu server and installed vim very recently. How can I fix the indentation error?

user3483203
  • 50,081
  • 9
  • 65
  • 94
Jeremie
  • 405
  • 1
  • 7
  • 20
  • Indentation should be four spaces, i think you are using 8 spaces in your tab – Latika Agarwal May 16 '18 at 02:31
  • `import ipdb; ipdb.set_trace()`, the `;` is suspicious, it is perl syntax – Gang May 16 '18 at 02:51
  • 1
    @Gang It's not, it's valid Python syntax. – phd May 16 '18 at 14:49
  • https://stackoverflow.com/questions/48735671/use-vim-retab-to-solve-taberror-inconsistent-use-of-tabs-and-spaces-in-indentat – phd May 16 '18 at 14:51
  • The semi-colon is valid, but not appropriate outside of short snippets intended for `exec` or command-line one-liners. (I.e, don't use it in a script.) – chepner May 16 '18 at 19:16
  • @chepner that single line is what is provided in the [python docs](https://docs.python.org/3/library/pdb.html) as the example of how to use `pdb`. I assume this recommendation is in here to allow for easy, obvious cleanup and "turning on/off" the debugger with comments when using `pdb` or `ipdb`. – SCB May 16 '18 at 23:26

2 Answers2

9

Since Python is sensitive to indentation, it is desirable to configure your editor to use spaces instead of tabs. For vim, you can add this to your .vimrc

set tabstop=4

Every time you type tab, vim will use 4 spaces instead.

Also, to fix the indentation of your current file, type:

:retab
Jed Cua
  • 251
  • 2
  • 11
6

It might be helpful to turn on white-space characters in vim to see what is happening.

:set listchars=eol:¬,tab:>·,trail:~,extends:>,precedes:<,space:·

What you'll probably see is a mix where some lines use tabs () and others use spaces (··), something that's not allowed in python.

I'd recommend setting up your vim config so that it automatically switches tabs to spaces in python. Maybe something like?

autocmd Filetype python setlocal ts=4 sw=4 sts=0 expandtab
SCB
  • 5,821
  • 1
  • 34
  • 43