ubuntu linux上的python:json.decoder.jsondeCoderror:期望值:第2行第6列

ubuntu linux上的python:json.decoder.jsondeCoderror:期望值:第2行第6列,python,python-3.x,azure,ubuntu-16.04,azure-cli2,Python,Python 3.x,Azure,Ubuntu 16.04,Azure Cli2,当我在Ubuntu 16.04上运行python脚本时,我发现了以下错误 当我运行相同的代码,但不确定哪个软件包没有正确安装时,它在Windows上运行良好 import subprocess import json #one vnet and one subnet in the resourcegroup. def get_vnet_name(resourcegroup): get_vnet_command=["az","network","vnet","list","--resou

当我在Ubuntu 16.04上运行python脚本时,我发现了以下错误

当我运行相同的代码,但不确定哪个软件包没有正确安装时,它在Windows上运行良好

import subprocess
import json

#one vnet and one subnet in the resourcegroup.
def get_vnet_name(resourcegroup):
    get_vnet_command=["az","network","vnet","list","--resource-group",resourcegroup]
    get_vnet=subprocess.run(get_vnet_command, shell = True, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
    a=get_vnet.stdout.decode('utf-8')
    d=json.loads(a)
    for item in d:
        vname=item["name"]
        subnets=item["subnets"]
    for i in subnets:
        subnetname=i["name"]
    return vname,subnetname

def create_vm(vm_resourcegroup,vm_name, vm_image,vm_username, vm_passowrd,vm_vnet,vm_subnet, vm_size):
    create_vm_command=["az","vm","create","--resource-group",vm_resourcegroup,"--name",vm_name,"--image",vm_image, "--admin-username", vm_username,"--admin-password",vm_passowrd,"--vnet-name",vm_vnet,"--subnet",vm_subnet,"--size", vm_size]
    create_vm=subprocess.run(create_vm_command, shell = True, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
    return


if __name__=="__main__":
    rscgroup_name="vm-test-group"
    avm_name="testvm1"
    avm_image="Win2019Datacenter"
    avm_username="myuser"
    avm_password="mypassword"
    avm_size="Standard_D2_V3"
    vault_name = "aqrahkeyvault"
    certificate_name = "staticwebsite"

    avm_vnet,avm_subnet=get_vnet_name(rscgroup_name)
    create_vm(rscgroup_name,avm_name,avm_image,avm_username,avm_password,avm_vnet,avm_subnet,avm_size)
下面是与json.decoder相关的错误:

  root@linuxvm:/home/azureuser# python3.6  test2.py
    Traceback (most recent call last):
      File "test2.py", line 32, in <module>
        avm_vnet,avm_subnet=get_vnet_name(rscgroup_name)
      File "test2.py", line 9, in get_vnet_name
        d=json.loads(a)
      File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
        return _default_decoder.decode(s)
      File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
        raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 2 column 6 (char 6)

我尝试安装python,但没有解决问题。

我尝试重现您的问题,并成功找到原因。实际上,脚本基本上是正确的,但是对于AZ命令不返回任何结果到STDUT,您可能不会考虑。 例如,我的订阅中有一个不存在的资源组,例如不存在的rg。如果我将其作为参数-resource group的值传递,下面的脚本将向stderr返回错误信息,stdout值为b

stdout和stderr的结果如下所示

>>> get_vnet.stdout
b''
>>> get_vnet.stderr
b"ERROR: Resource group 'non-exist-rg' could not be found.\r\n"
因此,如果您将stdout值传递给json.loads函数,它将抛出与您相同的问题,json.decoder.JSONDecodeError:期望值:第1行第1列char 0,因为json.loads不能处理空内容。在这里,对于可以接收字节值的stdout或stderr的字节值,不需要解码函数,如下图所示

因此,要解决这个问题,解决方案是检查stdout值是否为空或stderr值是否为非空

def get_vnet_name(resourcegroup):
    get_vnet_command=["az","network","vnet","list","--resource-group",resourcegroup]
    get_vnet=subprocess.run(get_vnet_command, shell = True, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
    # decode for stdout is not necessary
    # a=get_vnet.stdout.decode('utf-8')
    vname,subnetname = '', ''
    if get_vnet.stdout == b'':
        d=json.loads(get_vnet.stdout)
        for item in d:
            vname=item["name"]
            subnets=item["subnets"]
        for i in subnets:
            subnetname=i["name"]
    return vname,subnetname
然后,在调用create_vm方法之前,需要检查vname和subnetname的值


希望能有所帮助。

您是否安装了所有必要的模块?显然,您没有安装apt_pkg?为什么使用az cli而不是Azure Python SDK?运行此代码之前是否对cli进行了身份验证?不会返回任何内容,因为您需要进行身份验证。您很可能已在上面的代码段中公开了您的凭据用户和密码。确保移除它们!谢谢mc51,我忘了取下它了。嗨,Gleb,在使用az登录运行cmd之前,我对azure cli进行了身份验证。然后运行python脚本。我安装了apt_pkg两次。不确定它是否仍然会给我同样的错误,而且我需要运行cli代码,因为我团队中的大多数工程师都熟悉azure cli。另外,使用azure cli和python包装也适用于他们感谢peter进行回归测试,我还通过删除shell=True解决了同样的问题,我不确定shell=True有什么问题,但在Linux上它会产生问题。再次感谢。我把这个标记为答案。
def get_vnet_name(resourcegroup):
    get_vnet_command=["az","network","vnet","list","--resource-group",resourcegroup]
    get_vnet=subprocess.run(get_vnet_command, shell = True, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
    # decode for stdout is not necessary
    # a=get_vnet.stdout.decode('utf-8')
    vname,subnetname = '', ''
    if get_vnet.stdout == b'':
        d=json.loads(get_vnet.stdout)
        for item in d:
            vname=item["name"]
            subnets=item["subnets"]
        for i in subnets:
            subnetname=i["name"]
    return vname,subnetname