Nauč se Python > Kurzy > Začátečnický kurz v Tieto Czech > Working with databasis, JSON and Yaml > Packaging


The Setuptools toolkit, and the older Distutils, for distributing Python packages makes it easy to write install scripts in Python. You can use these scripts to build archive files for distribution, which the programmer (user) can then use for compiling and installing your libraries.

Simple example:

from setuptools import setup

      description='A simple example',
      author='Lumir Jasiok',

Now, you can simply execute

python build

Expected output:

running build
running build_py
creating build
creating build/lib
copying -> build/lib

Now, you can find inside build directory.

Other commands, that are interesting for you are:

python sdist

This command will create source package, that can be shipped independendly on architecture.

python bdist

This command will build "built distribution" package, you can think about it as "binary package" for your platform. By default, bdist will create package in .egg format. This can be placed in private pypi registry and used to distribute python packages internally.

There is also possibility to build RPM packages like this:

python bdist --format=rpm

Other possible options are:

Format Description
gztar gzipped tar file (.tar.gz)
ztar compressed tar file (.tar.Z)
tar tar file (.tar)
zip zip file (.zip)
rpm RPM
pkgtool Solaris pkgtool
sdux HP-UX swinstall
wininst self-extracting ZIP file for Windows
msi Microsoft Installer example:

import os
from setuptools import setup

# Utility function to read the README file.
# Used for the long_description.  It's nice, because now 1) we have a top level
# README file and 2) it's easier to type in the README file than to put a raw
# string in below ...
def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

    name = "an_example_pypi_project",
    version = "0.0.4",
    author = "Andrew Carter",
    author_email = "",
    description = ("An demonstration of how to create, document, and publish "
                                   "to the cheese shop a5"),
    license = "BSD",
    keywords = "example documentation tutorial",
    url = "",
    packages=['an_example_pypi_project', 'tests'],
        "Development Status :: 3 - Alpha",
        "Topic :: Utilities",
        "License :: OSI Approved :: BSD License",

Directory structure

|-- an_example_pypi_project
|   |--
|   |--
|   |--
|-- tests
|-- |--
|-- |--
|-- |--


Python Classifiers


Development Status :: 1 - Planning
Development Status :: 2 - Pre-Alpha
Development Status :: 3 - Alpha
Development Status :: 4 - Beta
Development Status :: 5 - Production/Stable
Development Status :: 6 - Mature
Development Status :: 7 - Inactive
Environment :: Console
Environment :: Console :: Curses
Environment :: Console :: Framebuffer
Environment :: Console :: Newt
Environment :: Console :: svgalib

To see all commands type:

python --help-commands
Standard commands:
  build             build everything needed to install
  build_py          "build" pure Python modules (copy to build directory)
  build_ext         build C/C++ extensions (compile/link to build directory)
  build_clib        build C/C++ libraries used by Python extensions
  build_scripts     "build" scripts (copy and fixup #! line)
  clean             clean up temporary files from 'build' command
  install           install everything from build directory
  install_lib       install all Python modules (extensions and pure Python)
  install_headers   install C/C++ header files
  install_scripts   install scripts (Python or otherwise)
  install_data      install data files
  sdist             create a source distribution (tarball, zip file, etc.)
  register          register the distribution with the Python package index
  bdist             create a built (binary) distribution
  bdist_dumb        create a "dumb" built distribution
  bdist_rpm         create an RPM distribution
  bdist_wininst     create an executable installer for MS Windows
  check             perform some checks on the package
  upload            upload binary package to PyPI

Extra commands:
  alias             define a shortcut to invoke one or more commands
  bdist_egg         create an "egg" distribution
  develop           install package in 'development mode'
  dist_info         create a .dist-info directory
  easy_install      Find/get/install Python packages
  egg_info          create a distribution's .egg-info directory
  install_egg_info  Install an .egg-info directory for the package
  rotate            delete older distributions, keeping N newest files
  saveopts          save supplied options to setup.cfg or other config file
  setopt            set an option in setup.cfg or another config file
  test              run unit tests after in-place build
  upload_docs       Upload documentation to PyPI
  nosetests         Run unit tests using nosetests

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


In order to interact with pypi, you first need to setup an account. Go to and click on Register.

Now, once registered, when you run commands that interact with pypi you’ll have to enter your username and password each time.

To get around this, place a .pypirc file in your $HOME directory on linux. On windows, an you’ll need to set a HOME environ var to point to the directory where this file lives.

The structure of a .pypirc file is pretty simple:

servers = pypi

Registering Your Project

python register

Uploading Your Project

Use bdist command:

python bdist_egg
python bdist_wininst
python sdist
python upload

Toto je stránka lekce z kurzu, který probíhá nebo proběhl naživo s instruktorem.