问题
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