Packaging with Conda

If you are as happy for the big snake as I am, you have probably wondered how you can create a Conda package with your amazing code. Fear not, in the following text you will learn how to make others go;

conda install -c coolperson amazingcode

Roughly, the only thing needed to create a Conda package, is a ‘meta.yaml’ file specific for your code. This file contains all the metadata needed to create your package and is highly customizable. While this means the meta.yaml can be written to allow your Conda package to work on any operating system and with any dependencies (doesn’t have to be python) it can be annoying to write from scratch (here is a guide for manually writing this file). Since we just want to create a simple Conda package, we will in this guide avoid fiddling around with the meta.yaml file and instead create the file based on a PyPI package. This will also give you a nice template, if you later need to adapt your meta.yaml file.
Note: Conda packages can also be made from GitHub repositories, which is likely favorable in most cases, but it also requires some manual work on the meta.yaml.

1. Create a PyPI package of your code

Lucky for us, the OPIGlet Brennan has already made a guide for creating a PyPI package. Following this guide, I made the ‘out of this world’ avirusplease package.

2. Enter a Conda environment and install dependencies

Since we are creating a Conda package, we need to be working inside a Conda environment. Additionally, we need to install the following two dependencies.
Note: In some versions of Conda these will already be installed.

conda install conda-build anaconda-client

3. Create a meta.yaml file from your PyPI package

The next stop is creating your meta.yaml file. This is done using conda skeleton on your PyPI package to generate a ‘skeleton recipe’ (the meta.yaml file). Below, the meta.yaml is created for the ‘avirusplease’ package.
Note: ‘pypi’ is used because our package is hosted on PyPI, but you can also use ‘cran’ if you live in denial or ‘cpan’ if you are over 50 years.

conda skeleton pypi avirusplease

4. Create (build) your Conda package

You are finally ready to build your Conda package! This is done with the single line below.
Note: You can use ‘–python’ to choose a specific python version for your package, otherwise it will use the python version of your environment.
Note: If you want to be nice, you can build your package using different versions of python (even python 2).

conda-build --python 3.8 avirusplease

Take note of the exact path and filename of your Conda package shown at the end, you will need it in the next step.
Note: The file will be hiding inside the conda-bld folder of your environment and is a .tar.bz2 file. An example is:

/home/tobias/miniconda3/conda-bld/linux-64/avirusplease-0.0.1-py38hfeaa757_0.tar.bz2

5. Upload your Conda package

You have now created your Conda package, but instead of sharing it with the world, you are hiding it on your laptop. To change this, you need to login to your anaconda account via the terminal and upload your code! 
Note: If you do not have an anaconda account, you can quickly sign up here.

anaconda login
anaconda upload --all /home/tobias/miniconda3/conda-bld/linux-64/avirusplease-0.0.1-py38hfeaa757_0.tar.bz2
anaconda logout

Note: The ‘–all’ parameter tells Conda to convert your package to packages working on other platforms as well (macOS, Linux and Windows).

6. Force others to download your package

Congratulations! You have now shared your awesome package through Conda, which is a lovely thing!
Note: Currently, your package is only available from your anaconda accounts channel. This might be for the best, as it will be less likely others will find it.

conda install avirusplease -c tobiasheol

Author