Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使顶级python模块可调用?_Python - Fatal编程技术网

使顶级python模块可调用?

使顶级python模块可调用?,python,Python,当从命令行调用顶级python包时,是否可以执行操作 例如,我当前的结构如下所示: MyApp | - subdir1 | - pyfile.py | - pyfile.py | - pyfile.py | - subdir2 | - subsubdir1 | - pyfile.py | - pyfile.py - __init__ - other

当从命令行调用顶级python包时,是否可以执行操作

例如,我当前的结构如下所示:

MyApp 
    | - subdir1 
        | - pyfile.py
        | - pyfile.py
        | - pyfile.py
    | - subdir2
        | - subsubdir1
            | - pyfile.py
            | - pyfile.py
    - __init__ 
    - other.py 
我可以做所有正常的事情。e、 g.
从MyApp.subdir1导入pyfile

但是,我想允许用户从命令行通过其顶级名称调用应用程序,并让它做一些事情

python MyApp [options]
而不是他们必须使用
main()
func()将其范围限定到特定模块

python /path/to/MyApp/actions/somemodule.py [options]

这有可能吗

尝试
python-MyApp[options]
,其中您的
\uuuu init\uuuuuuuuuuuuupy
包含一个
if uuuuuuuuu name\uuuuuuuuuuu==“\uuuuuuuuuu main\uuuuuuuuuuuuuuuuuuu”:
如果您想让这对用户来说更容易,请使
setup.py
脚本安装一个只包含以下内容的命令(例如
/usr/bin/MyApp
):

#!/bin/bash
python -m MyApp $@
这样,他们甚至不必知道这是一个Python程序。他们所要做的就是调用
MyApp[options]

Setuptools甚至可以自动生成,称为“入口点”。有关其工作原理的更多详细信息,请参见此问题: