blob: a757ffc38bd26d2e623ee050dc9a068a6156e0fa [file] [log] [blame]
Éric Araujo3a9f58f2011-06-01 20:42:49 +02001.. _packaging-intro:
2
3*****************************
4An Introduction to Packaging
5*****************************
6
7This document covers using Packaging to distribute your Python modules,
8concentrating on the role of developer/distributor. If you're looking for
9information on installing Python modules you should refer to the
10:ref:`packaging-install-index` chapter.
11
12Throughout this documentation, the terms "Distutils", "the Distutils" and
13"Packaging" will be used interchangeably.
14
15.. _packaging-concepts:
16
17Concepts & Terminology
18======================
19
20Using Distutils is quite simple both for module developers and for
21users/administrators installing third-party modules. As a developer, your
22responsibilities (apart from writing solid, well-documented and well-tested
23code, of course!) are:
24
25* writing a setup script (:file:`setup.py` by convention)
26
27* (optional) writing a setup configuration file
28
29* creating a source distribution
30
31* (optional) creating one or more "built" (binary) distributions of your
32 project
33
34All of these tasks are covered in this document.
35
Éric Araujo5da37be2011-06-01 20:44:40 +020036Not all module developers have access to multiple platforms, so one cannot
Éric Araujo3a9f58f2011-06-01 20:42:49 +020037expect them to create buildt distributions for every platform. To remedy
38this, it is hoped that intermediaries called *packagers* will arise to address
39this need. Packagers take source distributions released by module developers,
Éric Araujo5da37be2011-06-01 20:44:40 +020040build them on one or more platforms and release the resulting built
41distributions. Thus, users on a greater range of platforms will be able to
42install the most popular Python modules in the most natural way for their
Éric Araujo3a9f58f2011-06-01 20:42:49 +020043platform without having to run a setup script or compile a single line of code.
44
45
46.. _packaging-simple-example:
47
48A Simple Example
49================
50
51A setup script is usually quite simple, although since it's written in Python
52there are no arbitrary limits to what you can do with it, though you should be
53careful about putting expensive operations in your setup script.
54Unlike, say, Autoconf-style configure scripts the setup script may be run
55multiple times in the course of building and installing a module
56distribution.
57
58If all you want to do is distribute a module called :mod:`foo`, contained in a
59file :file:`foo.py`, then your setup script can be as simple as::
60
61 from packaging.core import setup
62 setup(name='foo',
63 version='1.0',
64 py_modules=['foo'])
65
66Some observations:
67
68* most information that you supply to the Distutils is supplied as keyword
69 arguments to the :func:`setup` function
70
71* those keyword arguments fall into two categories: package metadata (name,
Éric Araujo5da37be2011-06-01 20:44:40 +020072 version number, etc.) and information about what's in the package (a list
Éric Araujo3a9f58f2011-06-01 20:42:49 +020073 of pure Python modules in this case)
74
75* modules are specified by module name, not filename (the same will hold true
76 for packages and extensions)
77
Éric Araujo5da37be2011-06-01 20:44:40 +020078* it's recommended that you supply a little more metadata than we have in the
79 example. In particular your name, email address and a URL for the
Éric Araujo3a9f58f2011-06-01 20:42:49 +020080 project if appropriate (see section :ref:`packaging-setup-script` for an example)
81
82To create a source distribution for this module you would create a setup
83script, :file:`setup.py`, containing the above code and run::
84
85 python setup.py sdist
86
87which will create an archive file (e.g., tarball on Unix, ZIP file on Windows)
88containing your setup script :file:`setup.py`, and your module :file:`foo.py`.
89The archive file will be named :file:`foo-1.0.tar.gz` (or :file:`.zip`), and
90will unpack into a directory :file:`foo-1.0`.
91
92If an end-user wishes to install your :mod:`foo` module all he has to do is
93download :file:`foo-1.0.tar.gz` (or :file:`.zip`), unpack it, and from the
94:file:`foo-1.0` directory run ::
95
96 python setup.py install
97
98which will copy :file:`foo.py` to the appropriate directory for
99third-party modules in their Python installation.
100
101This simple example demonstrates some fundamental concepts of Distutils.
102First, both developers and installers have the same basic user interface, i.e.
103the setup script. The difference is which Distutils *commands* they use: the
104:command:`sdist` command is almost exclusively for module developers, while
Éric Araujo5da37be2011-06-01 20:44:40 +0200105:command:`install` is more often used by installers (although some developers
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200106will want to install their own code occasionally).
107
Éric Araujo5da37be2011-06-01 20:44:40 +0200108If you want to make things really easy for your users, you can create more
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200109than one built distributions for them. For instance, if you are running on a
110Windows machine and want to make things easy for other Windows users, you can
111create an executable installer (the most appropriate type of built distribution
112for this platform) with the :command:`bdist_wininst` command. For example::
113
114 python setup.py bdist_wininst
115
116will create an executable installer, :file:`foo-1.0.win32.exe`, in the current
117directory. You can find out what distribution formats are available at any time
118by running ::
119
120 python setup.py bdist --help-formats
121
122
123.. _packaging-python-terms:
124
125General Python terminology
126==========================
127
Éric Araujo5da37be2011-06-01 20:44:40 +0200128If you're reading this document, you probably have a good idea of what Python
129modules, extensions and so forth are. Nevertheless, just to be sure that
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200130everyone is on the same page, here's a quick overview of Python terms:
131
132module
Éric Araujo5da37be2011-06-01 20:44:40 +0200133 The basic unit of code reusability in Python: a block of code imported by
134 some other code. Three types of modules are important to us here: pure
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200135 Python modules, extension modules and packages.
136
137pure Python module
138 A module written in Python and contained in a single :file:`.py` file (and
Éric Araujo5da37be2011-06-01 20:44:40 +0200139 possibly associated :file:`.pyc` and/or :file:`.pyo` files). Sometimes
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200140 referred to as a "pure module."
141
142extension module
143 A module written in the low-level language of the Python implementation: C/C++
144 for Python, Java for Jython. Typically contained in a single dynamically
145 loaded pre-compiled file, e.g. a shared object (:file:`.so`) file for Python
146 extensions on Unix, a DLL (given the :file:`.pyd` extension) for Python
147 extensions on Windows, or a Java class file for Jython extensions. Note that
148 currently Distutils only handles C/C++ extensions for Python.
149
150package
Éric Araujo5da37be2011-06-01 20:44:40 +0200151 A module that contains other modules, typically contained in a directory of
152 the filesystem and distinguished from other directories by the presence of a
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200153 file :file:`__init__.py`.
154
155root package
Éric Araujo5da37be2011-06-01 20:44:40 +0200156 The root of the hierarchy of packages. (This isn't really a package,
157 since it doesn't have an :file:`__init__.py` file. But... we have to
158 call it something, right?) The vast majority of the standard library is
159 in the root package, as are many small standalone third-party modules that
160 don't belong to a larger module collection. Unlike regular packages,
161 modules in the root package can be found in many directories: in fact,
162 every directory listed in ``sys.path`` contributes modules to the root
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200163 package.
164
165
166.. _packaging-term:
167
168Distutils-specific terminology
169==============================
170
171The following terms apply more specifically to the domain of distributing Python
172modules using Distutils:
173
174module distribution
175 A collection of Python modules distributed together as a single downloadable
176 resource and meant to be installed all as one. Examples of some well-known
177 module distributions are NumPy, SciPy, PIL (the Python Imaging
Éric Araujo5da37be2011-06-01 20:44:40 +0200178 Library) or mxBase. (Module distributions would be called a *package*,
179 except that term is already taken in the Python context: a single module
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200180 distribution may contain zero, one, or many Python packages.)
181
182pure module distribution
183 A module distribution that contains only pure Python modules and packages.
184 Sometimes referred to as a "pure distribution."
185
186non-pure module distribution
187 A module distribution that contains at least one extension module. Sometimes
188 referred to as a "non-pure distribution."
189
190distribution root
191 The top-level directory of your source tree (or source distribution). The
Éric Araujo5da37be2011-06-01 20:44:40 +0200192 directory where :file:`setup.py` exists. Generally :file:`setup.py` will
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200193 be run from this directory.