conda-build is an application that automates the process of building and distributing Python packages. It is a powerful tool that has the added advantage of handling dependencies that require C/C++ or other languages. This is particularly useful for scientific computing, where many Python packages have complex dependencies and require specialized libraries and tools.
A recipe outlines the steps needed to build a package from source code. We can create this "recipe" using grayskull
. A recipe includes all the necessary information, from downloading and installing dependencies to compiling the source code and creating the final package. Conda-build then renders the recipe to build the package. A recipe typically includes:
- A script that contains metadata.
- The script that installs the files for the package on macOS and Linux.
- The build script that installs the files for the package on Windows.
- Any additional setup files, depending on the complexity of the package.
In this article, we will guide you through the process of building your Python package with conda-build! conda-build can help us handle even the most complex dependencies for our package, making the process smoother and more efficient. Let's get ready to build an amazing Python package!
Getting Started
Prerequisites
Let's start building our own Python package. First, you need to ensure that you have a functional conda installation (e.g. via Miniconda or Miniforge). You can then use conda to install conda-build.
Installing conda-build
conda-build is usually installed in your base
environment, next to conda
:
$ conda install -n base conda-build
Creating a template for the recipe
For this tutorial, we'll be walking you through the process of building a recipe for PyPI's click
package using Grayskull. Grayskull is an automatic conda recipe generator that is used for creating recipes for Python packages on PyPI and non-PyPI packages available on GitHub. PyPI is among the various repositories available for Python packages on the internet.
$ conda install grayskull
$ grayskull pypi click
You can use any package available from PyPI instead of click
After executing this command, a new directory called click
will be generated. Inside this directory, you'll find a file named meta.yaml
. This file contains all the metadata necessary to create a conda package, such as the package name, version number, source code URL, installation instructions, dependencies, license, and more. Note that if you didn't use the grayskull
command, you'll need to manually write the script yourself. You can make any changes to the meta.yaml
file and add yourself as the maintainer and distributor of your package.
Building the package
To build your click
package, run this command from the root folder:
$ conda build click
After the build process is finished, conda-build will generate a package file in the conda-bld
directory.
To locate this file, you can use this command from the terminal:
$ conda build click --output
You can now install the package locally by running this command in the terminal:
$ conda install --use-local click
Congrats 🎊🎉, you have successfully downloaded your own package! You can modify this package and upload it to Anaconda.org.