The distutils.core module is the only module that needs to be installed to use the Distutils. It provides the setup() (which is called from the setup script). Indirectly provides the distutils.dist.Distribution and distutils.cmd.Command class.
The basic do-everything function that does most everything you could ever ask for from a Distutils method. See XXXXX
The setup function takes a large number of arguments. These are laid out in the following table.
argument name | value | type |
---|---|---|
name | The name of the package | a string |
version | The version number of the package | See distutils.version |
description | A single line describing the package | a string |
long_description | Longer description of the package | a string |
author | The name of the package author | a string |
author_email | The email address of the package author | a string |
maintainer | The name of the current maintainer, if different from the author | a string |
maintainer_email | The email address of the current maintainer, if different from the author | |
url | A URL for the package (homepage) | a URL |
download_url | A URL to download the package | a URL |
packages | A list of Python packages that distutils will manipulate | a list of strings |
py_modules | A list of Python modules that distutils will manipulate | a list of strings |
scripts | A list of standalone script files to be built and installed | a list of strings |
ext_modules | A list of Python extensions to be built | A list of instances of distutils.core.Extension |
classifiers | A list of categories for the package | The list of available categorizations is at http://pypi.python.org/pypi?:action=list_classifiers. |
distclass | the Distribution class to use | A subclass of distutils.core.Distribution |
script_name | The name of the setup.py script - defaults to sys.argv[0] | a string |
script_args | Arguments to supply to the setup script | a list of strings |
options | default options for the setup script | a string |
license | The license for the package | a string |
keywords | Descriptive meta-data, see PEP 314 | |
platforms | ||
cmdclass | A mapping of command names to Command subclasses | a dictionary |
data_files | A list of data files to install | a list |
package_dir | A mapping of package to directory names | a dictionary |
Run a setup script in a somewhat controlled environment, and return the distutils.dist.Distribution instance that drives things. This is useful if you need to find out the distribution meta-data (passed as keyword args from script to setup()), or the contents of the config files or command-line.
script_name is a file that will be read and run with exec(). sys.argv[0] will be replaced with script for the duration of the call. script_args is a list of strings; if supplied, sys.argv[1:] will be replaced by script_args for the duration of the call.
stop_after tells setup() when to stop processing; possible values:
value | description |
---|---|
init | Stop after the Distribution instance has been created and populated with the keyword arguments to setup() |
config | Stop after config files have been parsed (and their data stored in the Distribution instance) |
commandline | Stop after the command-line (sys.argv[1:] or script_args) have been parsed (and the data stored in the Distribution instance.) |
run | Stop after all commands have been run (the same as if setup() had been called in the usual way). This is the default value. |
In addition, the distutils.core module exposed a number of classes that live elsewhere.
A short description of each of these follows, but see the relevant module for the full reference.
The Extension class describes a single C or C++extension module in a setup script. It accepts the following keyword arguments in its constructor
argument name | value | type |
---|---|---|
name | the full name of the extension, including any packages — ie. not a filename or pathname, but Python dotted name | string |
sources | list of source filenames, relative to the distribution root (where the setup script lives), in Unix form (slash- separated) for portability. Source files may be C, C++, SWIG (.i), platform-specific resource files, or whatever else is recognized by the build_ext command as source for a Python extension. | string |
include_dirs | list of directories to search for C/C++ header files (in Unix form for portability) | string |
define_macros | list of macros to define; each macro is defined using a 2-tuple (name, value), where value is either the string to define it to or None to define it without a particular value (equivalent of #define FOO in source or -DFOO on Unix C compiler command line) | (string, string) tuple or (name, None) |
undef_macros | list of macros to undefine explicitly | string |
library_dirs | list of directories to search for C/C++ libraries at link time | string |
libraries | list of library names (not filenames or paths) to link against | string |
runtime_library_dirs | list of directories to search for C/C++ libraries at run time (for shared extensions, this is when the extension is loaded) | string |
extra_objects | list of extra files to link with (eg. object files not implied by ‘sources’, static library that must be explicitly specified, binary resource files, etc.) | string |
extra_compile_args | any extra platform- and compiler-specific information to use when compiling the source files in ‘sources’. For platforms and compilers where a command line makes sense, this is typically a list of command-line arguments, but for other platforms it could be anything. | string |
extra_link_args | any extra platform- and compiler-specific information to use when linking object files together to create the extension (or to create a new static Python interpreter). Similar interpretation as for ‘extra_compile_args’. | string |
export_symbols | list of symbols to be exported from a shared extension. Not used on all platforms, and not generally necessary for Python extensions, which typically export exactly one symbol: init + extension_name. | string |
depends | list of files that the extension depends on | string |
language | extension language (i.e. 'c', 'c++', 'objc'). Will be detected from the source extensions if not provided. | string |
A Distribution describes how to build, install and package up a Python software package.
See the setup() function for a list of keyword arguments accepted by the Distribution constructor. setup() creates a Distribution instance.
This module provides the abstract base class for the CCompiler classes. A CCompiler instance can be used for all the compile and link steps needed to build a single project. Methods are provided to set options for the compiler — macro definitions, include directories, link path, libraries and the like.
This module provides the following functions.
Determine the default compiler to use for the given platform.
osname should be one of the standard Python OS names (i.e. the ones returned by os.name) and platform the common value returned by sys.platform for the platform in question.
The default values are os.name and sys.platform in case the parameters are not given.
The abstract base class CCompiler defines the interface that must be implemented by real compiler classes. The class also has some utility methods used by several compiler classes.
The basic idea behind a compiler abstraction class is that each instance can be used for all the compile/link steps in building a single project. Thus, attributes common to all of those compile and link steps — include directories, macros to define, libraries to link against, etc. — are attributes of the compiler instance. To allow for variability in how individual files are treated, most of those attributes may be varied on a per-compilation or per-link basis.
The constructor for each subclass creates an instance of the Compiler object. Flags are verbose (show verbose output), dry_run (don’t actually execute the steps) and force (rebuild everything, regardless of dependencies). All of these flags default to 0 (off). Note that you probably don’t want to instantiate CCompiler or one of its subclasses directly - use the distutils.CCompiler.new_compiler() factory function instead.
The following methods allow you to manually alter compiler options for the instance of the Compiler class.
Add libname to the list of libraries that will be included in all links driven by this compiler object. Note that libname should *not* be the name of a file containing a library, but the name of the library itself: the actual filename will be inferred by the linker, the compiler, or the compiler class (depending on the platform).
The linker will be instructed to link against libraries in the order they were supplied to add_library() and/or set_libraries(). It is perfectly valid to duplicate library names; the linker will be instructed to link against libraries as many times as they are mentioned.
The following methods implement methods for autodetection of compiler options, providing some functionality similar to GNU autoconf.
Define the executables (and options for them) that will be run to perform the various stages of compilation. The exact set of executables that may be specified here depends on the compiler class (via the ‘executables’ class attribute), but most will have:
attribute | description |
---|---|
compiler | the C/C++ compiler |
linker_so | linker used to create shared objects and libraries |
linker_exe | linker used to create binary executables |
archiver | static library creator |
On platforms with a command-line (Unix, DOS/Windows), each of these is a string that will be split into executable name and (optional) list of arguments. (Splitting the string is done similarly to how Unix shells operate: words are delimited by spaces, but quotes and backslashes can override this. See distutils.util.split_quoted().)
The following methods invoke stages in the build process.
Compile one or more source files. Generates object files (e.g. transforms a .c file to a .o file.)
sources must be a list of filenames, most likely C/C++ files, but in reality anything that can be handled by a particular compiler and compiler class (eg. MSVCCompiler can handle resource files in sources). Return a list of object filenames, one per source filename in sources. Depending on the implementation, not all source files will necessarily be compiled, but all corresponding object filenames will be returned.
If output_dir is given, object files will be put under it, while retaining their original path component. That is, foo/bar.c normally compiles to foo/bar.o (for a Unix implementation); if output_dir is build, then it would compile to build/foo/bar.o.
macros, if given, must be a list of macro definitions. A macro definition is either a (name, value) 2-tuple or a (name,) 1-tuple. The former defines a macro; if the value is None, the macro is defined without an explicit value. The 1-tuple case undefines a macro. Later definitions/redefinitions/undefinitions take precedence.
include_dirs, if given, must be a list of strings, the directories to add to the default include file search path for this compilation only.
debug is a boolean; if true, the compiler will be instructed to output debug symbols in (or alongside) the object file(s).
extra_preargs and extra_postargs are implementation-dependent. On platforms that have the notion of a command-line (e.g. Unix, DOS/Windows), they are most likely lists of strings: extra command-line arguments to prepend/append to the compiler command line. On other platforms, consult the implementation class documentation. In any event, they are intended as an escape hatch for those occasions when the abstract compiler framework doesn’t cut the mustard.
depends, if given, is a list of filenames that all targets depend on. If a source file is older than any file in depends, then the source file will be recompiled. This supports dependency tracking, but only at a coarse granularity.
Raises CompileError on failure.
Link a bunch of stuff together to create a static library file. The “bunch of stuff” consists of the list of object files supplied as objects, the extra object files supplied to add_link_object() and/or set_link_objects(), the libraries supplied to add_library() and/or set_libraries(), and the libraries supplied as libraries (if any).
output_libname should be a library name, not a filename; the filename will be inferred from the library name. output_dir is the directory where the library file will be put. XXX defaults to what?
debug is a boolean; if true, debugging information will be included in the library (note that on most platforms, it is the compile step where this matters: the debug flag is included here just for consistency).
target_lang is the target language for which the given objects are being compiled. This allows specific linkage time treatment of certain languages.
Raises LibError on failure.
Link a bunch of stuff together to create an executable or shared library file.
The “bunch of stuff” consists of the list of object files supplied as objects. output_filename should be a filename. If output_dir is supplied, output_filename is relative to it (i.e. output_filename can provide directory components if needed).
libraries is a list of libraries to link against. These are library names, not filenames, since they’re translated into filenames in a platform-specific way (eg. foo becomes libfoo.a on Unix and foo.lib on DOS/Windows). However, they can include a directory component, which means the linker will look in that specific directory rather than searching all the normal locations.
library_dirs, if supplied, should be a list of directories to search for libraries that were specified as bare library names (ie. no directory component). These are on top of the system default and those supplied to add_library_dir() and/or set_library_dirs(). runtime_library_dirs is a list of directories that will be embedded into the shared library and used to search for other shared libraries that *it* depends on at run-time. (This may only be relevant on Unix.)
export_symbols is a list of symbols that the shared library will export. (This appears to be relevant only on Windows.)
debug is as for compile() and create_static_lib(), with the slight distinction that it actually matters on most platforms (as opposed to create_static_lib(), which includes a debug flag mostly for form’s sake).
extra_preargs and extra_postargs are as for compile() (except of course that they supply command-line arguments for the particular linker being used).
target_lang is the target language for which the given objects are being compiled. This allows specific linkage time treatment of certain languages.
Raises LinkError on failure.
Preprocess a single C/C++ source file, named in source. Output will be written to file named output_file, or stdout if output_file not supplied. macros is a list of macro definitions as for compile(), which will augment the macros set with define_macro() and undefine_macro(). include_dirs is a list of directory names that will be added to the default list, in the same way as add_include_dir().
Raises PreprocessError on failure.
The following utility methods are defined by the CCompiler class, for use by the various concrete subclasses.
This module provides the UnixCCompiler class, a subclass of CCompiler that handles the typical Unix-style command-line C compiler:
This module provides MSVCCompiler, an implementation of the abstract CCompiler class for Microsoft Visual Studio. Typically, extension modules need to be compiled with the same compiler that was used to compile Python. For Python 2.3 and earlier, the compiler was Visual Studio 6. For Python 2.4 and 2.5, the compiler is Visual Studio .NET 2003. The AMD64 and Itanium binaries are created using the Platform SDK.
MSVCCompiler will normally choose the right compiler, linker etc. on its own. To override this choice, the environment variables DISTUTILS_USE_SDK and MSSdk must be both set. MSSdk indicates that the current environment has been setup by the SDK’s SetEnv.Cmd script, or that the environment variables had been registered when the SDK was installed; DISTUTILS_USE_SDK indicates that the distutils user has made an explicit choice to override the compiler selection by MSVCCompiler.
This module provides BorlandCCompiler, an subclass of the abstract CCompiler class for the Borland C++ compiler.
This module provides the CygwinCCompiler class, a subclass of UnixCCompiler that handles the Cygwin port of the GNU C compiler to Windows. It also contains the Mingw32CCompiler class which handles the mingw32 port of GCC (same as cygwin in no-cygwin mode).
This module provides the EMXCCompiler class, a subclass of UnixCCompiler that handles the EMX port of the GNU C compiler to OS/2.
This module provides a few functions for creating archive files, such as tarballs or zipfiles.
This module provides functions for performing simple, timestamp-based dependency of files and groups of files; also, functions based entirely on such timestamp dependency analysis.
This module provides functions for operating on directories and trees of directories.
Copy an entire directory tree src to a new location dst. Both src and dst must be directory names. If src is not a directory, raise DistutilsFileError. If dst does not exist, it is created with mkpath(). The end result of the copy is that every file in src is copied to dst, and directories under src are recursively copied to dst. Return the list of files that were copied or might have been copied, using their output name. The return value is unaffected by update or dry_run: it is simply the list of all files under src, with the names changed to be under dst.
preserve_mode and preserve_times are the same as for copy_file() in distutils.file_util; note that they only apply to regular files, not to directories. If preserve_symlinks is true, symlinks will be copied as symlinks (on platforms that support them!); otherwise (the default), the destination of the symlink will be copied. update and verbose are the same as for copy_file().
This module contains some utility functions for operating on individual files.
Copy file src to dst. If dst is a directory, then src is copied there with the same name; otherwise, it must be a filename. (If the file exists, it will be ruthlessly clobbered.) If preserve_mode is true (the default), the file’s mode (type and permission bits, or whatever is analogous on the current platform) is copied. If preserve_times is true (the default), the last-modified and last-access times are copied as well. If update is true, src will only be copied if dst does not exist, or if dst does exist but is older than src.
link allows you to make hard links (using os.link()) or symbolic links (using os.symlink()) instead of copying: set it to 'hard' or 'sym'; if it is None (the default), files are copied. Don’t set link on systems that don’t support it: copy_file() doesn’t check if hard or symbolic linking is available. It uses _copy_file_contents() to copy file contents.
Return a tuple (dest_name, copied): dest_name is the actual name of the output file, and copied is true if the file was copied (or would have been copied, if dry_run true).
Move file src to dst. If dst is a directory, the file will be moved into it with the same name; otherwise, src is just renamed to dst. Returns the new full name of the file.
Warning
Handles cross-device moves on Unix using copy_file(). What about other systems?
This module contains other assorted bits and pieces that don’t fit into any other utility module.
Return a string that identifies the current platform. This is used mainly to distinguish platform-specific build directories and platform-specific built distributions. Typically includes the OS name and version and the architecture (as supplied by ‘os.uname()’), although the exact information included depends on the OS; eg. for IRIX the architecture isn’t particularly important (IRIX only runs on SGI hardware), but for Linux the kernel version isn’t particularly important.
Examples of returned values:
For non-POSIX platforms, currently just returns sys.platform.
For Mac OS X systems the OS version reflects the minimal version on which binaries will run (that is, the value of MACOSX_DEPLOYMENT_TARGET during the build of Python), not the OS version of the current system.
For universal binary builds on Mac OS X the architecture value reflects the univeral binary status instead of the architecture of the current processor. For 32-bit universal binaries the architecture is fat, for 64-bit universal binaries the architecture is fat64, and for 4-way universal binaries the architecture is universal. Starting from Python 2.7 and Python 3.2 the architecture fat3 is used for a 3-way universal build (ppc, i386, x86_64) and intel is used for a univeral build with the i386 and x86_64 architectures
Examples of returned values on Mac OS X:
Ensure that ‘os.environ’ has all the environment variables we guarantee that users can use in config files, command-line options, etc. Currently this includes:
Perform shell/Perl-style variable substitution on s. Every occurrence of $ followed by a name is considered a variable, and variable is substituted by the value found in the local_vars dictionary, or in os.environ if it’s not in local_vars. os.environ is first checked/augmented to guarantee that it contains certain values: see check_environ(). Raise ValueError for any variables not found in either local_vars or os.environ.
Note that this is not a fully-fledged string interpolation function. A valid $variable can consist only of upper and lower case letters, numbers and an underscore. No { } or ( ) style quoting is available.
Convert a string representation of truth to true (1) or false (0).
True values are y, yes, t, true, on and 1; false values are n, no, f, false, off and 0. Raises ValueError if val is anything else.
Byte-compile a collection of Python source files to either .pyc or .pyo files in the same directory. py_files is a list of files to compile; any files that don’t end in .py are silently skipped. optimize must be one of the following:
If force is true, all files are recompiled regardless of timestamps.
The source filename encoded in each bytecode file defaults to the filenames listed in py_files; you can modify these with prefix and basedir. prefix is a string that will be stripped off of each source filename, and base_dir is a directory name that will be prepended (after prefix is stripped). You can supply either or both (or neither) of prefix and base_dir, as you wish.
If dry_run is true, doesn’t actually do anything that would affect the filesystem.
Byte-compilation is either done directly in this interpreter process with the standard py_compile module, or indirectly by writing a temporary script and executing it. Normally, you should let byte_compile() figure out to use direct compilation or not (see the source for details). The direct flag is used by the script generated in indirect mode; unless you know what you’re doing, leave it set to None.
This module provides the Distribution class, which represents the module distribution being built/installed/distributed.
This module provides the Extension class, used to describe C/C++ extension modules in setup scripts.
This module provides the DEBUG flag.
Provides exceptions used by the Distutils modules. Note that Distutils modules may raise standard exceptions; in particular, SystemExit is usually raised for errors that are obviously the end-user’s fault (eg. bad command-line arguments).
This module is safe to use in from ... import * mode; it only exports symbols whose names start with Distutils and end with Error.
This module provides a wrapper around the standard getopt module that provides the following additional features:
The option_table is a list of 3-tuples: (long_option, short_option, help_string)
If an option takes an argument, its long_option should have '=' appended; short_option should just be a single character, no ':' in any case. short_option should be None if a long_option doesn’t have a corresponding short_option. All option tuples must have long options.
The FancyGetopt class provides the following methods:
Parse command-line options in args. Store as attributes on object.
If args is None or not supplied, uses sys.argv[1:]. If object is None or not supplied, creates a new OptionDummy instance, stores option values there, and returns a tuple (args, object). If object is supplied, it is modified in place and getopt() just returns args; in both cases, the returned args is a modified copy of the passed-in args list, which is left untouched.
Generate help text (a list of strings, one per suggested line of output) from the option table for this FancyGetopt object.
If supplied, prints the supplied header at the top of the help.
This module provides the FileList class, used for poking about the filesystem and building lists of files.
This module provides the spawn() function, a front-end to various platform-specific functions for launching another program in a sub-process. Also provides find_executable() to search the path for a given executable name.
The distutils.sysconfig module provides access to Python’s low-level configuration information. The specific configuration variables available depend heavily on the platform and configuration. The specific variables depend on the build process for the specific version of Python being run; the variables are those found in the Makefile and configuration header that are installed with Python on Unix systems. The configuration header is called pyconfig.h for Python versions starting with 2.2, and config.h for earlier versions of Python.
Some additional functions are provided which perform some useful manipulations for other parts of the distutils package.
The following function is only intended for use within the distutils package.
Do any platform-specific customization of a distutils.ccompiler.CCompiler instance.
This function is only needed on Unix at this time, but should be called consistently to support forward-compatibility. It inserts the information that varies across Unix flavors and is stored in Python’s Makefile. This information includes the selected compiler, compiler and linker options, and the extension used by the linker for shared objects.
This function is even more special-purpose, and should only be used from Python’s own build procedures.
This module provides the TextFile class, which gives an interface to text files that (optionally) takes care of stripping comments, ignoring blank lines, and joining lines with backslashes.
This class provides a file-like object that takes care of all the things you commonly want to do when processing a text file that has some line-by-line syntax: strip comments (as long as # is your comment character), skip blank lines, join adjacent lines by escaping the newline (ie. backslash at end of line), strip leading and/or trailing whitespace. All of these are optional and independently controllable.
The class provides a warn() method so you can generate warning messages that report physical line number, even if the logical line in question spans multiple physical lines. Also provides unreadline() for implementing line-at-a-time lookahead.
TextFile instances are create with either filename, file, or both. RuntimeError is raised if both are None. filename should be a string, and file a file object (or something that provides readline() and close() methods). It is recommended that you supply at least filename, so that TextFile can include it in warning messages. If file is not supplied, TextFile creates its own using the open() built-in function.
The options are all boolean, and affect the values returned by readline()
option name | description | default |
---|---|---|
strip_comments | strip from '#' to end-of- line, as well as any whitespace leading up to the '#'—unless it is escaped by a backslash | true |
lstrip_ws | strip leading whitespace from each line before returning it | false |
rstrip_ws | strip trailing whitespace (including line terminator!) from each line before returning it. | true |
skip_blanks | skip lines that are empty *after* stripping comments and whitespace. (If both lstrip_ws and rstrip_ws are false, then some lines may consist of solely whitespace: these will *not* be skipped, even if skip_blanks is true.) | true |
join_lines | if a backslash is the last non-newline character on a line after stripping comments and whitespace, join the following line to it to form one logical line; if N consecutive lines end with a backslash, then N+1 physical lines will be joined to form one logical line. | false |
collapse_join | strip leading whitespace from lines that are joined to their predecessor; only matters if (join_lines and not lstrip_ws) | false |
Note that since rstrip_ws can strip the trailing newline, the semantics of readline() must differ from those of the built-in file object’s readline() method! In particular, readline() returns None for end-of-file: an empty string might just be a blank line (or an all-whitespace line), if rstrip_ws is true but skip_blanks is not.
This module supplies the abstract base class Command.
Abstract base class for defining command classes, the “worker bees” of the Distutils. A useful analogy for command classes is to think of them as subroutines with local variables called options. The options are declared in initialize_options() and defined (given their final values) in finalize_options(), both of which must be defined by every command class. The distinction between the two is necessary because option values might come from the outside world (command line, config file, ...), and any options dependent on other options must be computed after these outside influences have been processed — hence finalize_options(). The body of the subroutine, where it does all its work based on the values of its options, is the run() method, which must also be implemented by every command class.
The class constructor takes a single argument dist, a Distribution instance.
This section outlines the steps to create a new Distutils command.
A new command lives in a module in the distutils.command package. There is a sample template in that directory called command_template. Copy this file to a new module with the same name as the new command you’re implementing. This module should implement a class with the same name as the module (and the command). So, for instance, to create the command peel_banana (so that users can run setup.py peel_banana), you’d copy command_template to distutils/command/peel_banana.py, then edit it so that it’s implementing the class peel_banana, a subclass of distutils.cmd.Command.
Subclasses of Command must define the following methods.
sub_commands formalizes the notion of a “family” of commands, e.g. install as the parent with sub-commands install_lib, install_headers, etc. The parent of a family of commands defines sub_commands as a class attribute; it’s a list of 2-tuples (command_name, predicate), with command_name a string and predicate a function, a string or None. predicate is a method of the parent command that determines whether the corresponding command is applicable in the current situation. (E.g. we install_headers is only applicable if we have any C header files to install.) If predicate is None, that command is always applicable.
sub_commands is usually defined at the end of a class, because predicates can be methods of the class, so they must already have been defined. The canonical example is the install command.
Builds a Windows Installer (.msi) binary package.
In most cases, the bdist_msi installer is a better choice than the bdist_wininst installer, because it provides better support for Win64 platforms, allows administrators to perform non-interactive installations, and allows installation through group policies.
Alternative implementation of build_py which also runs the 2to3 conversion library on each .py file that is going to be installed. To use this in a setup.py file for a distribution that is designed to run with both Python 2.x and 3.x, add:
try:
from distutils.command.build_py import build_py_2to3 as build_py
except ImportError:
from distutils.command.build_py import build_py
to your setup.py, and later:
cmdclass = {'build_py': build_py}
to the invocation of setup().