Distutils ignores build/lib on Ubuntu

余生长醉 提交于 2021-01-28 13:41:55

问题


I have a setup.py script which builds files to be installed to the ./build/lib directory. The files are populated by the run() method of my custom distutils.command.build.build subclass:

from distutils.command.build import build
from distutils.core import setup

class MyBuild(build):
    def run(self):
        # Populate files to ./build/lib

setup(
    # ...
    cmdclass=dict(build=MyBuild)
)

Now, according to this article the setup script should copy everything in the ./build/lib directory to the installation directory, which works as expected on OSX but not on Ubuntu 14.04 where it ignores the ./build/lib directory but rather installs files found in ./build/lib.<plat>, which in turn doesn't work on OSX where the ./build/lib.<plat> directory is ignored.

Is there a consistent, platform independent way to build and install files with distutils? The files are platform-independent.


回答1:


In the MyBuild.run() method, populate files to the path given in self.build_lib instead of a hardcoded path.

from distutils.command.build import build
from distutils.core import setup

class MyBuild(build):
    def run(self):
        build_path = self.build_lib
        # Populate files to 'build_path'

setup(
    # ...
    cmdclass=dict(build=MyBuild)
)

Do not change the value of self.build_lib in MyBuild.run() as it is generated from command line arguments and/or various default values. The same goes for several other attributes such as build_scripts, build_base, build_purelib, etc.




回答2:


The simplest solution seems to be in setting the build_lib attribute of the distutils.command.build.build command class. The attribute is set in the initialize_options() method which we need to override to set the attribute:

from distutils.command.build import build
from distutils.core import setup

class MyBuild(build):
    def initialize_options(self):
        build.initialize_options(self)
        self.build_lib = 'build/lib'

    def run(self):
        # Populate files to ./build/lib

setup(
    # ...
    cmdclass=dict(build=MyBuild)
)


来源:https://stackoverflow.com/questions/25720643/distutils-ignores-build-lib-on-ubuntu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!