jupyter notebook错误: ipykernel_launcher.py: error: no such option: -f

在jupyter notebook中遇到使用optparse库时出现`ipykernel_launcher.py: error: no such option: -f`的错误。问题源于命令行选项与jupyter内核启动参数冲突。解决方案是在调用optparse时避免使用`-f`标志,以防止与内建选项冲突。

jupyter notebook中使用 optparse 库
结论:使用 optparser.parse_args() 时要加入
optparser.add_option(’-f’,’–file’, default=‘hello you need me’, help=“hello you need me”)
源代码:
首先是可以正常使用的代码
···
import optparse
optparser1 = optparse.OptionParser()
optparser1.add_option(’-f’,’–file’, default=‘hello you need me’, help=“hello you need me”)
optparser1.add_option("-w","–write", default=‘1’, type=“int”, help=“write”)
option , args = optparser1.parse_args()
print("option : ",option)
print("args : ",args)
···
output:

option : {‘file’: ‘C:\Users\xx\xxjson’, ‘write’: 1}
args : []

然后是正常传参的代码

import optparse
optparser2 = optparse.OptionParser()
optparser2.add_option("-w","--write", default='1', type="int", help="world")
fakeArgs = ['-w','1', 'arg1', 'arg2']
option , args = optparser2.parse_args(fakeArgs)
print("option : ",option)
print("args : ",args)

output:

option : {‘write’: 1}
args : [‘arg1’, ‘arg2’]

最后是出现错误的代码
import optparse
optparser3 = optparse.OptionParser()
optparser3.add_option("-w","--write", default='1', type="int", help="world")
option , args = optparser3.parse_args()
print("option : ",option)
print("args : ",args)

output:

Usage: ipykernel_launcher.py [options]
ipykernel_launcher.py: error: no such option: -f
ERROR:root:Internal Python error in the inspect module.
Below is the traceback from this internal error.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值