2017-03-22 10 views
0

루프에서 python3.4 클래스 메소드를 호출하려고합니다. 하지만 "TabError: inconsistent use of tabs and spaces in indentation""for 루프"(pyvmomi)에서 python3.4 들여 쓰기 오류

나는 "vim"과 "gedit/sublime"모두에서 코드를 보았고 명백한 오류는 보이지 않았습니다.

들여 쓰기가 아닌 다른 오류가 있습니까?

감사합니다.

$ cat pyvmomi-loop.py 
from __future__ import (absolute_import, division, 
         print_function, unicode_literals) 
from builtins import * 
import atexit 
import sys 
sys.path.insert(0, '/usr/lib/python2.7/site-packages') 
import pyVmomi 
import argparse 
import atexit 
import itertools 
from pyVim.connect import SmartConnect, Disconnect 
import humanize 
from pyVim import connect 
from pyVmomi import vmodl 
from pyVmomi import vim 

## cred 
host="1.2.3.4" 
user="aa\bb" 
password="[email protected]" 
port=443 

## Ignore certificate error 
import ssl 
try: 
    _create_unverified_https_context = ssl._create_unverified_context 
except AttributeError: 
    pass 
else: 
    print ("Ignoring SSL Error") 
    ssl._create_default_https_context = _create_unverified_https_context 

## Fetch id an dpw 

def GetArgs(): 

    parser = argparse.ArgumentParser(
     description='Process args for retrieving all the Virtual Machines') 
    parser.add_argument('-s', '--host', required=True, action='store', 
         help='Remote host to connect to') 
    parser.add_argument('-o', '--port', type=int, default=443, action='store', 
         help='Port to connect on') 
    parser.add_argument('-u', '--user', required=True, action='store', 
         help='User name to use when connecting to host') 
    parser.add_argument('-p', '--password', required=False, action='store', 
         help='Password to use when connecting to host') 
    args = parser.parse_args() 
    return args 


## Method to fetch VM info 

def printHost(host): 

    try: 
     summary = host.summary 
     stats = summary.quickStats 
     hardware = host.hardware 
     cpuUsage = stats.overallCpuUsage 
     memoryCapacity = hardware.memorySize 
     memoryCapacityInMB = hardware.memorySize/MBFACTOR 
     memoryUsage = stats.overallMemoryUsage 
     freeMemoryPercentage = 100 - (
      (float(memoryUsage)/memoryCapacityInMB) * 100 
     ) 
     print ("--------------------------------------------------") 
     print ("Host name: ", host.name) 
     print ("Host CPU usage: ", cpuUsage) 
     print ("Host memory capacity: ", humanize.naturalsize(memoryCapacity, 
                  binary=True)) 
     print ("Host memory usage: ", memoryUsage/1024, "GiB") 
     print ("Free memory percentage: " + str(freeMemoryPercentage) + "%") 
     print ("--------------------------------------------------") 
    except Exception as error: 
     print ("Unable to access information for host: ", host.name) 
     print (error) 
     pass 



## Main method 

def main(): 

    # argsCred = GetArgs() 

    try: 
     service_instance = connect.SmartConnect(host=host, 
               user=user, 
               pwd=password, 
               port=port, 
               ) 

## What to do when exiting 
     atexit.register(connect.Disconnect, service_instance) 

##Content object 
     content = service_instance.RetrieveContent() 

     container = content.rootFolder # starting point to look into 
     viewType = [vim.VirtualMachine] # object types to look for 
     recursive = True # whether we should look into it recursively 

## Create a view 
     containerView = content.viewManager.CreateContainerView(
      container, viewType, recursive) 

## Loop through all obhects to return name and VMware tools version 
     children = containerView.view 
     for child in children: 
      if child.summary.guest is not None: 
       try: 
        tools_version = child.summary.guest.toolsStatus 
          osFam = child.summary.guest.guestId 
        print("VM: {}, VMware-tools: {}".format(child.name, tools_version)) 
         print("VM: {}, OS Fam: {}".format(child.name, osFam)) 
       except: 
        print("Vmware-tools: None") 




    except vmodl.MethodFault as error: 
     print("Caught vmodl fault : " + error.msg) 
     return -1 

    return 0 

## Start program 
if __name__ == "__main__": 
    main() 

답변

0

이 코드에는 들여 쓰기 문제가 있습니다.

변경

for child in children: if child.summary.guest is not None: try: tools_version = child.summary.guest.toolsStatus osFam = child.summary.guest.guestId print("VM: {}, VMware-tools: {}".format(child.name, tools_version)) print("VM: {}, OS Fam: {}".format(child.name, osFam)) except: print("Vmware-tools: None")

그 오류는 라인 (114)에있다, 116 가있을 수 있기 때문에이 코드를 붙여 복사하지 마십시오, 전체 평 파일 인 경우 for child in children: if child.summary.guest is not None: try: tools_version = child.summary.guest.toolsStatus osFam = child.summary.guest.guestId print("VM: {}, VMware-tools: {}".format(child.name, tools_version)) print("VM: {}, OS Fam: {}".format(child.name, osFam)) except: print("Vmware-tools: None")

으로 너무 많은 공백 내가 언급 한 줄에서 코드를 들여 쓰기 만하면됩니다.

편집 : vim이나 gedit 또는 다른 텍스트 편집기에서 코드를 작성하는 것은 좋지 않습니다. Pycharm을보세요 :

+0

전체 py 파일이었습니다. 이 장소에서 들여 쓰기가 잘못 될 수있는 다른 들여 쓰기를 보시겠습니까? 감사! – SndLt

+0

정력에 대한 당신의 의견은 나쁜 생각 일뿐입니다. 관련이 없으므로 제거해야합니다. –

+0

방금 ​​pycharm 있어요. 이것이 많은 도움이 될 것이라고 확신합니다. 고마워요! – SndLt