Execute an installed Python package as a script?

后端 未结 3 1470
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 23:48

Is there a way to enable a package to be executed as a script? For example:

[~]# easy_install /path/to/foo.egg
...
[~]# python -m foo --name World
Hello Worl         


        
3条回答
  •  佛祖请我去吃肉
    2020-12-05 00:40

    I think this may be a limitation of Python 2.6. I've tested it, and executing a package (either in . or installed from an egg with easy_install) with the -m option works fine in 2.7, but not in 2.6. For example, on my system (Ubuntu) with a test package called pkg_exec in the current directory, and where __main__.py simply prints sys.argv:

    xx@xx:~/tmp/pkg_exec$ python2.6 -m pkg_exec
    /usr/bin/python2.6: pkg_exec is a package and cannot be directly executed
    xx@xx:~/tmp/pkg_exec$ python2.7 -m pkg_exec
    ['/home/xx/tmp/pkg_exec/pkg_exec/__main__.py']
    

    Also, according to the 2.7 docs:

    Changed in version 2.7: Supply the package name to run a __main__ submodule.

提交回复
热议问题