本文介绍了如何使用 setuptools 生成调用 `python -m mypackage` 的 console_scripts 入口点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力成为一名优秀的 Pythonista 并遵循 PEP 338我计划部署的包.

I am trying to be a good Pythonista and following PEP 338 for my package I plan on deploying.

我还尝试在 python setuptools install 上使用 setuptools entry_points{'console_scripts': ... } 选项生成我的可执行脚本.

I am also trying to generate my executable scripts upon python setuptools install using setuptools entry_points{'console_scripts': ... } options.

如何使用 entry_points 生成调用 python -m mypackage(并传递 *args、**kwargs)的二进制文件?

How can I use entry_points to generate a binary that calls python -m mypackage (and passes *args, **kwargs) ?

这里有一些我没有成功的尝试:

Here are a few attempts I have made with no success:

setuptools(
...

(1)

entry_points=
       {'console_scripts': ['mypkg=mypkg.__main__'],},

(2)

entry_points=
       {'console_scripts': ['mypkg=mypkg.main'],},

(3)

entry_points=
       {'console_scripts': ['mypkg=python -m mypkg'],},

我一直在使用的主要资源:

Primary resources I have been using:

推荐答案

我认为这是看待问题的错误方式.您不希望脚本调用 python -m mypackage,但希望脚本具有与 python -m mypackage

I think this is the wrong way to look at the problem. You don't want your script to call python -m mypackage, but you want the script to have the same entry point as python -m mypackage

考虑这个简单的例子:

script_proj/
├── script_proj
│   ├── __init__.py
│   └── __main__.py
└── setup.py

和简约的 setup.py:

and the minimalistic setup.py:

from setuptools import setup

setup(
    name="script_proj",
    packages=["script_proj"],
    entry_points = {
        "console_scripts": [
            "myscript = script_proj.__main__:main",
        ]
    }
)

__main__.py 是一个虚拟模块,包含 main 方法.

__main__.py is a dummy module and contains the main method.

def main():
    print("Hello world!")

if __name__ == "__main__":
    main()

安装后,您有可执行文件myscript,它调用__main__.py 中的main 方法.在这个包设计中,python -m script_proj 也调用了相同的 main 方法.

After installing, you have the executable myscript, which calls the main method in __main__.py.In this package design python -m script_proj also calls the same main method.

这篇关于如何使用 setuptools 生成调用 `python -m mypackage` 的 console_scripts 入口点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 23:52