Python,使用`python3.6 -m somepackge.run`

时间:2018-11-03 10:05:09

标签: python python-3.x pip

我想做与python3.6 -m http.server相同的事情。我想像这样运行我的单线纸。我该怎么做呢?现在我有:

def run():
    print('Great!')


if __name__ == '__main__':
    run()

我尝试了python3.6 -m fastapi,但我有/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6: No module named test.__main__; 'test' is a package and cannot be directly executed

以及python3.6 -m fastapi.run /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6: No module named test.run

1 个答案:

答案 0 :(得分:3)

您需要在模块中定义一个名为__main__.py的魔术文件。参见Python 3 docs on __main__

通常在其中运行单个main()入口点函数。

例如,请查看pip'sTox's one