为了保持一致性,您可以为两者添加一个选项setup.py install
(setup.py develop
又名pip install -e
):(以 Ronen Botzer 的回答为基础)
from setuptools import setup
from setuptools.command.install import install
from setuptools.command.develop import develop
class CommandMixin(object):
user_options = [
('someopt', None, 'a flag option'),
('someval=', None, 'an option that takes a value')
]
def initialize_options(self):
super().initialize_options()
# Initialize options
self.someopt = None
self.someval = 0
def finalize_options(self):
# Validate options
if self.someval < 0:
raise ValueError("Illegal someval!")
super().finalize_options()
def run(self):
# Use options
global someopt
someopt = self.someopt # will be 1 or None
super().run()
class InstallCommand(CommandMixin, install):
user_options = getattr(install, 'user_options', []) + CommandMixin.user_options
class DevelopCommand(CommandMixin, develop):
user_options = getattr(develop, 'user_options', []) + CommandMixin.user_options
setup(
...,
cmdclass={
'install': InstallCommand,
'develop': DevelopCommand,
}
然后,您可以将选项传递给 pip,例如:
pip install --install-option="--someval=1" --install-option="--someopt" .
或在开发模式下:
pip install -e --install-option="--someval=1" .