Python setuptools的“ setup.py install”不会自动调用build吗?

时间:2018-07-09 10:41:57

标签: python setuptools

我正在尝试打包一个Python 3.6项目,该项目还需要编译CPP代码。我的测试setup.py文件当前如下所示:

print("Hello world from setup.py")
from distutils.command.build import build
from setuptools import setup

class TestBuild(build):
    def run(self):
        print("Hello world from TestBuild")    

setup(
    name="Test",
    cmdclass={
        'build': TestBuild,
    },    
)

这里的想法是将CPP编译代码放在 TestBuild中。当我运行 python setup.py build 时,它似乎运行良好:

> python setup.py build
Hello world from setup.py
running build
Hello world from TestBuild

但是,运行 python setup.py install 似乎根本不运行构建步骤:

> python setup.py install
Hello world from setup.py
running install
running bdist_egg
running egg_info
writing Test.egg-info/PKG-INFO
writing top-level names to Test.egg-info/top_level.txt
writing dependency_links to Test.egg-info/dependency_links.txt
reading manifest file 'Test.egg-info/SOURCES.txt'
writing manifest file 'Test.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
warning: install_lib: 'build/lib' does not exist -- no Python modules to install

creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying Test.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying Test.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying Test.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying Test.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating 'dist/Test-0.0.0-py3.5.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing Test-0.0.0-py3.5.egg

我的问题:

  1. 安装命令是否会自动调用 build 命令?
  2. 如果这样做,我做错了什么?如果没有,如何确保 build 命令运行?
  3. 在哪里可以找到安装过程的详细说明?我试图在setuptools文档中进行挖掘,但未找到类似的内容。

1 个答案:

答案 0 :(得分:1)

  
      
  • install命令是否会自动调用build命令?
  •   

是的,但是它没有运行python setup.py build-它称为 相应的代码就在一个进程中,因此您永远不会看到 第二次打印。如果确实需要打印,则必须用custom classes覆盖setuptools命令;为buildbdist_egginstall命令创建此类。

  
      
  • 在哪里可以找到安装过程的详细说明?我试图在setuptools文档中进行挖掘,但未找到类似的内容。
  •   

恐怕distutilssetuptools源代码是唯一的权威参考。

bdist_egg不会直接运行build;它运行egg_infobuild_clibinstall_libinstall_lib调用运行build_pybuild_ext的{​​{3}}。因此,看来您必须重写build_py,而不是build