17db96d56Sopenharmony_ci.. _distutils-intro: 27db96d56Sopenharmony_ci 37db96d56Sopenharmony_ci**************************** 47db96d56Sopenharmony_ciAn Introduction to Distutils 57db96d56Sopenharmony_ci**************************** 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci.. include:: ./_setuptools_disclaimer.rst 87db96d56Sopenharmony_ci 97db96d56Sopenharmony_ciThis document covers using the Distutils to distribute your Python modules, 107db96d56Sopenharmony_ciconcentrating on the role of developer/distributor: if you're looking for 117db96d56Sopenharmony_ciinformation on installing Python modules, you should refer to the 127db96d56Sopenharmony_ci:ref:`install-index` chapter. 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_ci 157db96d56Sopenharmony_ci.. _distutils-concepts: 167db96d56Sopenharmony_ci 177db96d56Sopenharmony_ciConcepts & Terminology 187db96d56Sopenharmony_ci====================== 197db96d56Sopenharmony_ci 207db96d56Sopenharmony_ciUsing the Distutils is quite simple, both for module developers and for 217db96d56Sopenharmony_ciusers/administrators installing third-party modules. As a developer, your 227db96d56Sopenharmony_ciresponsibilities (apart from writing solid, well-documented and well-tested 237db96d56Sopenharmony_cicode, of course!) are: 247db96d56Sopenharmony_ci 257db96d56Sopenharmony_ci* write a setup script (:file:`setup.py` by convention) 267db96d56Sopenharmony_ci 277db96d56Sopenharmony_ci* (optional) write a setup configuration file 287db96d56Sopenharmony_ci 297db96d56Sopenharmony_ci* create a source distribution 307db96d56Sopenharmony_ci 317db96d56Sopenharmony_ci* (optional) create one or more built (binary) distributions 327db96d56Sopenharmony_ci 337db96d56Sopenharmony_ciEach of these tasks is covered in this document. 347db96d56Sopenharmony_ci 357db96d56Sopenharmony_ciNot all module developers have access to a multitude of platforms, so it's not 367db96d56Sopenharmony_cialways feasible to expect them to create a multitude of built distributions. It 377db96d56Sopenharmony_ciis hoped that a class of intermediaries, called *packagers*, will arise to 387db96d56Sopenharmony_ciaddress this need. Packagers will take source distributions released by module 397db96d56Sopenharmony_cidevelopers, build them on one or more platforms, and release the resulting built 407db96d56Sopenharmony_cidistributions. Thus, users on the most popular platforms will be able to 417db96d56Sopenharmony_ciinstall most popular Python module distributions in the most natural way for 427db96d56Sopenharmony_citheir platform, without having to run a single setup script or compile a line of 437db96d56Sopenharmony_cicode. 447db96d56Sopenharmony_ci 457db96d56Sopenharmony_ci 467db96d56Sopenharmony_ci.. _distutils-simple-example: 477db96d56Sopenharmony_ci 487db96d56Sopenharmony_ciA Simple Example 497db96d56Sopenharmony_ci================ 507db96d56Sopenharmony_ci 517db96d56Sopenharmony_ciThe setup script is usually quite simple, although since it's written in Python, 527db96d56Sopenharmony_cithere are no arbitrary limits to what you can do with it, though you should be 537db96d56Sopenharmony_cicareful about putting arbitrarily expensive operations in your setup script. 547db96d56Sopenharmony_ciUnlike, say, Autoconf-style configure scripts, the setup script may be run 557db96d56Sopenharmony_cimultiple times in the course of building and installing your module 567db96d56Sopenharmony_cidistribution. 577db96d56Sopenharmony_ci 587db96d56Sopenharmony_ciIf all you want to do is distribute a module called :mod:`foo`, contained in a 597db96d56Sopenharmony_cifile :file:`foo.py`, then your setup script can be as simple as this:: 607db96d56Sopenharmony_ci 617db96d56Sopenharmony_ci from distutils.core import setup 627db96d56Sopenharmony_ci setup(name='foo', 637db96d56Sopenharmony_ci version='1.0', 647db96d56Sopenharmony_ci py_modules=['foo'], 657db96d56Sopenharmony_ci ) 667db96d56Sopenharmony_ci 677db96d56Sopenharmony_ciSome observations: 687db96d56Sopenharmony_ci 697db96d56Sopenharmony_ci* most information that you supply to the Distutils is supplied as keyword 707db96d56Sopenharmony_ci arguments to the :func:`setup` function 717db96d56Sopenharmony_ci 727db96d56Sopenharmony_ci* those keyword arguments fall into two categories: package metadata (name, 737db96d56Sopenharmony_ci version number) and information about what's in the package (a list of pure 747db96d56Sopenharmony_ci Python modules, in this case) 757db96d56Sopenharmony_ci 767db96d56Sopenharmony_ci* modules are specified by module name, not filename (the same will hold true 777db96d56Sopenharmony_ci for packages and extensions) 787db96d56Sopenharmony_ci 797db96d56Sopenharmony_ci* it's recommended that you supply a little more metadata, in particular your 807db96d56Sopenharmony_ci name, email address and a URL for the project (see section :ref:`setup-script` 817db96d56Sopenharmony_ci for an example) 827db96d56Sopenharmony_ci 837db96d56Sopenharmony_ciTo create a source distribution for this module, you would create a setup 847db96d56Sopenharmony_ciscript, :file:`setup.py`, containing the above code, and run this command from a 857db96d56Sopenharmony_citerminal:: 867db96d56Sopenharmony_ci 877db96d56Sopenharmony_ci python setup.py sdist 887db96d56Sopenharmony_ci 897db96d56Sopenharmony_ciFor Windows, open a command prompt window (:menuselection:`Start --> 907db96d56Sopenharmony_ciAccessories`) and change the command to:: 917db96d56Sopenharmony_ci 927db96d56Sopenharmony_ci setup.py sdist 937db96d56Sopenharmony_ci 947db96d56Sopenharmony_ci:command:`sdist` will create an archive file (e.g., tarball on Unix, ZIP file on Windows) 957db96d56Sopenharmony_cicontaining your setup script :file:`setup.py`, and your module :file:`foo.py`. 967db96d56Sopenharmony_ciThe archive file will be named :file:`foo-1.0.tar.gz` (or :file:`.zip`), and 977db96d56Sopenharmony_ciwill unpack into a directory :file:`foo-1.0`. 987db96d56Sopenharmony_ci 997db96d56Sopenharmony_ciIf an end-user wishes to install your :mod:`foo` module, all they have to do is 1007db96d56Sopenharmony_cidownload :file:`foo-1.0.tar.gz` (or :file:`.zip`), unpack it, and---from the 1017db96d56Sopenharmony_ci:file:`foo-1.0` directory---run :: 1027db96d56Sopenharmony_ci 1037db96d56Sopenharmony_ci python setup.py install 1047db96d56Sopenharmony_ci 1057db96d56Sopenharmony_ciwhich will ultimately copy :file:`foo.py` to the appropriate directory for 1067db96d56Sopenharmony_cithird-party modules in their Python installation. 1077db96d56Sopenharmony_ci 1087db96d56Sopenharmony_ciThis simple example demonstrates some fundamental concepts of the Distutils. 1097db96d56Sopenharmony_ciFirst, both developers and installers have the same basic user interface, i.e. 1107db96d56Sopenharmony_cithe setup script. The difference is which Distutils *commands* they use: the 1117db96d56Sopenharmony_ci:command:`sdist` command is almost exclusively for module developers, while 1127db96d56Sopenharmony_ci:command:`install` is more often for installers (although most developers will 1137db96d56Sopenharmony_ciwant to install their own code occasionally). 1147db96d56Sopenharmony_ci 1157db96d56Sopenharmony_ciOther useful built distribution formats are RPM, implemented by the 1167db96d56Sopenharmony_ci:command:`bdist_rpm` command, Solaris :program:`pkgtool` 1177db96d56Sopenharmony_ci(:command:`bdist_pkgtool`), and HP-UX :program:`swinstall` 1187db96d56Sopenharmony_ci(:command:`bdist_sdux`). For example, the following command will create an RPM 1197db96d56Sopenharmony_cifile called :file:`foo-1.0.noarch.rpm`:: 1207db96d56Sopenharmony_ci 1217db96d56Sopenharmony_ci python setup.py bdist_rpm 1227db96d56Sopenharmony_ci 1237db96d56Sopenharmony_ci(The :command:`bdist_rpm` command uses the :command:`rpm` executable, therefore 1247db96d56Sopenharmony_cithis has to be run on an RPM-based system such as Red Hat Linux, SuSE Linux, or 1257db96d56Sopenharmony_ciMandrake Linux.) 1267db96d56Sopenharmony_ci 1277db96d56Sopenharmony_ciYou can find out what distribution formats are available at any time by running 1287db96d56Sopenharmony_ci:: 1297db96d56Sopenharmony_ci 1307db96d56Sopenharmony_ci python setup.py bdist --help-formats 1317db96d56Sopenharmony_ci 1327db96d56Sopenharmony_ci 1337db96d56Sopenharmony_ci.. _python-terms: 1347db96d56Sopenharmony_ci 1357db96d56Sopenharmony_ciGeneral Python terminology 1367db96d56Sopenharmony_ci========================== 1377db96d56Sopenharmony_ci 1387db96d56Sopenharmony_ciIf you're reading this document, you probably have a good idea of what modules, 1397db96d56Sopenharmony_ciextensions, and so forth are. Nevertheless, just to be sure that everyone is 1407db96d56Sopenharmony_cioperating from a common starting point, we offer the following glossary of 1417db96d56Sopenharmony_cicommon Python terms: 1427db96d56Sopenharmony_ci 1437db96d56Sopenharmony_cimodule 1447db96d56Sopenharmony_ci the basic unit of code reusability in Python: a block of code imported by some 1457db96d56Sopenharmony_ci other code. Three types of modules concern us here: pure Python modules, 1467db96d56Sopenharmony_ci extension modules, and packages. 1477db96d56Sopenharmony_ci 1487db96d56Sopenharmony_cipure Python module 1497db96d56Sopenharmony_ci a module written in Python and contained in a single :file:`.py` file (and 1507db96d56Sopenharmony_ci possibly associated :file:`.pyc` files). Sometimes referred to as a 1517db96d56Sopenharmony_ci "pure module." 1527db96d56Sopenharmony_ci 1537db96d56Sopenharmony_ciextension module 1547db96d56Sopenharmony_ci a module written in the low-level language of the Python implementation: C/C++ 1557db96d56Sopenharmony_ci for Python, Java for Jython. Typically contained in a single dynamically 1567db96d56Sopenharmony_ci loadable pre-compiled file, e.g. a shared object (:file:`.so`) file for Python 1577db96d56Sopenharmony_ci extensions on Unix, a DLL (given the :file:`.pyd` extension) for Python 1587db96d56Sopenharmony_ci extensions on Windows, or a Java class file for Jython extensions. (Note that 1597db96d56Sopenharmony_ci currently, the Distutils only handles C/C++ extensions for Python.) 1607db96d56Sopenharmony_ci 1617db96d56Sopenharmony_cipackage 1627db96d56Sopenharmony_ci a module that contains other modules; typically contained in a directory in the 1637db96d56Sopenharmony_ci filesystem and distinguished from other directories by the presence of a file 1647db96d56Sopenharmony_ci :file:`__init__.py`. 1657db96d56Sopenharmony_ci 1667db96d56Sopenharmony_ciroot package 1677db96d56Sopenharmony_ci the root of the hierarchy of packages. (This isn't really a package, since it 1687db96d56Sopenharmony_ci doesn't have an :file:`__init__.py` file. But we have to call it something.) 1697db96d56Sopenharmony_ci The vast majority of the standard library is in the root package, as are many 1707db96d56Sopenharmony_ci small, standalone third-party modules that don't belong to a larger module 1717db96d56Sopenharmony_ci collection. Unlike regular packages, modules in the root package can be found in 1727db96d56Sopenharmony_ci many directories: in fact, every directory listed in ``sys.path`` contributes 1737db96d56Sopenharmony_ci modules to the root package. 1747db96d56Sopenharmony_ci 1757db96d56Sopenharmony_ci 1767db96d56Sopenharmony_ci.. _distutils-term: 1777db96d56Sopenharmony_ci 1787db96d56Sopenharmony_ciDistutils-specific terminology 1797db96d56Sopenharmony_ci============================== 1807db96d56Sopenharmony_ci 1817db96d56Sopenharmony_ciThe following terms apply more specifically to the domain of distributing Python 1827db96d56Sopenharmony_cimodules using the Distutils: 1837db96d56Sopenharmony_ci 1847db96d56Sopenharmony_cimodule distribution 1857db96d56Sopenharmony_ci a collection of Python modules distributed together as a single downloadable 1867db96d56Sopenharmony_ci resource and meant to be installed *en masse*. Examples of some well-known 1877db96d56Sopenharmony_ci module distributions are NumPy, SciPy, Pillow, 1887db96d56Sopenharmony_ci or mxBase. (This would be called a *package*, except that term is 1897db96d56Sopenharmony_ci already taken in the Python context: a single module distribution may contain 1907db96d56Sopenharmony_ci zero, one, or many Python packages.) 1917db96d56Sopenharmony_ci 1927db96d56Sopenharmony_cipure module distribution 1937db96d56Sopenharmony_ci a module distribution that contains only pure Python modules and packages. 1947db96d56Sopenharmony_ci Sometimes referred to as a "pure distribution." 1957db96d56Sopenharmony_ci 1967db96d56Sopenharmony_cinon-pure module distribution 1977db96d56Sopenharmony_ci a module distribution that contains at least one extension module. Sometimes 1987db96d56Sopenharmony_ci referred to as a "non-pure distribution." 1997db96d56Sopenharmony_ci 2007db96d56Sopenharmony_cidistribution root 2017db96d56Sopenharmony_ci the top-level directory of your source tree (or source distribution); the 2027db96d56Sopenharmony_ci directory where :file:`setup.py` exists. Generally :file:`setup.py` will be 2037db96d56Sopenharmony_ci run from this directory. 204