关于cmdclass参数,pip3和`python3 setup.py install`之间的区别 [英] Difference between pip3 and `python3 setup.py install` regarding cmdclass argument

查看:118
本文介绍了关于cmdclass参数,pip3和`python3 setup.py install`之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试配置软件包,以便在安装过程中执行脚本.因此,我从setuptools.command install继承并创建了我的自定义类ActionOnInstall以在安装软件包时进行处理.如

I tried to configure my package such that a script is executed on the installation process. Therefore, I inherited from setuptools.command install and created my custom class ActionOnInstall to do stuff when package is installed. This class is called via setuptools setup() argument cmdclass as described here.

此类setup.py文件的最小示例如下

A minimal example of such a setup.py file looks like

from setuptools import find_packages, setup
from setuptools.command.install import install


class ActionOnInstall(install):
    def run(self):
        print("Call install.run(self) works!")
        install.run(self)


setup(name='name',
      cmdclass={
      'install': ActionOnInstall})

通过执行

pip3 install <path-to-dir-with-setup.py>

成功运行,但不执行ActionOnInstall.run()中指定的命令.通过

runs successfully but does not execute commands specified in ActionOnInstall.run(). More directly calling this setup.py by

python3 setup.py install 

执行ActionOnInstall.run()中指定的命令.

然后,我发现自己在问:这两种安装软件包的方法的实际区别是什么?我知道,就像其他帖子告诉我们的那样,pip使软件包安装变得更轻松.但是,没有解释这两种方法如何不同地对待setup()cmdclass参数.因此,非常感谢您的来信.

Then, I found myself asking: what is the actual difference of these both approaches to install a package. I know, like other posts tell us, pip makes life easier regarding package installation. But how these both approaches treat the cmdclass argument of setup() differently is not explained. Thus, I would highly appreciate to hear from you guys.

推荐答案

pip会调用setup.py,但它会重定向stdout/stderr.要在pip下测试setup.py,请在固定位置写入文件:

pip calls your setup.py but it redirects stdout/stderr. To test setup.py under pip write to a file in a fixed location:

class ActionOnInstall(install):
    def run(self):
        print("Call install.run(self) works!", file=open('/tmp/debug.log', 'w'))
        install.run(self)

pip install .

这篇关于关于cmdclass参数,pip3和`python3 setup.py install`之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆