pdm-project / awesome-pdm

A curated list of awesome PDM plugins and resources
https://awesome.pdm-project.org/
354 stars 17 forks source link

本地测试开发插件注册后,运行插件失败 #26

Open 522247020 opened 1 month ago

522247020 commented 1 month ago

使用 pdm init 初始化项目 在包内 __init__ 中添加内容

from pdm.core import Core

from .command import HelloCommand

def hello_plugin(core: Core):
    core.register_command(HelloCommand, "hello")

同目录新建了一个文件 command.py


from pdm.cli.commands.base import BaseCommand

class HelloCommand(BaseCommand):
    """向指定的人打招呼。
    如果未指定,将从"hello.name"配置中读取。
    """
    name = 'hello'

    def add_arguments(self, parser):
        parser.add_argument("-n", "--name", help="要问候的人的姓名")

    def handle(self, project, options):
        if not options.name:
            name = project.config["hello.name"]
        else:
            name = options.name
        print(f"Hello, {name}")

pyproject.toml 文件中添加

[tool.pdm]
distribution = false
plugins = [
    "-e file:///${PROJECT_ROOT}"
]

在命令行执行

pdm install --plugins

执行 pdm self list 也能看到注册的插件,但是使用 pdm hello 这个命令会出现错误

$ pdm hello 123
Usage: pdm [-h] [-V] [-c CONFIG] [-v | -q] [--no-cache] [-I] [--pep582 [SHELL]] [-n] ...
pdm: error: Script unknown: hello