argparse

Jupyter notebook 上无法使用argparse.parse_args()

前提是你 提交于 2020-03-05 11:29:53
本博客讲述了在 jupyter notebook 上 argparse.parse_args() 无法正确运行的解决办法 问题描述 argparse 是我比较习惯的命令行解析程序,之前一直在pycharm 中用(教程请见本人另外一篇 博客 ),今天在 jupyter notebook 中使用的时候发现报错了! 报错代码: import argparse import os import random import numpy as np import torch import torch . backends . cudnn as cudnn arg = argparse . ArgumentParser ( ) arg . add_argument ( "--workers" , type = int , help = "数据加载线程的个数,默认8 线程" , default = 4 ) arg . add_argument ( "--batchSize" , type = int , help = "输入批数据的大小,默认200" , default = 200 ) arg . add_argument ( "--lr" , type = float , default = 0.0002 , help = "学习率,默认大小是 0.0002" ) arg . add

Python学习笔记|Python之Argparse

浪子不回头ぞ 提交于 2020-03-04 19:48:42
基本用法 import argparse parser = argparse . ArgumentParser ( ) parser . parse_args ( ) 结果为: $ python prog . py - - help usage : prog . py [ - h ] optional arguments : - h , - - help show this help message and exit 位置参数 用法1 import argparse parser = argparse . ArgumentParser ( ) parser . add_argument ( "echo" ) args = parser . parse_args ( ) print args . echo 结果 $ python prog . py - - help usage : prog . py [ - h ] echo positional arguments : echo optional arguments : - h , - - help show this help message and exit 用法2 import argparse parser = argparse . ArgumentParser ( ) parser . add_argument (

python中的argparse模块(参数解析) --和没有--有什么区别

一曲冷凌霜 提交于 2020-03-03 07:28:49
引入 在做项目的时候,发现一个问题: 针对下面的这句一直理解不了: parser.add_argument("--x", help="横坐标", type=int) 这边带个--和没有--到底有什么区别,各种查找,也没发现我想要的答案。 后面再一个犄角旮旯的地方发现下面这个博文: 示例代码 import argparse parse = argparse.ArgumentParser() parse.add_argument("a", help="params means") parse.add_argument("-C", "--gc", default="count") parse.add_argument("--ga", help="params means ga",dest='simple_value',choices=['A', 'B', 'C', 0]) parse.add_argument("--gb", help="params means gb",action="store_const",const='value-to-store') args = parse.parse_args() print args.simple_value,args.gb,args.gc 解释 ### add_argument 说明 不带'--'的参数 调用脚本时必须输入值

How to access a python argparse argument with a dot in the name

孤人 提交于 2020-02-16 05:42:32
问题 Python's argparse lets me define argument names containing a dot in the name. But how can I access these ? import argparse parser = argparse.ArgumentParser() parser.add_argument("inputfile.txt") parser.add_argument("outputfile.txt") args = parser.parse_args(['hello', 'world']) # now args is: # Namespace(inputfile.txt='hello', outputfile.txt='world') # and this does not work print(args.inputfile.txt) >>> AttributeError: 'Namespace' object has no attribute 'inputfile' Obviously attribute names

How to access a python argparse argument with a dot in the name

好久不见. 提交于 2020-02-16 05:42:21
问题 Python's argparse lets me define argument names containing a dot in the name. But how can I access these ? import argparse parser = argparse.ArgumentParser() parser.add_argument("inputfile.txt") parser.add_argument("outputfile.txt") args = parser.parse_args(['hello', 'world']) # now args is: # Namespace(inputfile.txt='hello', outputfile.txt='world') # and this does not work print(args.inputfile.txt) >>> AttributeError: 'Namespace' object has no attribute 'inputfile' Obviously attribute names

Passing multiple arguments via command line in R

笑着哭i 提交于 2020-02-14 13:20:41
问题 I am trying to pass multiple file path arguments via command line to an Rscript which can then be processed using an arguments parser. Ultimately I would want something like this Rscript test.R --inputfiles fileA.txt fileB.txt fileC.txt --printvar yes --size 10 --anotheroption helloworld -- etc... passed through the command line and have the result as an array in R when parsed args$inputfiles = "fileA.txt", "fileB.txt", "fileC.txt" I have tried several parsers including optparse and getopt

Python_argparse.Argumentparser()

纵饮孤独 提交于 2020-02-13 17:29:37
argparse模块简介 官方文档 argparse是一个Python模块:命令行选项、参数和子命令解析器。 argparse模块可以让人轻松编写用户友好的命令行接口。程序定义它需要的参数,然后argparse将弄清楚如何从sys.argv解析出那些参数。argparse模块还会自动生成帮助和使用手册,并在用户给程序传入无效参数时报出错误信息。 argparse使用流程 1. 创建解析器 parser = argparse . ArgumentParser ( description = 'Process some integers.' ) 使用argparse的第一步是创建一个ArgumentParser对象。 ArgumentParser对象包含将命令行解析成Python数据类型所需的全部信息。 2.添加参数 parser . add_argument ( 'integers' , metavar = 'N' , type = int , nargs = '+' , help = 'an integer for the accumulator' ) 给一个ArgumentParser添加程序参数信息是通过调用add_argument()方法完成的。 3.解析参数 >> > parser . parse_args ( [ '--sum' , '7' , '-1' , '42' ]

Python参数解析模块sys、getopt、argparse使用与对比分析

人盡茶涼 提交于 2020-02-12 02:32:01
今天小编就为大家分享一篇关于Python参数解析模块sys、getopt、argparse使用与对比分析,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧 一些命令行工具的使用能够大大简化代码脚本的维护成本,提升复用性,今天主要是借助于python提供的几种主流的参数解析工具来实现简单的功能,主要是学习实践为主,这是新年伊始开工的第一篇,还是花了一番功夫来完成写作的和实验的,希望能够帮到需要的朋友们,新的一年里,祝大家心想事成! 好了,废话不多说,下面进入正文。 Python中有三个内建的模块用于处理命令行参数: 第一个:sys,最简单,只能够提供简单的参数解析功能 第二个:getopt,只能简单的处理命令行参数 ,较sys封装更好一点 第三个:argparse,使其更加容易的编写用户友好的命令行接口。它所需的程序进程了参数定义,argparse将更好的解析 sys.argv。同时argparse模块还能自动生成帮助及用户输入错误参数时的提示信息。 在命令行参数中分为“-”和“–”两种模式,具体的使用方法以及与异同点我都会在下面的实际使用中介绍到,主要是正确完成对两种命令参数模式的区分就行了。接下来的实践中,首先以sys模块为例,来观察该模块的参数解析过程,具体实践如下: def sysFunc(): ''' 基于 sys

Python (3.6.3) argparse: default value of optional parameter to be another parameter's value

旧城冷巷雨未停 提交于 2020-02-06 07:58:50
问题 I have a function that takes as parameters an input folder (required) and output folder (optional), but I want the default value of the (optional) output folder to be the input folder. I can do this of course using, e.g. p = argparse.ArgumentParser(description="blah") p.add_argument('inpath', type=str, help="Path to input") p.add_argument('--outpath', required=False, type=str, help="Path to output") argin = p.parse_args() if argin.outpath is None: argin.outpath = argin.inpath but I want to

Argparse: mixing parent parser with subparsers

馋奶兔 提交于 2020-02-05 03:23:51
问题 I want to write a simple tool that takes an arbitrary number of input files and performs one operation on each of them. The syntax is stupidly simple: mytool operation input1 input2 ... inputN Some of these operations may require an extra argument mytool operation op_argument input1 input2 ... inputN In addition to this I'd like the users to be able to specify whether the operations should be performed in place, and to specify the target directory of the output. mytool -t $TARGET --in-place