57

I am a bit confused. There seem to be two different kind of Python packages, source distributions (setup.py sdist) and egg distributions (setup.py bdist_egg).

Both seem to be just archives with the same data, the python source files. One difference is that pip, the most recommended package manager, is not able to install eggs.

What is the difference between the two and what is 'the' way to do distribute my packages?

(Note, I am not wanting to distribute my packages through PyPI, but I want to use a package manager that fetches my dependencies from PyPI)

Peter Smit
  • 27,696
  • 33
  • 111
  • 170

2 Answers2

76

setup.py sdist creates a source distribution: it contains setup.py, the source files of your module/script (.py files or .c/.cpp for binary modules), your data files, etc. The result is an archive that can then be used to recompile everything on any platform.

setup.py bdist (and bdist_*) creates a built distribution: it includes .pyc files, .so/.dll/.dylib for binary modules, .exe if using py2exe on Windows, your data files... but no setup.py. The result is an archive that is specific to a platform (for example linux-x86_64) and to a version of Python, and that can be installed simply by extracting it into the root of your filesystem (executables are in /usr/bin (or equivalent), data files in /usr/share, modules in /usr/lib/pythonX.X/site-packages/...). You can even build rpm archives that can be directly installed using your package manager.

Schnouki
  • 7,527
  • 3
  • 33
  • 38
  • i have tried both the above options. First to create a source distribution and then to create a build distribution. when i create the Src Dist, it creates a .gz file which is okay for drc distribution. But for build distribution when i do bdist, it creates a two folders, one called lib and another an empty directory named bdist.win32. i want to do something like, create a distribution for linux system (maybe binary) which i can give to end users and the end user should be able to use the application but not read the code. how can i do this? is RPM the answer? – Vasanth Nag K V Sep 12 '18 at 08:11
  • 1
    Why would one create a built distribution in Python? What is the use-case? – Martin Thoma Sep 21 '19 at 11:11
  • 1
    @MartinThoma C extensions precompiled for various platforms. – Cochise Ruhulessin Sep 09 '20 at 23:15
  • When I do python3 bdist bdist_wheel the files created (both wheel and the tar.gz contain my source *.py files. The major difference is that the gz file created with the sdist option contains a directory hierarchy and compiled python as well as src. The bdist option contains only the src without the hierarchy. The information content is otherwise identical. Of course this is python3 and 2022 which is a little later than 2011. – Brian Reinhold May 16 '22 at 12:25
24

2021 update: the tools to build and use eggs no longer exist in Python.

There are many more than two different kind of Python (distribution) packages. This command lists many subcommands:

$ python setup.py --help-commands

Notice the various different bdist types.

An egg was a new package type, introduced by setuptools but later adopted by the standard library. It is meant to be installed monolithic onto sys.path. This differs from an sdist package which is meant to have setup.py install run, copying each file into place and perhaps taking other actions as well (building extension modules, running additional arbitrary Python code included in the package).

eggs are largely obsolete at this point in time. EDIT: eggs are gone, they were used with the command "easy_install" that's been removed from Python.

The favored packaging format now is the "wheel" format, notably used by "pip install".

Whether you create an sdist or an egg (or wheel) is independent of whether you'll be able to declare what dependencies the package has (to be downloaded automatically at installation time by PyPI). All that's necessary for this dependency feature to work is for you to declare the dependencies using the extra APIs provided by distribute (the successor of setuptools) or distutils2 (the successor of distutils - otherwise known as packaging in the current development version of Python 3.x).

https://packaging.python.org/ is a good resource for further information about packaging. It covers some of the specifics of declaring dependencies (eg install_requires but not extras_require afaict).

user5994461
  • 5,301
  • 1
  • 36
  • 57
Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
  • The links seem to be not updated in a while. Is there any newer material out there? – Anthony Kong Jul 21 '17 at 02:32
  • @Jean-PaulCalderone Is bdist and sdist both are same ? – PySaad Jan 08 '18 at 10:50
  • 8
    They're not quite. sdist is a "source distribution". bdist is a "binary distribution". For a pure Python project, those things are pretty close. If your project includes any extension modules, though, the sdist includes the source for those extension modules and use of the sdist will require a compiler. The bdist includes the compiled form of those extension modules and does not require a compiler but will only run on a system that is very close to the one you created the bdist on (often requiring the same *distro* on Linux, for example). – Jean-Paul Calderone Jan 08 '18 at 19:20
  • @Jean-PaulCalderone, suppose I have to distribute some images along with Python files, because my `py` files use the images as input. When I use the `sdist` or the `bdist` option, both only include the python files. How do I also package the images along with the python files? – alpha_989 May 27 '18 at 23:54
  • You have to specify which files to include. https://stackoverflow.com/questions/48696115/python-package-with-data-files – Jean-Paul Calderone May 28 '18 at 10:33