I want to create a bytecode-only distribution from distutils (no really, I do; I know what I'm doing). Using setuptools and the bdist_egg command, you can simply provide the --exclude-source parameter. Unfortunately the standard commands don't have such an option.
distutils has been deprecated in NumPy 1.23. 0 . It will be removed for Python 3.12; for Python <= 3.11 it will not be removed until 2 years after the Python 3.12 release (Oct 2025).
Most Python users will not want to use this module directly, but instead use the cross-version tools maintained by the Python Packaging Authority. In particular, setuptools is an enhanced alternative to distutils that provides: support for declaring project dependencies.
Python Built Distribution A built distribution, also sometimes referred to as a bdist, is slightly more complex in that it first pre-interprets or “builds” the package and critically cuts out the post-install setup.py build step that is necessary when using sdist.
Built Distribution. A Distribution format containing files and metadata that only need to be moved to the correct location on the target system, to be installed. Wheel is such a format, whereas distutil's Source Distribution is not, in that it requires a build step before it can be installed.
The distutils "build_py" command is the one that matters, as it's (indirectly) reused by all the commands that create distributions. If you override the byte_compile(files) method, something like:
try:
from setuptools.command.build_py import build_py
except ImportError:
from distutils.command.build_py import build_py
class build_py(build_py)
def byte_compile(self, files):
super(build_py, self).byte_compile(files)
for file in files:
if file.endswith('.py'):
os.unlink(file)
setup(
...
cmdclass = dict(build_py=build_py),
...
)
You should be able to make it so that the source files are deleted from the build tree before they're copied to the "install" directory (which is a temporary directory when bdist commands invoke them).
Note: I have not tested this code; YMMV.
Try this:
from distutils.command.install_lib import install_lib
class install_lib(install_lib, object):
""" Class to overload install_lib so we remove .py files from the resulting
RPM """
def run(self):
""" Overload the run method and remove all .py files after compilation
"""
super(install_lib, self).run()
for filename in self.install():
if filename.endswith('.py'):
os.unlink(filename)
def get_outputs(self):
""" Overload the get_outputs method and remove any .py entries in the
file list """
filenames = super(install_lib, self).get_outputs()
return [filename for filename in filenames
if not filename.endswith('.py')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With