Skip to main content
 首页 » 编程设计

python之setup.py 中的入口点

2025年05月04日7insus

我正在使用 Click 在 python 中创建 CLI。这是我的入口点脚本:

entry_points=''' 
    [console_scripts] 
    noo=noo.noodle:downloader 
''', 

我制作了一个包,我在 __init__.py 文件中添加了 import noodle 以便它可以导入包含函数 downloader 的文件 noodle () - 需要由 entry_point 脚本执行。但是,当我安装 setup.py 时,出现错误:ImportError: No module named noo.noodle 当我运行 noodle --help 时在终端?

请您参考如下方法:

直接来自 documentation on click.pocoo.org :

你的脚本.py:

import click 
 
@click.command() 
def cli(): 
    """Example script.""" 
    click.echo('Hello World!') 

设置.py:

from setuptools import setup 
 
setup( 
    name='yourscript', 
    version='0.1', 
    py_modules=['yourscript'], 
    install_requires=[ 
        'Click', 
    ], 
    entry_points=''' 
        [console_scripts] 
        yourscript=yourscript:cli 
    ''', 
) 

如果您的 CLI 应用程序中有多个命令,我通常会像这样创建一个点击组:

初始化.py:

import click 
 
@click.group() 
@click.option('--debug/--no-debug', default=False, help='My test option.') 
def cli(debug): 
    """Add some initialisation code to log accordingly for debugging purposes or no""" 
    pass   
 
@cli.command() 
def configure(): 
    """Configure the application""" 
    pass 

setup.py 文件看起来与点击文档中的文件完全一样。