带参数的python设置工具console_scripts [英] python setup tools console_scripts with arguments

查看:79
本文介绍了带参数的python设置工具console_scripts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在命令行上实现我的 pypy.py 脚本,我需要使用设置工具,但是由于我的 pypy.py 需要两个参数,console_script 无法正常工作,请指导我如何正确修改它以继续工作推荐行.

I want to implement my pypy.py script on commandline, I need to work with setup tools but the console_script does not work properly as my pypy.py needs two arguments, please guide me how can I modify it properly to work on commendline.

python.py

def main(agru1, argu2):

    "do something"

 if __name__ == "__main__":
        main()

当我将它包含在我的 setup.py 文件中时,作为 console_script 如下

when I include it in my setup.py file, as console_script as follow

setup( 
     entry_points={
        'console_scripts': ['pypy = pypy.pypy:main'],
    }

)

当我在命令行上运行它时出现以下错误:

And I get the following error when I run it on commandline:

Traceback (most recent call last):
File "/usr/local/bin/python", line 9, in <module>
load_entry_point('Pypy==0.1', 'console_scripts', 'pypy')()
TypeError: main() takes at least 2 arguments (0 given)

推荐答案

入口点必须是一个接受参数的函数.如果您想从命令行传入参数,请说您想像这样调用它:

The entry point must be a function that takes zero arguments. If you want to pass in arguments from the command line, say you want to invoke it like:

$ pypy a1 a2

您需要从 sys.argv 读取它们 代替.所以你的 python 模块应该包含这个:

You need to read them from sys.argv instead. So your python module should contain this:

def program(arg1, arg2):
    print(arg1, arg2)

def main():
    import sys
    arg1, arg2 = sys.argv[1], sys.argv[2]
    program(arg1, arg2)

if __name__ == "__main__":
    main()

按上述方式运行该命令应将 a1 a2 打印到控制台中.对用户输入的错误处理是您自己的练习.

Running that command as above should print out a1 a2 into the console. Error handling on user input is your own exercise.

这篇关于带参数的python设置工具console_scripts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆