运行 带有自定义选项的 setuptools 安装命令时出现运行时错误

RuntimeError when running a setuptools install command with custom options

我通过子类化 setuptools.Command 在 setup.py 中使用自定义安装选项,但在调用父 运行 函数时会抛出异常。这是 setup.py 代码

from setuptools import setup
from setuptools import Command
import os, json, sys

class vboxcustom(Command):
    user_options = [
            ("disk-path=", "d", "Location where vbox disk files will be stored."),
            ]

    def initialize_options(self):
        self.disk_path = None

    def finalize_options(self):
        if self.disk_path is None:
            self.disk_path = os.path.expanduser("~/vbox")

    def run(self):
        settings_file = os.path.expanduser("~/.vbox")

        settings = {"disk_path": self.disk_path}

        f = open(settings_file, 'w')
        f.write(json.dumps(settings))

        super(vboxcustom, self).run()

setup(
        name="vbox",
        version="1.0",
        author="Ravi",
        author_email="email@email",
        python_requires=">=3",
        install_requires=["dnspython"],
        packages=["vboxhelper"],
        scripts=["scripts/vbox"],
        cmdclass={
            "install": vboxcustom,
            }
        )

抛出的异常是这样的: RuntimeError: abstract method -- subclass <class '__main__.vboxcustom'> must override。看来我必须重写一个方法但是什么方法(我只是猜测,错误很不具体)

堆栈跟踪是:

Traceback (most recent call last):
  File "setup.py", line 37, in <module>
    "install": vboxcustom,
  File "/home/ravi/work/virenvs/testvbox/lib/python3.6/site-packages/setuptools/__init__.py", line 129, in setup
    return distutils.core.setup(**attrs)
  File "/usr/lib/python3.6/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/lib/python3.6/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "setup.py", line 25, in run
    super(vboxcustom, self).run()
  File "/usr/lib/python3.6/distutils/cmd.py", line 176, in run
    % self.__class__)
RuntimeError: abstract method -- subclass <class '__main__.vboxcustom'> must override

我想我不应该从 setuptools.Command 继承来覆盖安装命令,所以我从 setuptools.command.install.install 继承,现在我得到了一个不同的错误。抛出的新异常是这样的:

distutils.errors.DistutilsGetoptError: invalid negative alias 'no-compile': option 'no-compile' not defined

您需要从 class install:

中保留 user_options
class vboxcustom(install):
    user_options = install.user_options + [
        ("disk-path=", "d", "Location where vbox disk files will be stored."),
    ]