我对以下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
答案 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>
传递它。