【问题标题】:Unable to pass parameters to pythonscript from ant build.xml file. How can i pass the value?无法从 ant build.xml 文件向 pythonscript 传递参数。我怎样才能传递价值?
【发布时间】:2021-01-01 23:10:42
【问题描述】:

我在运行 build.xml 文件时尝试从我的 ant build.xml 文件运行 python 程序,但我无法将参数传递给 python 脚本

检查给定数的阶乘的 Python 程序:

fact.py

#!/usr/bin/python

def factorial(num):
    if num == 1:
        return num
    else:
        return num * factorial(num - 1)

 num = int(input('Enter a Number: '))
 if num < 0:
     print 'Factorial cannot be found for negative numbers'
 elif num == 0:
     print 'Factorial of 0 is 1'
 else:
     print ('Factorial of', num, 'is: ', factorial(num))

build.xml 文件如下所示:

<project name="ant_test" default="python" basedir=".">
<target name="python" >
        
        <exec dir="D:\app\python" executable="D:\app\python\python.exe" failonerror="true">
            <arg line="D:\app\ant-workout\fact.py/>
        </exec>
</target>

当运行 build.xml 文件时,它运行 fact.py python 程序,它期望用户输入来检查给定数字的阶乘。

如何将数字从 ant build.xml 文件传递​​给 python 程序

提前致谢!!!!!

【问题讨论】:

    标签: python xml build ant


    【解决方案1】:

    根据documentation

    请注意,您无法与分叉程序进行交互,唯一的方式 向它发送 输入 是通过 输入inputstring 属性。另请注意,从 Ant 1.6 开始,任何在分叉程序中读取输入的尝试都将收到 EOF (-1)。这是对 Ant 1.5 的更改,在 Ant 1.5 中,这样的尝试会被阻止。
    (强调我的)

    那你能做什么?提供以下任一项:

    输入
    从中获取执行命令的标准输入的文件。该属性与 inputstring 属性互斥。

    输入字符串
    一个字符串,用作执行命令的输入流。该属性与输入属性互斥。


    例子:

    <project name="ant_test" default="python" basedir=".">
    <target name="python" >
            
            <exec dir="D:\app\python" executable="D:\app\python\python.exe" 
                                      failonerror="true" 
                                      inputstring="42">
                <arg line="D:\app\ant-workout\fact.py/>
            </exec>
    </target>
    

    【讨论】:

    • 如果我需要传递多个输入,我该怎么做?
    • @john 试试input=... - 我的猜测是您可以将每行 1 个输入放入文件中,每个输入将用于一个pythonic 'input()'陈述。您可能最好重新设计您的 pyhton 脚本以接受命令行参数,如果提供这些参数,请使用它们而不是“交互式用户输入模式”。 这只是猜测
    猜你喜欢
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 2014-08-13
    • 2013-11-30
    • 1970-01-01
    • 2022-08-19
    相关资源
    最近更新 更多