重写cmdclass时忽略python setuptools install_requires

zse*_*der 20 python distutils setuptools install-requires

我有一个setup.py看起来像这样:

from setuptools import setup
from subprocess import call
from setuptools.command.install import install

class MyInstall(install):
    def run(self):
        call(["pip install -r requirements.txt --no-clean"], shell=True)
        install.run(self)

setup(
    author='Attila Zseder',
    version='0.1',
    name='entity_extractor',
    packages=['...'],
    install_requires=['DAWG', 'mrjob', 'cchardet'],
    package_dir={'': 'modules'},
    scripts=['...'],
    cmdclass={'install': MyInstall},
)
Run Code Online (Sandbox Code Playgroud)

我需要MyInstall因为我想从github安装一些库而我不想使用dependency_links选项,因为它不鼓励(例如这里),所以我可以用requirements.txt来做这件事.

当我安装这个软件包时pip,一切都运行正常,但由于某些原因,我必须以一种纯粹的方式解决这个问题python setup.py install.它没有.

当覆盖cmdclasssetup()与我自己的类,install_requires似乎被忽略.一旦我注释掉那一行,就会安装这些软件包.

我知道例如在distutils中不支持install_requires(如果我记得很清楚),但它在setuptools中.然后cmdclass就不会有任何影响install_requires.

我用Google搜索了这个问题几个小时,在stackoverflow上找到了很多相关的答案,但不是针对这个特殊的问题.

把所有需要的包放到requirements.txt,一切都运行正常,但我想了解为什么会这样.谢谢!

小智 15

同样的问题刚好发生在我身上.它在某种程度上似乎触发setuptools进行"旧式安装" distutils,实际上并不支持install_requires.

你在setuptools/setuptools/command/install.py,第51-74行调用install.run(self)调用run(self)

https://bitbucket.org/pypa/setuptools/src/8e8c50925f18eafb7e66fe020aa91a85b9a4b122/setuptools/command/install.py?at=default

def run(self):
    # Explicit request for old-style install?  Just do it
    if self.old_and_unmanageable or self.single_version_externally_managed:
        return _install.run(self)

    # Attempt to detect whether we were called from setup() or by another
    # command.  If we were called by setup(), our caller will be the
    # 'run_command' method in 'distutils.dist', and *its* caller will be
    # the 'run_commands' method.  If we were called any other way, our
    # immediate caller *might* be 'run_command', but it won't have been
    # called by 'run_commands'.  This is slightly kludgy, but seems to
    # work.
    #
    caller = sys._getframe(2)
    caller_module = caller.f_globals.get('__name__','')
    caller_name = caller.f_code.co_name

    if caller_module != 'distutils.dist' or caller_name!='run_commands':
        # We weren't called from the command line or setup(), so we
        # should run in backward-compatibility mode to support bdist_*
        # commands.
        _install.run(self)
    else:
        self.do_egg_install()
Run Code Online (Sandbox Code Playgroud)

我不确定这种行为是否有意,而是取代

install.run(self)
Run Code Online (Sandbox Code Playgroud)

install.do_egg_install()
Run Code Online (Sandbox Code Playgroud)

应该解决你的问题.至少它适用于我,但我也希望得到一个更详细的答案.谢谢!


小智 9

根据/sf/answers/1413724581/,更正确的方法可能是覆盖bdist_egg命令.

你可以尝试:

from setuptools.command.bdist_egg import bdist_egg as _bdist_egg

class bdist_egg(_bdist_egg):
    def run(self):
        call(["pip install -r requirements.txt --no-clean"], shell=True)
        _bdist_egg.run(self)

...

setup(...
    cmdclass={'bdist_egg': bdist_egg},  # override bdist_egg
)
Run Code Online (Sandbox Code Playgroud)

它对我有用,install_require不再被忽视.尽管如此,我仍然不明白为什么大多数人似乎都会覆盖cmdclass install并且不会抱怨install_require被忽视.