image_url = sys.argv [1] IndexError:列表索引超出范围

时间:2019-01-14 19:28:49

标签: python firebase google-cloud-firestore

我对以下Python代码部分有疑问:

import sys  
import requests  
import firebase_admin  
from firebase_admin import credentials  
from firebase_admin import storage  
image_url = sys.argv[1]  
cred = credentials.Certificate('certificate.json')  
firebase_admin.initialize_app(cred,{'storageBucket':'gs://rpi-demo-6d21d.appspot.com'})  
bucket = storage.bucket()  
image_data = requests.get(image_url).content  
blob = bucket.blob('happy.jpg')  
blob.upload_from_string(image_data,content_type='image/jpg')  
print(blob.public_url)

具体错误如下:

Traceback (most recent call last):
File "demo4.py", line 6, in <module>
image_url = sys.argv[1]
IndexError: list index out of range

1 个答案:

答案 0 :(得分:0)

sys.argv列表是通过传递给脚本的命令行参数填充的,因此,如果未提供任何参数,则可能会收到类似

的错误
Traceback (most recent call last):
File "demo4.py", line 6, in <module>
image_url = sys.argv[1]
IndexError: list index out of range

简单的复制就是

import sys
print(sys.argv[1])
demo.py

并使用python demo.py运行它。您将获得输出

$ python demo.py
Traceback (most recent call last):
  File "demo.py", line 2, in <module>
    print(sys.argv[1])
IndexError: list index out of range

但是如果您使用python demo.py foo向其传递参数,则会得到所需的输出:

$ python demo.py foo
foo

特别是对于您的示例,在调用demo4.py脚本时,您似乎没有传递图像url命令行参数。您可以使用python demo4.py <image_url_goes_here>传递它。