Packaging with distutils

Another useful feature of setup() is that it can create various types of "distribution" formats. It will take all of the modules you've specified and bundle them up into packages that are easy to distribute to others. You can do this with the sdist command (which is shorthand for 'source distribution'):

(palindrome_env)$ python setup.py sdist --format zip
running sdist
running check
warning: check: missing required meta-data: url

warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)

warning: sdist: standard file not found: should have one of README, README.txt

writing manifest file 'MANIFEST'
creating palindrome-1.0
making hard links in palindrome-1.0...
hard linking palindrome.py -> palindrome-1.0
hard linking setup.py -> palindrome-1.0
creating dist
creating 'dist/palindrome-1.0.zip' and adding 'palindrome-1.0' to it
adding 'palindrome-1.0/palindrome.py'
adding 'palindrome-1.0/PKG-INFO'
adding 'palindrome-1.0/setup.py'
removing 'palindrome-1.0' (and everything under it)

If we look, we'll see that this command created a new directory, dist, which contains the newly generated distribution file:

(palindrome_env) $ ls dist
palindrome-1.0.zip

And if we unzip that file we'll see that it contains our project's source code, including the setup.py:

(palindrome_env)$ cd dist
(palindrome_env)$ unzip palindrome-1.0.zip
Archive: palindrome-1.0.zip
inflating: palindrome-1.0/palindrome.py
inflating: palindrome-1.0/PKG-INFO
inflating: palindrome-1.0/setup.py

So now you can send this zip file to anyone who wants to use your code, and they can use the setup.py to install it into their system. Very convenient!

Note that the sdist command can produce distributions of various types. To see the available options, you can use the --help-formats option:

(palindrome_env) $ python setup.py sdist --help-formats
List of available source distribution formats:
--formats=bztar bzip2'ed tar-file
--formats=gztar gzip'ed tar-file
--formats=tar uncompressed tar file
--formats=zip ZIP file
--formats=ztar compressed tar file

This section really just touches on the very basics of distutils. You can find out more about how to use distutils by passing --help to setup.py:

(palindrome_env) $ python setup.py --help
Common commands: (see '--help-commands' for more)

setup.py build will build the package underneath 'build/'
setup.py install will install the package

Global options:
--verbose (-v) run verbosely (default)
--quiet (-q) run quietly (turns verbosity off)
--dry-run (-n) don't actually do anything
--help (-h) show detailed help message
--command-packages list of packages that provide distutils commands

Information display options (just display information, ignore any commands)
--help-commands list all available commands
--name print package name
--version (-V) print package version
--fullname print <package name>-<version>
--author print the author's name
--author-email print the author's email address
--maintainer print the maintainer's name
--maintainer-email print the maintainer's email address
--contact print the maintainer's name if known, else the author's
--contact-email print the maintainer's email address if known, else the
author's
--url print the URL for this package
--license print the license of the package
--licence alias for --license
--description print the package description
--long-description print the long package description
--platforms print the list of platforms
--classifiers print the list of classifiers
--keywords print the list of keywords
--provides print the list of packages/modules provided
--requires print the list of packages/modules required
--obsoletes print the list of packages/modules made obsolete

usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: setup.py --help [cmd1 cmd2 ...]
or: setup.py --help-commands
or: setup.py cmd --help

For many simple projects you'll find that what we've just covered is almost all you need to know.

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset