刘克兰 發表於 2019-8-7 14:45:00

运行python脚本时传入参数的几种方式

<p><strong>如果在运行python脚本时需要传入一些参数,例如<code>gpus</code>与<code>batch_size</code>,可以使用如下三种方式。</strong></p>
<div class="cnblogs_code">
<pre>python script.py 0,1,2 10<span style="color: rgba(0, 0, 0, 1)">
python script.py </span>-gpus=0,1,2 --batch-size=10<span style="color: rgba(0, 0, 0, 1)">
python script.py </span>-gpus=0,1,2 --batch_size=10</pre>
</div>
<p>这三种格式对应不同的参数解析方式,分别为<strong><code>sys.argv</code>、<code>argparse、</code>&nbsp;<code>tf.app.run,</code></strong>&nbsp;前两者是python自带的功能,后者是<code>tensorflow</code>提供的便捷方式。</p>
<p><span style="font-size: 16px"><strong>1.sys.argv</strong></span></p>
<div>
<div><code>sys</code>模块是很常用的模块, 它封装了与python解释器相关的数据,例如<code>sys.modules</code>里面有已经加载了的所有模块信息,<code>sys.path</code>里面是<code>PYTHONPATH</code>的内容,而<code>sys.argv</code>则封装了传入的参数数据。<br>
使用<code>sys.argv</code>接收上面第一个命令中包含的参数方式如下:</div>
<div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> sys
gpus </span>= sys.argv
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">gpus = </span>
batch_size = sys.argv
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(gpus)
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(batch_size)</pre>
</div>
<p><img src="https://img2018.cnblogs.com/blog/1620624/201908/1620624-20190807142116570-1962942826.png"></p>
<p><span style="font-size: 16px"><strong>2.argparse</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> argparse
parser </span>= argparse.ArgumentParser(description=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">manual to this script</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
parser.add_argument(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">--gpus</span><span style="color: rgba(128, 0, 0, 1)">"</span>, type=str, default=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">0</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
parser.add_argument(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">--batch-size</span><span style="color: rgba(128, 0, 0, 1)">"</span>, type=int, default=32<span style="color: rgba(0, 0, 0, 1)">)
args </span>=<span style="color: rgba(0, 0, 0, 1)"> parser.parse_args()
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(args.gpus)
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(args.batch_size)</pre>
</div>
<div>
<div>需要注意的是,脚本运行命令<code>python script.py -gpus=0,1,2 --batch-size=10</code>中的<code>--batch-size</code>会被自动解析成<code>batch_size</code>.<br>
<code>parser.add_argument</code> 方法的<code>type</code>参数理论上可以是任何合法的类型, 但有些参数传入格式比较麻烦,例如list,所以一般使用<code>bool</code>, <code>int</code>, <code>str</code>, <code>float</code>这些基本类型就行了,更复杂的需求可以通过<code>str</code>传入,然后手动解析。<code>bool</code>类型的解析比较特殊,传入任何值都会被解析成<code>True</code>,传入空值时才为<code>False</code></div>


</div>
<p><img src="https://img2018.cnblogs.com/blog/1620624/201908/1620624-20190807143656564-2040744979.png"></p>
<p><span style="font-size: 16px"><strong>3.tf.app.run</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> tensorflow as tf
tf.app.flags.DEFINE_string(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">gpus</span><span style="color: rgba(128, 0, 0, 1)">'</span>, None, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">gpus to use</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
tf.app.flags.DEFINE_integer(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">batch_size</span><span style="color: rgba(128, 0, 0, 1)">'</span>, 5, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">batch size</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)

FLAGS </span>=<span style="color: rgba(0, 0, 0, 1)"> tf.app.flags.FLAGS

</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> main(_):
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(FLAGS.gpus)
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(FLAGS.batch_size)

</span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span>==<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">:
    tf.app.run()</span></pre>
</div>
<div>
<div>
<p>有几点需要注意:</p>
<p><code>tensorflow</code>只提供以下几种方法:</p>
<p><code>tf.app.flags.DEFINE_string</code>,<em id="__mceDel"><br>
</em><code>tf.app.flags.DEFINE_integer</code>,<em id="__mceDel"><br>
</em><code>tf.app.flags.DEFINE_boolean</code>,<em id="__mceDel"><br>
</em><code>tf.app.flags.DEFINE_float</code> 四种方法,分别对应<code>str</code>, <code>int</code>,<code>bool</code>,<code>float</code>类型的参数。这里对<code>bool</code>的解析比较严格,传入1会被解析成<code>True</code>,其余任何值都会被解析成<code>False</code>。</p>
<p>脚本中需要定义一个接收一个参数的<code>main</code>方法:<code>def main(_):</code>,这个传入的参数是脚本名,一般用不到, 所以用下划线接收。</p>
<p>以<code>batch_size</code>参数为例,传入这个参数时使用的名称为<code>--batch_size</code>,也就是说,中划线不会像在<code>argparse</code> 中一样被解析成下划线。</p>
<p><code>tf.app.run()</code>会寻找并执行入口脚本的<code>main</code>方法。也只有在执行了<code>tf.app.run()</code>之后才能从<code>FLAGS</code>中取出参数。<br>
从它的签名来看,它也是可以自己指定需要执行的方法的,不一定非得叫<code>main</code>:</p>
<p><img src="https://img2018.cnblogs.com/blog/1620624/201908/1620624-20190807144501890-363436685.png"></p>

</div>

</div>



</div>



</div><br><br>
来源:https://www.cnblogs.com/answerThe/p/11315185.html
頁: [1]
查看完整版本: 运行python脚本时传入参数的几种方式