我正在尝试打包一个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
我的问题:
答案 0 :(得分:1)
- install命令是否会自动调用build命令?
是的,但是它没有运行python setup.py build
-它称为
相应的代码就在一个进程中,因此您永远不会看到
第二次打印。如果确实需要打印,则必须用custom classes覆盖setuptools
命令;为build
,bdist_egg
和install
命令创建此类。
- 在哪里可以找到安装过程的详细说明?我试图在setuptools文档中进行挖掘,但未找到类似的内容。
恐怕distutils
和setuptools
源代码是唯一的权威参考。
bdist_egg
不会直接运行build
;它运行egg_info
,build_clib
和install_lib
。 install_lib
调用运行build_py
和build_ext
的{{3}}。因此,看来您必须重写build_py
,而不是build
。