ipython笔记本argparse错误
如果我从命令行运行此代码,它可以正常工作ipython笔记本argparse错误,python,python-3.x,python-2.7,jupyter-notebook,argparse,Python,Python 3.x,Python 2.7,Jupyter Notebook,Argparse,如果我从命令行运行此代码,它可以正常工作 import argparse import sys from googleapiclient import sample_tools from pprint import pprint def execute_request(service, property_uri, request): """Executes a searchAnalytics.query request. Args: service: The we
import argparse
import sys
from googleapiclient import sample_tools
from pprint import pprint
def execute_request(service, property_uri, request):
"""Executes a searchAnalytics.query request.
Args:
service: The webmasters service to use when executing the query.
property_uri: The site or app URI to request data for.
request: The request to be executed.
Returns:
An array of response rows.
"""
return service.searchanalytics().query(
siteUrl=property_uri, body=request).execute()
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument('property_uri', type=str,
help=('Site or app URI to query data for (including '
'trailing slash).'),
action='store_true')
argparser.add_argument('start_date', type=str,
help=('Start date of the requested date range in '
'YYYY-MM-DD format.'),
action='store_true')
argparser.add_argument('end_date', type=str,
help=('End date of the requested date range in '
'YYYY-MM-DD format.'),
action='store_true')
service, flags = sample_tools.init(
sys.argv, 'webmasters', 'v3', __doc__, 'client_secrets.json', parents=[argparser],
scope='https://www.googleapis.com/auth/webmasters.readonly')
# First run a query to learn which dates we have data for. You should always
# check which days in a date range have data before running your main query.
# This query shows data for the entire range, grouped and sorted by day,
# descending; any days without data will be missing from the results.
request = {
'startDate': flags.start_date,
'endDate': flags.end_date,
'dimensions': ['date']
}
response = execute_request(service, flags.property_uri, request)
pprint(response)
如果我从ipython笔记本运行它,我会得到以下错误:
ArgumentParser(prog='-c',用法=None,描述=None,版本=None,
格式化程序_类=,
冲突\u handler='error',add\u help=False)
用法:-c[-h][--auth\u host\u name auth\u host\u name]
[--noauth_local_webserver]
[--auth_host_port[auth_host_port[auth_host_port…]]
[--logging_level{DEBUG,INFO,WARNING,ERROR,CRITICAL}]
属性\u uri开始\u日期结束\u日期
-错误:参数太少
发生异常,请使用%tb查看完整回溯
正如错误消息所说,您提供的参数太少。你可以加上
import sys
sys.argv = ['scriptname.py', 'argument1', ...]
在脚本的顶部,使其在Jupyter中运行。在命令行版本中使用任何参数。您如何从命令行调用此参数?您必须传递任何参数吗?请查看
sys.argv
。错误用法看起来是由jupyter服务器产生的,而不是由解析器产生的。以前有人问过这个问题,我不认为你可以在笔记本上输入命令行