Home > Back-end >  IndentationError: unexpected indent - python script
IndentationError: unexpected indent - python script

Time:03-02

I have issue when executing my python script on my linux server, can someone know what is wrong with my script?

def construct_service(api_service):
    CLIENT_SERVICE_FILE = "mygcpkey.json"
    try:
        if api_service == 'drive':
            API_NAME = 'drive'
            API_VERSION = 'v3'
            SCOPES = ['https://www.googleapis.com/auth/drive']
            return Create_Service(CLIENT_SERVICE_FILE, API_NAME, API_VERSION, SCOPES)
         elif api_service == 'gmail':
         API_NAME = 'gmail'
         API_VERSION = 'v1'
         SCOPES = ['https://mail.google.com/']
         return Create_Service(CLIENT_SERVICE_FILE, API_NAME, API_VERSION, SCOPES)

Error

  File "bgmail.py", line 14
    elif api_service == 'gmail':
    ^
IndentationError: unexpected indent

CodePudding user response:

It's hard to say without having the "exact" code. In copy-paste you might have changed a tab to 4 spaces or vice versa and the rest of the code has the other. That's what I typically have when I've copied something from the web and they use a different whitespace setup than I (I know that PEP8 suggests 4 spaces of indentation, but I prefer tab that render as 2 spaces in VSCode, for example). That is something that you might have ran into.

And actually, this should look like this

         elif api_service == 'gmail':
             #Here you also need to indent...
             API_NAME = 'gmail'
             API_VERSION = 'v1'
             SCOPES = ['https://mail.google.com/']
             return Create_Service(CLIENT_SERVICE_FILE, API_NAME, API_VERSION, SCOPES)

CodePudding user response:

I think this will work:

def construct_service(api_service):
    CLIENT_SERVICE_FILE = "mygcpkey.json"
    try:
        if api_service == 'drive':
            API_NAME = 'drive'
            API_VERSION = 'v3'
            SCOPES = ['https://www.googleapis.com/auth/drive']
            return Create_Service(CLIENT_SERVICE_FILE, API_NAME, API_VERSION, SCOPES)
         elif api_service == 'gmail':
             API_NAME = 'gmail'
             API_VERSION = 'v1'
             SCOPES = ['https://mail.google.com/']
             return Create_Service(CLIENT_SERVICE_FILE, API_NAME, API_VERSION, SCOPES)

CodePudding user response:

If you have PyCharm editor then you can select your code and use code->Reformat code option. Code in your file will be arranged according to code standards.

  • Related