Text of docs.python.org › 3.11 › distutils › setupscript.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

2. Writing the Setup Script — Python 3.11.18 documentation

Navigation index modules | next | previous | Python » 3.11.18 Documentation » Distributing Python Modules (Legacy version) » 2. Writing the Setup Script | Theme | 2. Writing the Setup Script ¶ Note This document is being retained solely until the setuptools documentation at https://setuptools.readthedocs.io/en/latest/setuptools.html independently covers all of the relevant information currently included here. The setup script is the centre of all activity in building, distributing, and installing modules using the Distutils.

The main purpose of the setup script is to describe your module distribution to the Distutils, so that the various commands that operate on your modules do the right thing. As we saw in section A Simple Example above, the setup script consists mainly of a call to setup() , and most information supplied to the Distutils by the module developer is supplied as keyword arguments to setup() . Here’s a slightly more involved example, which we’ll follow for the next couple of sections: the Distutils’ own setup script. (Keep in mind that although the Distutils are included with Python 1.6 and later, they also have an independent existence so that Python 1.5.2 users can use them to install other module distributions.

The Distutils’ own setup script, shown here, is used to install the package into Python 1.5.2.) #!/usr/bin/env python from distutils.core import setup setup ( name = 'Distutils' , version = '1.0' , description = 'Python Distribution Utilities' , author = 'Greg Ward' , author_email = 'gward@python.net' , url = 'https://www.python.org/sigs/distutils-sig/' , packages = [ 'distutils' , 'distutils.command' ], ) There are only two differences between this and the trivial one-file distribution presented in section A Simple Example : more metadata, and the specification of pure Python modules by package, rather than by module.

This is important since the Distutils consist of a couple of dozen modules split into (so far) two packages; an explicit list of every module would be tedious to generate and difficult to maintain. For more information on the additional meta-data, see section Additional meta-data . Note that any pathnames (files or directories) supplied in the setup script should be written using the Unix convention, i.e. slash-separated.

The Distutils will take care of converting this platform-neutral representation into whatever is appropriate on your current platform before actually using the pathname. This makes your setup script portable across operating systems, which of course is one of the major goals of the Distutils. In this spirit, all pathnames in this document are slash-separated. This, of course, only applies to pathnames given to Distutils functions.

If you, for example, use standard Python functions such as glob.glob() or os.listdir() to specify files, you should be careful to write portable code instead of hardcoding path separators: glob . glob ( os . path . join ( 'mydir' , 'subdir' , '*.html' )) os . listdir ( os . path . join ( 'mydir' , 'subdir' )) 2.1. Listing whole packages ¶ The packages option tells the Distutils to process (build, distribute, install, etc.) all pure Python modules found in each package mentioned in the packages list.

In order to do this, of course, there has to be a correspondence between package names and directories in the filesystem. The default correspondence is the most obvious one, i.e. package distutils is found in the directory distutils relative to the distribution root. Thus, when you say packages = ['foo'] in your setup script, you are promising that the Distutils will find a file foo/__init__.py (which might be spelled differently on your system, but you get the idea) relative to the directory where your setup script lives.

If you break this promise, the Distutils will issue a warning but still process the broken package anyway. If you use a different convention to lay out your source directory, that’s no problem: you just have to supply the package_dir option to tell the Distutils about your convention. For example, say you keep all Python source under lib , so that modules in the “root package” (i.e., not in any package at all) are in lib , modules in the foo package are in lib/foo , and so forth.

Then you would put package_dir = { '' : 'lib' } in your setup script. The keys to this dictionary are package names, and an empty package name stands for the root package. The values are directory names relative to your distribution root. In this case, when you say packages = ['foo'] , you are promising that the file lib/foo/__init__.py exists. Another possible convention is to put the foo package right in lib , the foo.bar package in lib/bar , etc.

This would be written in the setup script as package_dir = { 'foo' : 'lib' } A package: dir entry in the package_dir dictionary implicitly applies to all packages below package , so the foo.bar case is automatically handled here. In this example, having packages = ['foo', 'foo.bar'] tells the Distutils to look for lib/__init__.py and lib/bar/__init__.py . (Keep in mind that although package_dir applies recursively, you must explicitly list all packages in packages : the Distutils will not recursively scan your source tree looking for any directory with an __init__.py file.) 2.2.

Listing individual modules ¶ For a small module distribution, you might prefer to list all modules rather than listing packages—especially the case of a single module that goes in the “root package” (i.e., no package at all). This simplest case was shown in section A Simple Example ; here is a slightly more involved example: py_modules = [ 'mod1' , 'pkg.mod2' ] This describes two modules, one of them in the “root” package, the other in the pkg package.

Again, the default package/directory layout implies that these two modules can be found in mod1.py and pkg/mod2.py , and that pkg/__init__.py exists as well. And again, you can override the package/directory correspondence using the package_dir option. 2.3. Describing extension modules ¶ Just as writing Python extension modules is a bit more complicated than writing pure Python modules, describing them to the Distutils is a bit more complicated.

Unlike pure modules, it’s not enough just to list modules or packages and expect the Distutils to go out and find the right files; you have to specify the extension name, source file(s), and any compile/link requirements (include directories, libraries to link with, etc.). All of this is done through another keyword argument to setup() , the ext_modules option. ext_modules is just a list of Extension instances, each of which describes a single extension module.

Suppose your distribution includes a single extension, called foo and implemented by foo.c . If no additional instructions to the compiler/linker are needed, describing this extension is quite simple: Extension ( 'foo' , [ 'foo.c' ]) The Extension class can be imported from distutils.core along with setup() . Thus, the setup script for a module distribution that contains only this one extension and nothing else might be: from distutils.core import setup , Extension setup ( name = 'foo' , version = '1.0' , ext_modules = [ Extension ( 'foo' , [ 'foo.c' ])], ) The Extension class (actually, the underlying extension-building machinery implemented by the build_ext command) supports a great deal of flexibility in describing Python extensions, which is explained in the following sections.

2.3.1. Extension names and packages ¶ The first argument to the Extension constructor is always the name of the extension, including any package names. For example, Extension ( 'foo' , [ 'src/foo1.c' , 'src/foo2.c' ]) describes an extension that lives in the root package, while Extension ( 'pkg.foo' , [ 'src/foo1.c' , 'src/foo2.c' ]) describes the same extension in the pkg package. The source files and resulting object code are identical in both cases; the only difference is where in the filesystem

The rest of this page is on the live site.