Packaging and distributing projects - Python Packaging User Guide
Skip to content Back to top View this page Edit this page Packaging and distributing projects ¶ Page Status : Outdated Last Reviewed : 2023-12-14 This section covers some additional details on configuring, packaging and distributing Python projects with setuptools that aren’t covered by the introductory tutorial in Packaging Python Projects . It still assumes that you are already familiar with the contents of the Installing Packages page.
The section does not aim to cover best practices for Python project development as a whole. For example, it does not provide guidance or tool recommendations for version control, documentation, or testing. For more reference material, see Building and Distributing Packages in the Setuptools docs, but note that some advisory content there may be outdated. In the event of conflicts, prefer the advice in the Python Packaging User Guide.
Requirements for packaging and distributing ¶ First, make sure you have already fulfilled the requirements for installing packages . Install “twine” [ 1 ] : Unix/macOS python3 -m pip install twine Windows py -m pip install twine You’ll need this to upload your project distributions to PyPI (see below ). Configuring your project ¶ Initial files ¶ setup.py ¶ The most important file is setup.py which exists at the root of your project directory.
For an example, see the setup.py in the PyPA sample project . setup.py serves two primary functions: It’s the file where various aspects of your project are configured. The primary feature of setup.py is that it contains a global setup() function. The keyword arguments to this function are how specific details of your project are defined. The most relevant arguments are explained in the section below . It’s the command line interface for running various commands that relate to packaging tasks.
To get a listing of available commands, run python3 setup.py --help-commands . setup.cfg ¶ setup.cfg is an ini file that contains option defaults for setup.py commands. For an example, see the setup.cfg in the PyPA sample project . README.rst / README.md ¶ All projects should contain a readme file that covers the goal of the project. The most common format is reStructuredText with an “rst” extension, although this is not a requirement; multiple variants of Markdown are supported as well (look at setup() ’s long_description_content_type argument).
For an example, see README.md from the PyPA sample project . Note Projects using Setuptools 0.6.27+ have standard readme files ( README.rst , README.txt , or README ) included in source distributions by default. The built-in distutils library adopts this behavior beginning in Python 3.7. Additionally, Setuptools 36.4.0+ will include a README.md if found. If you are using setuptools, you don’t need to list your readme file in MANIFEST.in .
Otherwise, include it to be explicit. MANIFEST.in ¶ A MANIFEST.in is needed when you need to package additional files that are not automatically included in a source distribution. For details on writing a MANIFEST.in file, including a list of what’s included by default, see “ Using MANIFEST.in ”. However, you may not have to use a MANIFEST.in . For an example, the PyPA sample project has removed its manifest file, since all the necessary files have been included by Setuptools 43.0.0 and newer.
Note MANIFEST.in does not affect built distributions such as wheels. LICENSE.txt ¶ Every package should include a license file detailing the terms of distribution. In many jurisdictions, packages without an explicit license can not be legally used or distributed by anyone other than the copyright holder. If you’re unsure which license to choose, you can use resources such as GitHub’s Choose a License or consult a lawyer.
For an example, see the LICENSE.txt from the PyPA sample project . <your package> ¶ Although it’s not required, the most common practice is to include your Python modules and packages under a single top-level package that has the same name as your project, or something very close. For an example, see the sample package that’s included in the PyPA sample project . setup() args ¶ As mentioned above, the primary feature of setup.py is that it contains a global setup() function.
The keyword arguments to this function are how specific details of your project are defined. Some are temporarily explained below until their information is moved elsewhere. The full list can be found in the setuptools documentation . Most of the snippets given are taken from the setup.py contained in the PyPA sample project . See Versioning for more information on ways to use versions to convey compatibility information to your users. packages ¶ packages = find_packages ( include = [ 'sample' , 'sample.*' ]), Set packages to a list of all packages in your project, including their subpackages, sub-subpackages, etc.
Although the packages can be listed manually, setuptools.find_packages() finds them automatically. Use the include keyword argument to find only the given packages. Use the exclude keyword argument to omit packages that are not intended to be released and installed. py_modules ¶ py_modules = [ "six" ], If your project contains any single-file Python modules that aren’t part of a package, set py_modules to a list of the names of the modules (minus the .py extension) in order to make Setuptools aware of them. install_requires ¶ install_requires = [ 'peppercorn' ], “install_requires” should be used to specify what dependencies a project minimally needs to run.
When the project is installed by pip , this is the specification that is used to install its dependencies. For more on using “install_requires” see install_requires vs requirements files . package_data ¶ package_data = { 'sample' : [ 'package_data.dat' ], }, Often, additional files need to be installed into a package . These files are often data that’s closely related to the package’s implementation, or text files containing documentation that might be of interest to programmers using the package.
These files are called “package data”. The value must be a mapping from package name to a list of relative path names that should be copied into the package. The paths are interpreted as relative to the directory containing the package. For more information, see Including Data Files from the setuptools docs . data_files ¶ data_files = [( 'my_data' , [ 'data/data_file' ])], Although configuring package_data is sufficient for most needs, in some cases you may need to place data files outside of your packages .
The data_files directive allows you to do that. It is mostly useful if you need to install files which are used by other programs, which may be unaware of Python packages. Each (directory, files) pair in the sequence specifies the installation directory and the files to install there. The directory must be a relative path (although this may change in the future, see wheel Issue #92 ), and it is interpreted relative to the installation prefix (Python’s sys.prefix for a default installation; site.USER_BASE for a user installation).
Each file name in files is interpreted relative to the setup.py script at the top of the project source distribution. For more information see the distutils section on Installing Additional Files . Note When installing packages as egg, data_files is not supported. So, if your project uses Setuptools , you must use pip to install it. Alternatively, if you must use python setup.py , then you need to pass the --old-and-unmanageable option. scripts ¶ Although setup() supports a scripts keyword for pointing to pre-made scripts to install, the recommended approach to achieve cross-platform compatibility is to use Creating executable scripts entry points (see below).
Choosing a versioning scheme ¶ See Versioning for information on common version schemes and how to choose between them. Working in “development mode” ¶ You can install a project in “e
The rest of this page is on the live site.