2

I am trying to package a Python 3.6 project which additionally requires compiling CPP code. My test setup.py file currently looks like this:

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,
    },    
)

The idea here is to place the CPP-compiling code inside TestBuild. When I run python setup.py build it seems to work well:

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

However, running python setup.py install does not seem to run the build step at all:

> 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

My questions:

  1. Does the install command automatically invokes the build command?
  2. If it does, what did I do wrong? If it does not, how can I ensure the build command runs?
  3. Where can I find detailed description of the install process? I tried to dig inside the setuptools documentation and did not find anything like this.
Gadi A
  • 3,449
  • 8
  • 36
  • 54
  • The `build` command won't run because you haven't specified any source files to include in the egg, neither via `packages` nor via `python_modules` nor via `extensions` for C/C++ code. – hoefling Jul 09 '18 at 19:26

1 Answers1

0
  • Does the install command automatically invokes the build command?

Yes but it doesn't run python setup.py build — it calls the corresponding code right inside one process hence you never see the second print. If you really need to print it you have to override setuptools commands with custom classes; create such classes for build, bdist_egg and install commands.

  • Where can I find detailed description of the install process? I tried to dig inside the setuptools documentation and did not find anything like this.

I'm afraid distutils and setuptools source code is the only authoritative reference.

bdist_egg doesn't run build directly; it runs egg_info, build_clib and install_lib. install_lib calls distutils.build which runs build_py and build_ext. So it seems you have to override build_py, not build.

phd
  • 82,685
  • 13
  • 120
  • 165
  • I think that I've done the custom class thing - isn't it what I do in the attached code? Thanks! – Gadi A Jul 09 '18 at 12:45
  • This is clearly not true and I don't think it has been in past versions of setuptools. – Peter Aug 28 '20 at 10:11
  • @Peter What exactly is not true? – phd Aug 28 '20 at 10:14
  • Passing a class inheriting from e.g. `build` to `cmdclass` will not result in that classes `run` method being called when running `python setup.py install`. – Peter Aug 28 '20 at 10:17
  • @Peter What about custom `build_py`? – phd Aug 28 '20 at 10:17
  • @phd: Still no. maybe if you're running something other than `install` but that's what the OP was asking about. Maybe I'm misunderstanding your answer but I cannot produce a minimal working example. – Peter Aug 28 '20 at 10:20
  • @Peter I see. Thanks! – phd Aug 28 '20 at 10:21